Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 757240c

Browse files
author
radha krishna valmiki
committed
fibinocci
1 parent 1dfb59c commit 757240c

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.g.ds.datastructure.graph;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
import java.util.Stack;
6+
7+
public class BreadthFirstSearch {
8+
static class Node
9+
{
10+
int value;
11+
Node next;
12+
public Node(int value,Node next)
13+
{
14+
this.value=value;
15+
this.next=next;
16+
}
17+
18+
}
19+
static class AdjList{
20+
Node head;
21+
public AdjList()
22+
{
23+
24+
}
25+
}
26+
//same as DFSGraph in DepthFirstSearch
27+
static class BFSGraph{
28+
AdjList[] adjList;
29+
int size;
30+
public BFSGraph(int size)
31+
{
32+
this.size=size;
33+
adjList = new AdjList[this.size];
34+
for(int i=0;i<this.size;i++)
35+
{
36+
adjList[i]=new AdjList();
37+
adjList[i].head=null;
38+
}
39+
}
40+
public void addNode(int src,int destination)
41+
{
42+
Node node = new Node(destination,null);
43+
node.next = adjList[src].head;
44+
adjList[src].head =node;
45+
}
46+
/*
47+
do the depth first traverse
48+
maintain the visited node
49+
*/
50+
public void explore(int startIndex)
51+
{
52+
Boolean[] visited = new Boolean[this.size];
53+
for(int i=0;i<this.size;i++)
54+
{
55+
visited[i]=false;
56+
}
57+
Queue<Integer> track = new LinkedList<>();
58+
track.add(startIndex);
59+
visited[startIndex]=true;
60+
while(!track.isEmpty())
61+
{
62+
int element = track.poll();
63+
visited[element]=true;
64+
System.out.println("Visited the Node:"+element);
65+
Node head = adjList[element].head;
66+
67+
while(head!=null)
68+
{
69+
if(visited[head.value]==false)
70+
{
71+
72+
track.add(head.value);
73+
visited[head.value]=true;
74+
75+
}
76+
else
77+
{
78+
head =head.next;
79+
}
80+
}
81+
82+
}
83+
84+
}
85+
}
86+
public static void main(String[] args)
87+
{
88+
BFSGraph bfsGraph = new BFSGraph(6);
89+
bfsGraph.addNode(0,2);
90+
bfsGraph.addNode(0,1);
91+
bfsGraph.addNode(1,4);
92+
bfsGraph.addNode(1,3);
93+
94+
bfsGraph.addNode(1,0);
95+
bfsGraph.addNode(3,1);
96+
bfsGraph.addNode(4,1);
97+
bfsGraph.addNode(2,5);
98+
99+
bfsGraph.addNode(2,0);
100+
bfsGraph.addNode(5,2);
101+
bfsGraph.explore(0);
102+
}
103+
}

src/main/java/com/g/ds/datastructure/graph/DepthFirstSearch.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
/*
77
1.Graph is stored using adjeceny list
8+
2.Visit the first node and the nodes linked to it
9+
if you hit the node linked to it then go the Node and traverse the list attached to Node
10+
and proceed subsequently.
811
*/
912
public class DepthFirstSearch {
1013

@@ -47,6 +50,7 @@ public void addNode(int src,int destination)
4750
}
4851
/*
4952
do the depth first traverse
53+
maintain the visited node
5054
*/
5155
public void explore(int startIndex)
5256
{

src/main/java/com/g/ds/sorting/MergeSort.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public void sort(int[] array)
2222

2323
if(array==null|| array.length==0)
2424
{
25+
26+
2527
return;
2628
}
2729
mergeSort(0,array.length-1);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.g.ds.trivial;
2+
3+
public class FibinocciNumbers {
4+
5+
//Classic recursion Example
6+
/*
7+
Time Complexity: T(n) = T(n-1) + T(n-2) which is exponential.
8+
We can observe that this implementation does a lot of repeated work
9+
(see the following recursion tree). So this is a bad implementation for nth Fibonacci number.
10+
fib(5)
11+
/
12+
fib(4) fib(3)
13+
/ /
14+
fib(3) fib(2) fib(2) fib(1)
15+
/ / /
16+
fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)
17+
/
18+
fib(1) fib(0)
19+
*/
20+
static class FibinocciRecursion
21+
{
22+
public int fibinocci(int n)
23+
{
24+
if(n<=1)
25+
{
26+
return n;
27+
}
28+
29+
return fibinocci(n-1)+fibinocci(n-2);
30+
}
31+
32+
}
33+
34+
35+
//Fibiocci dynamic Programming
36+
/*
37+
We can avoid the repeated work done is the method 1 by storing the Fibonacci numbers calculated so far.
38+
but we are allocating memory for every element that we are calculating
39+
*/
40+
static class FibinocciDynamicProgramming {
41+
42+
public int fibinocci(int n)
43+
{
44+
int[] fib = new int[n+2];
45+
46+
fib[0]=0;
47+
fib[1]=1;
48+
for(int i=2;i<=n;i++)
49+
{
50+
fib[i]=fib[i-1]+fib[i-2];
51+
}
52+
53+
for(int i=0;i<fib.length;i++)
54+
{
55+
System.out.println(i+" : "+fib[i]);
56+
}
57+
return fib[n];
58+
}
59+
60+
}
61+
62+
public static void main(String[] args)
63+
{
64+
FibinocciRecursion fr = new FibinocciRecursion();
65+
System.out.println("Recursion:"+ fr.fibinocci(9));
66+
67+
FibinocciDynamicProgramming fd = new FibinocciDynamicProgramming();
68+
System.out.println("Dynamic Programming:"+ fd.fibinocci(4));
69+
70+
}
71+
}

0 commit comments

Comments
 (0)