7 Star 32 Fork 21

空無一悟 / algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Main.java 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
空無一悟 提交于 2021-09-22 23:47 . init
/***********************************************************
* @Description : 拓扑排序测试
* @author : 梁山广(Liang Shan Guang)
* @date : 2019/12/26 0:13
* @email : liangshanguang2@gmail.com
***********************************************************/
package Chapter13DirectedGraph.Section06to09TopoSort;
import Chapter02GraphExpress.Graph;
import Chapter02GraphExpress.ReadGraph;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 1.无环,可以进行拓扑排序
String filepath = "src/main/java/Chapter13DirectedGraph/Section06to09TopoSort/有向无权无环图.txt";
Graph graph = new Graph(true);
ReadGraph.init(graph, filepath);
TopoSort topoSort = new TopoSort(graph);
List<Integer> topoOrder = topoSort.getTopoOrder();
System.out.print("拓扑排序结果为:");
System.out.println(topoOrder);
System.out.println(topoSort.hasCycle()?"图中有环":"图中无环");
System.out.println();
// 2.有环,无法进行拓扑排序
filepath = "src/main/java/Chapter13DirectedGraph/Section06to09TopoSort/有向无权有环图.txt";
graph = new Graph(true);
ReadGraph.init(graph, filepath);
topoSort = new TopoSort(graph);
topoOrder = topoSort.getTopoOrder();
System.out.print("拓扑排序结果为:");
System.out.println(topoOrder);
System.out.println(topoSort.hasCycle()?"图中有环":"图中无环");
}
}
/**
* 顶点数V = 5, 边数E = 5
* 拓扑排序结果为:[0, 1, 3, 2, 4]
* 图中无环
*
* 顶点数V = 5, 边数E = 5
* 拓扑排序结果为:[]
* 图中有环
*/
1
https://gitee.com/lsgwr/algorithms.git
git@gitee.com:lsgwr/algorithms.git
lsgwr
algorithms
algorithms
master

搜索帮助