-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
40 lines (31 loc) · 975 Bytes
/
App.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
package Graph.Question9;
public class App {
public static void dfsUtil(Graph graph, int source, boolean[] visited) {
visited[source] = true;
System.out.print(source + "-");
for (int nbr: graph.getAdjList()[source]) {
if (!visited[nbr])
dfsUtil(graph, nbr, visited);
}
}
public static void printComponents(Graph graph) {
int vertices = graph.getVertices();
boolean[] visited = new boolean[vertices];
for (int i=0; i<vertices; i++) {
if (!visited[i]) {
dfsUtil(graph, i, visited);
System.out.println();
}
}
}
public static void main(String[] args) {
Graph graph = new Graph(7);
graph.addEdge(0, 1);
graph.addEdge(1, 2);
graph.addEdge(3, 4);
graph.addEdge(5, 3);
graph.addEdge(5, 6);
graph.addEdge(3, 6);
printComponents(graph);
}
}