-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStronglyConnected.java
More file actions
90 lines (73 loc) · 2.17 KB
/
StronglyConnected.java
File metadata and controls
90 lines (73 loc) · 2.17 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
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
package algorithm.graph;
import java.util.Iterator;
import java.util.LinkedList;
/**
* The type Strongly connected.
*/
public class StronglyConnected {
/**
* Utility function.
*
* @param g the g
* @param v the v
* @param visited the visited
*/
public static void utilityFunction(Graph g, int v, boolean visited[]) {
visited[v] = true;
int temp;
LinkedList<Integer> Llist[];
Llist = g.getAdj();
Iterator<Integer> i = Llist[v].iterator();
while (i.hasNext()) {
temp = i.next();
if (!visited[temp])
utilityFunction(g, temp, visited);
}
}
/**
* Is strongly connected object.
*
* @param graph the graph
* @return the object
*/
public static Object isStronglyConnected(Graph graph) {
int vertices = graph.getVertices();
boolean[] visited = new boolean[vertices];
utilityFunction(graph, 0, visited);
for (int i = 0; i < vertices; i++)
if (visited[i] == false)
return false;
Graph g1 = graph.getTranspose(graph);
for (int i = 0; i < vertices; i++)
visited[i] = false;
// check transposed graph for non visited vertices
utilityFunction(g1, 0, visited);
// if transpose of graph has any unvisited nodes return false
for (int i = 0; i < vertices; i++)
if (visited[i] == false)
return false;
return true;
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[]) {
Graph g1 = new Graph(5);
g1.addEdge(0, 1);
g1.addEdge(1, 2);
g1.addEdge(2, 3);
g1.addEdge(3, 0);
g1.addEdge(2, 4);
g1.addEdge(4, 2);
Object x = StronglyConnected.isStronglyConnected(g1);
System.out.println("Graph g1 is strongly connected? " + x);
Graph g2 = new Graph(4);
g2.addEdge(0, 1);
g2.addEdge(1, 2);
g2.addEdge(2, 3);
x = StronglyConnected.isStronglyConnected(g2);
System.out.println("Graph g2 is strongly connected? " + x);
}
}