Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f49ca08

Browse files
Graph DFS implemented
1 parent c241658 commit f49ca08

File tree

5 files changed

+83
-11
lines changed

5 files changed

+83
-11
lines changed

‎src/geeksForGeeks/binarySearchTree/BinarySearchTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public static void main(String[] args)
125125
BinarySearchTree tree = new BinarySearchTree();
126126

127127
/* Let us create following BST
128-
50
128+
50
129129
/ \
130130
30 70
131131
/ \ / \

‎src/me/premaseem/datastructure/graph/Graph.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,52 @@ void addEdge(int p1, int p2 ){
2525
}
2626

2727
public static void main(String[] args) {
28+
/* Let us take the graph in cyclic order
29+
0
30+
/ \
31+
1 3
32+
/ \
33+
2 - - - - 4
34+
35+
BFS => >> 0 >> 1 >> 3 >> 2 >> 4
36+
DFS => >> 4 >> 2 >> 1 >> 3 >> 0
37+
38+
*/
39+
2840
Graph g = new Graph(5);
2941

3042
g.addEdge(0, 1);
31-
g.addEdge(0, 2);
3243
g.addEdge(1, 2);
33-
g.addEdge(2, 0);
34-
g.addEdge(2, 3);
35-
g.addEdge(3, 3);
36-
g.addEdge(0, 4);
37-
g.addEdge(4, 1);
44+
g.addEdge(0, 3);
45+
g.addEdge(3, 4);
46+
g.addEdge(2, 4);
47+
48+
49+
g.BFS(0);
50+
g.DFS(0);
51+
52+
}
53+
54+
55+
void DFS(int v){
56+
boolean[] visited = new boolean[size];
57+
DFSR(v,visited);
58+
}
59+
60+
void DFSR(int v, boolean[] visited){
61+
62+
visited[v] = true;
63+
64+
for (int vert: vertex[v]) {
65+
if(!visited[vert]){
66+
DFSR(vert,visited);
67+
}
68+
}
69+
System.out.print(" DFS visiting "+ v);
3870

39-
g.BFS(4);
4071
}
4172

73+
4274
void BFS(int v){
4375

4476
// default marked as false for visited vertex
@@ -51,16 +83,20 @@ void BFS(int v){
5183

5284
while(!q.isEmpty()){
5385
Integer vert = q.poll();
54-
System.out.println(" Visiting "+vert);
86+
System.out.print(" >>"+vert);
5587

5688
// getting the adjacent and adding them to q if they not been visited before
5789
LinkedList<Integer> adj = this.vertex[vert];
5890

5991
for (Integer vo:adj) {
92+
// System.out.print(" checking "+vo);
6093
if(!visited[vo]){
6194
visited[vo] = true;
6295
q.add(vo);
6396
}
97+
// else {
98+
// System.out.println(" already visited");
99+
// }
64100
}
65101

66102
}

‎src/me/premaseem/datastructure/graph/BusRoute.java renamed to ‎src/me/premaseem/datastructure/graph/busRoute/BusRoute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.premaseem.datastructure.graph;
1+
package me.premaseem.datastructure.graph.busRoute;
22

33
public class BusRoute {
44

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package me.premaseem.datastructure.graph.busRoute;
2+
3+
import java.util.ArrayList;
4+
5+
public class Graph {
6+
7+
ArrayList graphArray[];
8+
9+
public Graph(int numOfVertices) {
10+
graphArray = new ArrayList[numOfVertices];
11+
for (int i = 0; i < graphArray.length; i++) {
12+
graphArray[i] = new ArrayList<Integer>();
13+
}
14+
15+
}
16+
17+
public void addEdge(int from, int to) {
18+
graphArray[from].add(to);
19+
}
20+
21+
public void getAdjecents(int vertex) {
22+
ArrayList edges = graphArray[vertex];
23+
for (Object i : edges) {
24+
System.out.printf(" %s --> %s \n", vertex, i);
25+
}
26+
}
27+
28+
public void printGraphEdges() {
29+
for (int i = 0; i < graphArray.length; i++) {
30+
for (Object obj : graphArray[i]) {
31+
System.out.printf(" %s --> %s \n", i, obj);
32+
}
33+
}
34+
35+
}
36+
}

‎src/me/premaseem/datastructure/graph/Test.java renamed to ‎src/me/premaseem/datastructure/graph/busRoute/Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.premaseem.datastructure.graph;
1+
package me.premaseem.datastructure.graph.busRoute;
22

33
public class Test {
44

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /