-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthFirstSearch.java
More file actions
56 lines (47 loc) · 1.24 KB
/
DepthFirstSearch.java
File metadata and controls
56 lines (47 loc) · 1.24 KB
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
package Graph;
import java.util.*;
public class DepthFirstSearch {
static List<Integer> result = new ArrayList<>();
static HashSet<Integer> visited = new HashSet<>();
private static void dfsMain(Graph g) {
if (g.vertices < 1){
return;
}
for (int i = 0; i < g.vertices; i++) {
if (!visited.contains(i))
{
dfs(g, i);
}
}
System.out.println(result);
}
private static void dfs(Graph g, int source) {
result.add(source);
visited.add(source);
for (int item: g.adjacentList.get(source)) {
if (!visited.contains(item))
{
dfs(g, item);
}
}
}
public static void main(String[] args) {
Graph g = new Graph(5);
g.addEdge(0,1);
g.addEdge(0,2);
g.addEdge(1,3);
g.addEdge(1,4);
System.out.println("Graph1:");
g.printGraph();
dfsMain(g);
System.out.println();
Graph g2 = new Graph(5);
g2.addEdge(0,1);
g2.addEdge(0,4);
g2.addEdge(1,2);
g2.addEdge(4,3);
System.out.println("Graph2:");
g2.printGraph();
dfsMain(g2);
}
}