-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransitiveClosure.java
More file actions
41 lines (34 loc) · 1.13 KB
/
Copy pathTransitiveClosure.java
File metadata and controls
41 lines (34 loc) · 1.13 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
/*
* Sample usage: java TransitiveClosure tinyDG.txt
* Sample usage: java TransitiveClosure mediumDG.txt
*/
public class TransitiveClosure {
private DirectedDFS[] all;
public TransitiveClosure(Digraph G){
all = new DirectedDFS[G.V()];
for (int v=0; v<G.V(); v++)
all[v] = new DirectedDFS(G, v);
}
public boolean reachable(int v, int w){
return all[v].marked(w);
}
public static void main(String[] args){
In in = new In(args[0]);
Digraph G = new Digraph(in);
TransitiveClosure tc = new TransitiveClosure(G);
StdOut.print(" ");
for (int v = 0; v < G.V(); v++)
StdOut.printf("%3d", v);
StdOut.println();
StdOut.println("--------------------------------------------");
// print transitive closure
for (int v = 0; v < G.V(); v++) {
StdOut.printf("%3d: ", v);
for (int w = 0; w < G.V(); w++) {
if (tc.reachable(v, w)) StdOut.print(" T");
else StdOut.print(" ");
}
StdOut.println();
}
}
}