File tree 3 files changed +84
-0
lines changed
Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/directed
3 files changed +84
-0
lines changed Original file line number Diff line number Diff line change
1
+ package ca .mcmaster .chapter .four .graph .directed ;
2
+
3
+ import java .io .File ;
4
+ import java .io .FileInputStream ;
5
+ import java .io .FileNotFoundException ;
6
+
7
+ public class Topological {
8
+ private Iterable <Integer > order ;
9
+ public Topological (Digraph g ){
10
+ DirectedCycle dc = new DirectedCycle (g );
11
+ if (!dc .hasCycle ()){
12
+ DepthFirstOrder dfo = new DepthFirstOrder (g );
13
+ order = dfo .reversePost ();
14
+ }
15
+ }
16
+ public Iterable <Integer > order (){
17
+ return order ;
18
+ }
19
+ public boolean isDAG (){
20
+ return order != null ;
21
+ }
22
+ public static void main (String [] args ) throws FileNotFoundException {
23
+ Digraph g = new DigraphImpl (new FileInputStream (new File ("src/ca/mcmaster/chapter/four/graph/tinyDG.txt" )));
24
+ Topological topological = new Topological (g );
25
+ StringBuilder sb = new StringBuilder ();
26
+ for (int i : topological .order ()){
27
+ sb .append (i + " " );
28
+ }
29
+ System .out .println (sb .toString ());
30
+ }
31
+ }
Original file line number Diff line number Diff line change @@ -388,3 +388,25 @@ public class DepthFirstOrder {
388
388
}
389
389
}
390
390
```
391
+
392
+ ### 拓扑排序
393
+ > 给定一张有向图,将所有的顶点排序,使得所有的有向边均从排在前面的元素指向排在后面的元素。
394
+ > 有环图画不出拓扑图,因为有环图无法确定环上定点的顺序。
395
+ ``` Java
396
+ public class Topological {
397
+ private Iterable<Integer > order;
398
+ public Topological (Digraph g ){
399
+ DirectedCycle dc = new DirectedCycle (g);
400
+ if (! dc. hasCycle()){
401
+ DepthFirstOrder dfo = new DepthFirstOrder (g);
402
+ order = dfo. reversePost(); // depth first order的逆后序就是拓扑图的顺序。
403
+ }
404
+ }
405
+ public Iterable<Integer > order (){
406
+ return order;
407
+ }
408
+ public boolean isDAG (){
409
+ return order != null ;
410
+ }
411
+ }
412
+ ```
Original file line number Diff line number Diff line change
1
+ package ca .mcmaster .chapter .four .graph .directed ;
2
+
3
+ import java .io .File ;
4
+ import java .io .FileInputStream ;
5
+ import java .io .FileNotFoundException ;
6
+
7
+ public class Topological {
8
+ private Iterable <Integer > order ;
9
+ public Topological (Digraph g ){
10
+ DirectedCycle dc = new DirectedCycle (g );
11
+ if (!dc .hasCycle ()){
12
+ DepthFirstOrder dfo = new DepthFirstOrder (g );
13
+ order = dfo .reversePost ();
14
+ }
15
+ }
16
+ public Iterable <Integer > order (){
17
+ return order ;
18
+ }
19
+ public boolean isDAG (){
20
+ return order != null ;
21
+ }
22
+ public static void main (String [] args ) throws FileNotFoundException {
23
+ Digraph g = new DigraphImpl (new FileInputStream (new File ("src/ca/mcmaster/chapter/four/graph/tinyDG.txt" )));
24
+ Topological topological = new Topological (g );
25
+ StringBuilder sb = new StringBuilder ();
26
+ for (int i : topological .order ()){
27
+ sb .append (i + " " );
28
+ }
29
+ System .out .println (sb .toString ());
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments