【数据结构】拓扑排序C++实现

#include <iostream>
#include <string>
#include <queue>
using namespace std;
#define max_nv 100

typedef struct g* G;
struct g{
    int nv;     // 顶点数
    int ne;
    int Graph[max_nv][max_nv];
};

G init_graph() {
    G graph = new g;
    cin >> graph->nv;
    cin >> graph->ne;
    
    // 初始化
    for(int i=0; i<graph->nv; i++) {
        for(int j=0; j<graph->nv; j++) {
            graph->Graph[i][j] = 0;
        }
    }

    for(int i=0; i<graph->ne; i++) {
        int a, b;
        cin >> a >> b;
        graph->Graph[a][b] = 1;     //  确定是下标从0开始
    }

    return graph;
}

vector<int> cal_indgree(G graph) {
    // 记录每个节点的度
    vector<int> ret;
    for(int j=0; j<graph->nv; j++) {            // 改列和为0, 则说明该结点入度为0
        int cnt = 0;
        for(int i=0; i<graph->nv; i++) {
            if(graph->Graph[i][j]==1) {
                cnt++;
            }
        }
        ret.push_back(cnt);
    }

    return ret;
}

void TopSort(G graph, vector<int>& indegree) {
    queue<int> que;     // 存储入度为0的节点
    for(int i=0; i< graph->nv; i++) {
        if(indegree[i]==0)
            que.push(i);
    }

    vector<vector<int>> ans;
    int cnt = 0;
    while(!que.empty()) {
        int tt = que.size();
        vector<int> top;
        for(int i=0; i<tt; i++) {
            int tmp = que.front();
            que.pop();

            top.push_back(tmp);
            cnt++;

            // 断开tmp的后续节点的边
            for(int j=0; j<graph->nv; j++) {
                if(graph->Graph[tmp][j] == 1) {
                    indegree[j]--;
                    if(indegree[j]==0)
                        que.push(j);
                }
            }
        }
        ans.push_back(top);
    }

    if(cnt != graph->nv)
        cout << "loop is in the graph" << endl;

    for(int i=0; i<ans.size(); i++) {
        for(int j=0; j<ans[i].size(); j++) {
            cout << ans[i][j] + 1 << ' ';
        }
        cout << endl;
    }

}


int main()
{
    G graph = init_graph();
    vector<int> indegree = cal_indgree(graph);
    TopSort(graph, indegree);


    return 0;
}


// 测试用例
//5 6
//4 0
//0 1
//0 2
//1 3
//2 1
//2 4
// 0 2 1 3 4

// ZJU 陈越姥姥例子
// 15 14
// 0 2
// 1 2
// 3 4
// 4 5
// 2 6
// 7 8
// 6 9
// 8 9
// 6 10
// 8 10
// 6 11
// 1 12
// 9 13
// 5 14
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
拓扑排序(Topological Sort)是一种基于有向无环图(DAG)的排序算法,常用于解决任务调度、依赖关系等问题。 以下是拓扑排序的C语言实现示例: ```c #include <stdio.h> #define MAXN 1000 int n; // 顶点个数 int m; // 边的个数 int cnt[MAXN]; // 每个顶点的入度 int g[MAXN][MAXN]; // 图的邻接矩阵 void topo_sort() { int q[MAXN], head = 0, tail = -1; // 队列 // 将入度为0的顶点加入队列 for (int i = 0; i < n; i++) { if (cnt[i] == 0) { q[++tail] = i; } } // 循环取出队头,更新与之相邻的顶点的入度, // 若入度为0,则加入队列 while (head <= tail) { int u = q[head++]; printf("%d ", u); // 输出拓扑序列 for (int v = 0; v < n; v++) { if (g[u][v]) { cnt[v]--; if (cnt[v] == 0) { q[++tail] = v; } } } } } int main() { scanf("%d%d", &n, &m); // 读入顶点个数和边的个数 for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); // 读入一条边 g[u][v] = 1; // 添加边 cnt[v]++; // 更新入度 } topo_sort(); // 进行拓扑排序 return 0; } ``` 在上述代码中,`n`表示顶点个数,`m`表示边的个数,`cnt`表示每个顶点的入度,`g`表示图的邻接矩阵。`topo_sort`函数实现拓扑排序的过程。首先将所有入度为0的顶点加入队列。然后循环取出队头,更新与之相邻的顶点的入度,若入度为0,则加入队列。循环直到队列为空。在循环过程中,每次取出的队头即为拓扑序列中的一个顶点,输出即可。 注意,在实现中,为了方便,这里使用了队列来存储入度为0的顶点,而不是用优先队列来维护入度最小的顶点。因为这里没有对边的权值进行处理,所以使用队列并不影响结果。如果需要处理边的权值,则需要使用优先队列,具体实现可以参考其他资料。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值