-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
48 lines (35 loc) · 1.2 KB
/
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
41
42
43
44
45
46
47
48
package Graph.Question13;
import java.util.Arrays;
public class App {
private static void dfsUtil(Graph graph, boolean[] visited, int source) {
visited[source] = true;
for (int nbr: graph.getAdjList()[source]) {
if (!visited[nbr])
dfsUtil(graph, visited, nbr);
}
}
public static int getMotherVertex(Graph graph) {
boolean[] visited = new boolean[graph.getVertices()];
int lastVertex = 0; //Either last vertex in dfs would be mother vertex (or one of mother vertex), else no mother vertex exist
for (int i=0; i<graph.getVertices(); i++) {
if (!visited[i]) {
dfsUtil(graph, visited, i);
lastVertex = i;
}
}
Arrays.fill(visited, false);
dfsUtil(graph, visited, lastVertex);
for (boolean flag: visited)
if (!flag)
return -1;
return lastVertex;
}
public static void main(String[] args) {
Graph graph = new Graph(4);
graph.addEdge(0,1);
graph.addEdge(1,2);
graph.addEdge(3,0);
graph.addEdge(3,1);
System.out.println(getMotherVertex(graph));
}
}