拓扑排序 C++实现

(1) Directed Acyclic Graph 无环有向图

            检测一个工程是否顺利进行

            若以图中顶点表示活动,,有向边表示活动之间的优先关系,此图为AOV(Activity On Vertex network)

           Vi -> Vj ,表示 Vi 是 Vj 的前趋,Vj 是 Vi 的后继

(2)对AOV排序,即拓扑排序,算法:

             1.从AOV中选取无前趋的点(即入度为0的点)且输出

             2.从网中删去该点,并且删除所有由该点发出的边

             3.重复1,2,直到网中不存在无前趋的点

(3)结果

             1. 网中无点,成功。

             2.有点,说明网中有回路,排序失败。

(4)具体实现

#include <iostream>
using namespace std;
#define N 9

/* if (Vi -> Vj)  matrix[i][j] = 1   */
int matrix[N][N]=
{
0,1,1,1,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,
0,0,0,0,1,0,0,0,0,
0,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,1,1,0,
0,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,1,
0,0,0,0,0,0,0,0,1,
0,0,0,0,0,0,0,0,0
};


int into[N];  // Vertex N 's  indegrees
/* functions declaration */
void get_into_degree(int n);
void toposort(int n);


/* functions definition */
// in degrees
void get_into_degree(int n){
	for(int j = 0; j < n; ++j){
		into[j] = 0;
		for(int i = 0; i < n; ++i)
			if(matrix[i][j] == 1)
				into[j]++;
	}
}


// calculate and output
void toposort(int n){
	//需要输出n个结点,排序结束
	for(int i = 1; i <= n; ++i){
	   int j = 0;
	   // find the vertex which indegree is 0
	   while(j < n && into[j] != 0) j++; 
	   // output the vertex
	   cout<<j + 1<<" ";

	   into[j] = N; // in order not to output the j again

	   // clear the Vertex's edge
	   for(int k = 0; k < n; ++k)
			if(matrix[j][k] == 1)
				into[k]--;
	}
}

int main(){
	get_into_degree(N);
	toposort(N);
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
拓扑排序是一种常用的有向无环图(DAG)的排序算法,用于解决有向图中的依赖关系问题。其基本思想是通过不断删除入度为0的顶点,直到所有顶点都被删除或者发现存在环,从而确定一个有向图的拓扑序列。 C++实现拓扑排序的基本思路是使用一个队列来存储当前入度为0的顶点,并通过邻接表来记录每个顶点的入度和出度。具体步骤如下: 1. 初始化:将所有入度为0的顶点加入队列中。 2. 循环:不断从队列中取出一个顶点,将其邻接点的入度减1,如果减1后入度为0,则将其加入队列中。 3. 直到队列为空:如果队列为空时还有顶点的入度不为0,则说明存在环;否则,按照队列中的顺序输出拓扑序列。 以下是C++代码实现: ```c++ #include <iostream> #include <vector> #include <queue> using namespace std; vector<int> topoSort(vector<vector<int>>& graph) { int n = graph.size(); vector<int> inDegree(n, 0); vector<int> res; queue<int> q; // 计算每个顶点的入度 for (int i = 0; i < n; i++) { for (auto j : graph[i]) { inDegree[j]++; } } // 将所有入度为0的顶点加入队列 for (int i = 0; i < n; i++) { if (inDegree[i] == 0) { q.push(i); } } // 循环取出队列中的顶点,并更新其邻接点的入度 while (!q.empty()) { int u = q.front(); q.pop(); res.push_back(u); for (auto v : graph[u]) { if (--inDegree[v] == 0) { q.push(v); } } } // 如果存在入度不为0的顶点,则说明存在环 if (res.size() != n) { res.clear(); } return res; } int main() { vector<vector<int>> graph = {{1, 2}, {2, 3}, {4}, {3, 4}}; vector<int> res = topoSort(graph); if (res.empty()) { cout << "存在环" << endl; } else { cout << "拓扑序列为:"; for (auto x : res) { cout << x << " "; } cout << endl; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信知阁

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值