-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFSGraphTraversal.java
112 lines (89 loc) · 3.56 KB
/
DFSGraphTraversal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.*;
// A graph node
class GraphNode{
String name;
// this is a list of adjacent nodes for the current node and is used in adjacency list
ArrayList<GraphNode> neighbours;
// constructor
GraphNode(String name){
this.name = name;
this.neighbours = new ArrayList<>();
}
}
public class DFSGraphTraversal {
// The node list of graph , also used to maintain adjacency list
ArrayList<GraphNode> nodeList;
// constructor
DFSGraphTraversal(){
this.nodeList = new ArrayList<>();
}
// function to add an undirected and unweighted edge between two nodes with index i and j in nodeList
public void addUndirectedEdge(int i, int j){
GraphNode node1 = this.nodeList.get(i);
GraphNode node2 = this.nodeList.get(j);
this.nodeList.get(i).neighbours.add(node2);
this.nodeList.get(j).neighbours.add(node1);
}
// function to do DFS Traversal on Graph
// Time Complexity - O(V+E) Space Complexity - O(V+E)
public void dfs(){
System.out.println("DFS Traversal of given Graph: ");
// using hashset to keep track of visited nodes is better as compared to using flags
// If we use flags, we should reset all the visited flags to false for all the nodes after one bfs iteration.
// But in hashset we don not need to do anything like thing as a new hashset is created for every iteration of bfs
HashSet<GraphNode> visitedNodes = new HashSet<>();
for(GraphNode graphNode : this.nodeList){
if(!visitedNodes.contains(graphNode)){
dfsVisit(graphNode,visitedNodes);
}
}
}
// helper function for dfs which we are going to call recursively
private void dfsVisit(GraphNode curr , HashSet<GraphNode> visitedNodes){
// base case of recursion
if(visitedNodes.contains(curr)){
return;
}
System.out.print(curr.name+" ");
visitedNodes.add(curr);
for(GraphNode neighbour : curr.neighbours){
dfsVisit(neighbour,visitedNodes);
}
}
public static void main(String[] args) throws Exception {
DFSGraphTraversal graph = new DFSGraphTraversal();
// add 6 graph nodes to the nodeList
for(int i=0;i<6;i++){
graph.nodeList.add(new GraphNode("V"+i));
}
// Now we have to add edges between adjacent nodes.
// we are using adjacency list here
/* undirected and Unweighted Graph used for this code :
V0
/ \
V1 V2
/ \ |
| \ /
V3----V4
\ /
\ /
V5
*/
// add adjancent nodes of each node
graph.addUndirectedEdge(2,4);
graph.addUndirectedEdge(0,1);
graph.addUndirectedEdge(1,3);
graph.addUndirectedEdge(0,2);
graph.addUndirectedEdge(3,4);
graph.addUndirectedEdge(4,5);
graph.addUndirectedEdge(3,5);
graph.addUndirectedEdge(1,4);
// Now as we have added all the edges or adjacent nodes in adjacency list [the neighbour ArrayList of each node],
// we are ready to traverse the graph
graph.dfs();
}
}
/* ====================== OUTPUT =============================
DFS Traversal of given Graph:
V0 V1 V3 V4 V2 V5
=============================================================*/