forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Depth_First_Search.java
133 lines (111 loc) · 3.62 KB
/
Depth_First_Search.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.util.Hashtable;
import java.util.List;
import java.util.LinkedList;
import java.util.Stack;
/*
Instructions to run the program:
javac Depth_First_Search.java
java Graph
*/
// The Graph class represents a directed graph
// Constructor template: new Graph(integer)
// Argument: (integer) represents the number of vertices that the graph will possess
// Example: Graph graph = new Graph(10);
class Graph
{
int numberOfVertices;
Hashtable<Integer,List<Integer>> adj;
// Constructor which initializes 'numberOfVertices' and a Hashtable 'adj' which
// represents an adjacency list which contains a mapping between a vertex and
// it's adjacent vertices or neighbors
Graph(int numberOfVertices)
{
this.numberOfVertices = numberOfVertices;
this.adj = new Hashtable<Integer,List<Integer>>();
}
// Function to add an edge between the source and destination
// GIVEN: a source and a destination node
// EFFECT: adds an edge between the source and destination which is reflected
// in the 'adj' adjacency list
private void addEdge(int source, int destination)
{
// if the source node is not already present in the Hashtable 'adj' we create
// an entry with the given destination node
if(adj.get(source) == null)
{
LinkedList<Integer> neighbours = new LinkedList<Integer>();
neighbours.add(destination);
adj.put(source,neighbours);
}
// if the source node is already present in the Hashtable 'adj' we update
// the entry by adding destination node to the list of neighboring nodes
else
{
List<Integer> neighbours = adj.get(source);
neighbours.add(destination);
adj.put(source,neighbours);
}
}
// GIVEN: a start nodes
// EFFECT: prints out the sequence of Depth First Search in the preorder fashion
// (natural order in which the nodes are visited)
private void dfs(int start)
{
// maintains a record of visited nodes
boolean[] visited = new boolean[numberOfVertices];
// initially no nodes are visited
for(int i=0;i<numberOfVertices;i++)
{
visited[i] = false;
}
// maintain a recursionStack to avoid repetition of same recursive calls
// which might happen due to cycles (in DFS cycles are caused by
// back edges - an edge connecting a vertex u to it's ancestor v)
Stack<Integer> recursionStack = new Stack<Integer>();
visited[start] = true;
recursionStack.push(start);
while(!recursionStack.empty())
{
start = recursionStack.pop();
System.out.print(start+" ");
// for each adjacent node check if it's visited
// if visited is true - skip
// else - mark as visited and add it to recursion stack so that this node
// is processed next (in LIFO fashion)
for(Integer neighbor : adj.get(start))
{
if(!visited[neighbor])
{
visited[neighbor] = true;
recursionStack.push(neighbor);
}
}
}
}
public static void main(String args[])
{
// create new graph with 8 vertices
Graph graph = new Graph(8);
// Create edges between vertices
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 2);
graph.addEdge(1, 4);
graph.addEdge(2, 0);
graph.addEdge(2, 3);
graph.addEdge(3, 3);
graph.addEdge(3, 6);
graph.addEdge(4, 0);
graph.addEdge(4, 5);
graph.addEdge(5, 6);
graph.addEdge(5, 7);
graph.addEdge(6, 2);
graph.addEdge(7, 3);
// Output the DFS traversal
System.out.print("Depth First Traversal is: ");
graph.dfs(0);
}
/* Output
Depth First Traversal is : 0 2 3 6 1 4 5 7
*/
}