算法分析课程作业(仅供参考)
源代码:
import java.util.Arrays;
/**
* @description: Topological Sort
* @author: Qing Zhang
* @time: 09
*/
public class TopologicalSort {
/**
* @Description: Judge whether the in-degree is 0
* @Param: [paraGraph : Current graph, paraNode : The node be judged]
* @return: boolean
*/
public static boolean judgeNode(int[][] paraGraph, int paraNode) {
for (int i = 0; i < paraGraph.length; i++) {
if (paraGraph[i][paraNode] == 1) {
return false;
}
}
return true;
}
/**
* @Description: Modify the out degree of the current node to 0
* @Param: [paraGraph, paraNode]
* @return: void
*/
public static void reviseGraph(int[][] paraGraph, int paraNode) {
for (int i = 0; i < paraGraph.length; i++) {
paraGraph[paraNode][i] = 0;
}
}
/**
* @Description: Topological-Sort based on recursion.
* Output multiple results
* @Param: [paraGraph : Current graph, paraVisited : Current status of nodes,
* paraCount : The number of nodes be visited, paraStr : Current topological sorting sequence]
* @return: boolean
*/
public static boolean topologicalSort(int[][] paraGraph, boolean[] paraVisited, int paraCount, String paraStr) {
int length = paraGraph.length;
// Termination condition.
if (length == paraCount) {
System.out.println("----------------------");
System.out.println(paraStr);
return false;
}
// Store the status of the current graph.
boolean[] tempVisited;
int[][] tempGraph;
boolean isRing = true; // Judge whether has ring.
for (int i = 0; i < length; i++) {
if (!paraVisited[i] && judgeNode(paraGraph, i)) {
// Deep copy.
tempVisited = paraVisited.clone();
tempVisited[i] = true;
tempGraph = new int[length][length];
for (int j = 0; j < length; j++) {
tempGraph[j] = paraGraph[j].clone();
}
reviseGraph(tempGraph, i);
isRing = topologicalSort(tempGraph, tempVisited, paraCount + 1, new String(paraStr + i));
}
}
return isRing;
}
/**
* @Description: Use a directed acyclic graph to test topological sorting.
* @Param: []
* @return: void
*/
public static void unitTestForTPSort() {
// Initial.
int[][] testGraph = new int[][]{
{0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0}
};
boolean[] visited = new boolean[testGraph.length];
boolean isCycle = TopologicalSort.topologicalSort(testGraph, visited, 0, "");
if (isCycle) {
System.out.println("The graph has at least one cycle!");
}
}
/**
* @Description: Use a directed graph with rings to test topological sorting.
* @Param: []
* @return: void
*/
public static void unitTestForTPSort_Cycle() {
// Initial.
int[][] testGraph = new int[][]{
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 1},
{0, 1, 0, 0, 0},
{0, 0, 0, 0, 0},
};
boolean[] visited = new boolean[testGraph.length];
boolean isCycle = TopologicalSort.topologicalSort(testGraph, visited, 0, "");
if (isCycle) {
System.out.println("The graph has at least one cycle!");
}
}
public static void main(String[] args) {
System.out.println("\nUse a directed acyclic graph to test topological sorting :");
TopologicalSort.unitTestForTPSort();
System.out.println("\nUse a directed graph with rings to test topological sorting :");
TopologicalSort.unitTestForTPSort_Cycle();
}
}
构造数据:
有向无环图:
带环的有向图:
运行结果:
C:\Users\Administrator\.jdks\temurin-11.0.12-1\bin\java.exe "-javaagent:E:\IntelliJ IDEA 2020.1\lib\idea_rt.jar=59224:E:\IntelliJ IDEA 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\JavaProject\JustForTest\out\production\JustForTest TopologicalSort
Use a directed acyclic graph to test topological sorting :
----------------------
01234567
----------------------
01234657
----------------------
01236457
----------------------
01243567
----------------------
01243657
----------------------
01245367
----------------------
01245637
----------------------
01246357
----------------------
01246537
----------------------
01263457
----------------------
01264357
----------------------
01264537
----------------------
10234567
----------------------
10234657
----------------------
10236457
----------------------
10243567
----------------------
10243657
----------------------
10245367
----------------------
10245637
----------------------
10246357
----------------------
10246537
----------------------
10263457
----------------------
10264357
----------------------
10264537
Use a directed graph with rings to test topological sorting :
The graph has at least one cycle!
Process finished with exit code 0