🌓 天气与日历 切换到窄版

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 159|回复: 0

[其它教程] 拓扑排序,分别用C语言,C++,java,python编写出来

[复制链接]

2972

主题

4

回帖

3044

积分

管理员

积分
3044
发表于 2024-2-27 12:29:00 | 显示全部楼层 |阅读模式
拓扑排序是一种把有向无环图转换成线性序列的排序算法,算法的输入是一个有向无环图,经过算法分析吧图中的所有节点按照先后顺序进行拆解,最后得到一个有顺序的队列,在前的节点靠前,越靠后的节点或有多个节点指向该节点,那这个节点再队列中的位置就越靠后。

拓扑排序,分别用C语言,C++,java,python编写出来
下面是拓扑排序(Topological Sort)的不同语言实现示例:
1. C语言:

  1. #include <stdio.h>
  2. #define MAX_SIZE 100
  3. void topologicalSort(int graph[][MAX_SIZE], int n) {
  4.     int indegree[MAX_SIZE] = {0};
  5.     int queue[MAX_SIZE];
  6.     int front = 0, rear = 0;
  7.      // 计算每个顶点的入度
  8.     for (int i = 0; i < n; i++) {
  9.         for (int j = 0; j < n; j++) {
  10.             indegree[i] += graph[j][i];
  11.         }
  12.     }
  13.      // 将入度为0的顶点入队列
  14.     for (int i = 0; i < n; i++) {
  15.         if (indegree[i] == 0) {
  16.             queue[rear++] = i;
  17.         }
  18.     }
  19.      // 拓扑排序
  20.     while (front < rear) {
  21.         int vertex = queue[front++];
  22.         printf("%d ", vertex);
  23.          for (int i = 0; i < n; i++) {
  24.             if (graph[vertex][i] == 1) {
  25.                 indegree[i]--;
  26.                 if (indegree[i] == 0) {
  27.                     queue[rear++] = i;
  28.                 }
  29.             }
  30.         }
  31.     }
  32. }

复制代码
int main() {
    int n = 6;
    int graph[MAX_SIZE][MAX_SIZE] = {
        {0, 0, 0, 0, 0, 0},
        {0, 0, 1, 0, 0, 0},
        {0, 0, 0, 1, 0, 0},
        {0, 0, 0, 0, 1, 0},
        {0, 1, 0, 0, 0, 0},
        {1, 1, 0, 0, 0, 0}
    };
     printf("Topological Sort: ");
    topologicalSort(graph, n);
     return 0;
}





2. C++:

  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5. void topologicalSort(vector<vector<int>>& graph) {
  6.     int n = graph.size();
  7.     vector<int> indegree(n, 0);
  8.     queue<int> q;
  9.      // 计算每个顶点的入度
  10.     for (int i = 0; i < n; i++) {
  11.         for (int j = 0; j < n; j++) {
  12.             indegree[i] += graph[j][i];
  13.         }
  14.     }
  15.      // 将入度为0的顶点入队列
  16.     for (int i = 0; i < n; i++) {
  17.         if (indegree[i] == 0) {
  18.             q.push(i);
  19.         }
  20.     }
  21.      // 拓扑排序
  22.     while (!q.empty()) {
  23.         int vertex = q.front();
  24.         q.pop();
  25.         cout << vertex << " ";
  26.          for (int i = 0; i < n; i++) {
  27.             if (graph[vertex][i] == 1) {
  28.                 indegree[i]--;
  29.                 if (indegree[i] == 0) {
  30.                     q.push(i);
  31.                 }
  32.             }
  33.         }
  34.     }
  35. }

复制代码
int main() {
    int n = 6;
    vector<vector<int>> graph = {
        {0, 0, 0, 0, 0, 0},
        {0, 0, 1, 0, 0, 0},
        {0, 0, 0, 1, 0, 0},
        {0, 0, 0, 0, 1, 0},
        {0, 1, 0, 0, 0, 0},
        {1, 1, 0, 0, 0, 0}
    };
     cout << "Topological Sort: ";
    topologicalSort(graph);
     return 0;
}





3. Java:

  1. import java.util.*;
  2. public class TopologicalSort {
  3.     public static void topologicalSort(int[][] graph) {
  4.         int n = graph.length;
  5.         int[] indegree = new int[n];
  6.         Queue<Integer> queue = new LinkedList<>();
  7.          // 计算每个顶点的入度
  8.         for (int i = 0; i < n; i++) {
  9.             for (int j = 0; j < n; j++) {
  10.                 indegree[i] += graph[j][i];
  11.             }
  12.         }
  13.          // 将入度为0的顶点入队列
  14.         for (int i = 0; i < n; i++) {
  15.             if (indegree[i] == 0) {
  16.                 queue.add(i);
  17.             }
  18.         }
  19.          // 拓扑排序
  20.         while (!queue.isEmpty()) {
  21.             int vertex = queue.poll();
  22.             System.out.print(vertex + " ");
  23.              for (int i = 0; i < n; i++) {
  24.                 if (graph[vertex][i] == 1) {
  25.                     indegree[i]--;
  26.                     if (indegree[i] == 0) {
  27.                         queue.add(i);
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.     }
  33.    
  34. }
复制代码
public static void main(String[] args) {
        int n = 6;
        int[][] graph = {
            {0, 0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0, 0},
            {0, 0, 0, 1, 0, 0},
            {0, 0, 0, 0, 1, 0},
            {0, 1, 0, 0, 0, 0},
            {1, 0, 0, 0, 0, 0}
        };
         System.out.print("Topological Sort: ");
        topologicalSort(graph);
    }




4. Python:

  1. from collections import deque
  2. def topological_sort(graph):
  3.     n = len(graph)
  4.     indegree = [0] * n
  5.     queue = deque()
  6.      # 计算每个顶点的入度
  7.     for i in range(n):
  8.         for j in range(n):
  9.             indegree[i] += graph[j][i]
  10.      # 将入度为0的顶点入队列
  11.     for i in range(n):
  12.         if indegree[i] == 0:
  13.             queue.append(i)
  14.      # 拓扑排序
  15.     while queue:
  16.         vertex = queue.popleft()
  17.         print(vertex, end=" ")
  18.          for i in range(n):
  19.             if graph[vertex][i] == 1:
  20.                 indegree[i] -= 1
  21.                 if indegree[i] == 0:
  22.                     queue.append(i)
  23. # 主函数
  24. # 下面隐藏代码为主函数
  25. # 调用主函数
  26. if __name__ == "__main__":
  27.     main()
复制代码
def main():
    n = 6
    graph = [
        [0, 0, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0],
        [0, 0, 0, 1, 0, 0],
        [0, 0, 0, 0, 1, 0],
        [0, 1, 0, 0, 0, 0],
        [1, 1, 0, 0, 0, 0]

     print("Topological Sort: ", end="")
    topological_sort(graph)





这些示例展示了使用不同编程语言实现拓扑排序算法的方式。请根据您选择的编程语言,使用相应的示例来实现拓扑排序算法。


扫码关注微信公众号,免费查看完整算法内容。

相关帖子

扫码关注微信公众号,及时获取最新资源信息!下载附件优惠VIP会员5折;永久VIP免费
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。
快速回复 返回顶部 返回列表