基于JAVA的拓扑排序实现

10 篇文章 0 订阅
5 篇文章 0 订阅

算法分析课程作业(仅供参考)

源代码:

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值