拓扑排序

本文介绍了拓扑排序的基本原理和作用,主要用于判断有向无环图(DAG)中节点的顺序。拓扑排序可以检测图中是否存在环,并输出排序序列。当有环存在时,拓扑排序无法完成。文章通过实例展示了如何利用栈和优先队列实现拓扑排序,并给出了两个具体的应用问题:确定比赛名次和判断非法关系。对于复杂关系的判断,拓扑排序能有效找出合法的顺序。代码示例使用C++实现,演示了如何利用栈和优先队列进行排序。
摘要由CSDN通过智能技术生成

拓扑排序

原理

我就不说特别详细了,因为算法笔记里面都有,大概就是一个有向无环图中寻找谁是最先的爸爸的问题。

作用

判别有无环,然后输出序列。

有无环判别

有环的情况是 n u m num num(推入队列中的顶点数)<定点数n.

理解:死锁

假设1->2->3->1,形成了一个环,抛开其他顶点,假如1要入队列,那么3必须入队列,那么2必须入队列,即都入不了队列,所以
n u m < n num<n num<n

关于优先队列的使用

到时候有空在单独总结一下,这里基本的列一下。

  1. 基本数据类型int,double,char 等

p r i o r i t y q u e u e < i n t > q u e ; / / 从 大 到 小 p r i o r i t y q u e u e < i n t , v e c t o r < i n t > , g r e a t e r < i n t > > q u e ; / / 从 小 打 大 priority_queue<int> que;//从大到小\\ priority_queue<int,vector<int>,greater<int> > que;//从小打大 priorityqueue<int>que;//priorityqueue<int,vector<int>,greater<int>>que;//

  1. 结构体优先级设置方法

不仅有表示位置的index,还有表示点权的num。重构<符号。

struct node{
	int index;
	int num;
	friend operator<(node a, node b){
        return a.num>b.num;//return a.num<b.num;
    }
}

这里要记住的是:

<是大的是优先级高,即从大到小…>是小的优先级高,即从小到大。

  • 很容易混淆的是sort中的 c m p cmp cmp是与其相反的。

c m p cmp cmp中<是从小到大排序,>是从大到小排序,反正两者是要混着记忆就可以,记一个然后另外一个相反即可。

------------------------------------------

Problem A: 算法7-12:有向无环图的拓扑排序

题解

简单的拓扑排序,文章特别要求是用stack来存储,而不是queue来存储。

代码
#include<bits/stdc++.h>
using namespace std;
vector<int> vec;
int m[51][51],n,degree[51];
int Sort(){
	stack<int> que;
	int num=0;
	for(int i=0;i<n;++i){
		if(degree[i]==0){
			que.push(i);
		} 
	}
	while(!que.empty()){
		int u=que.top();
		vec.push_back(u);
		++num;
		que.pop();
		for(int v=0;v<n;++v){
			if(m[u][v]==1){
				--degree[v];	
				if(degree[v]==0) que.push(v);
			}
		}
	}
	return num;
}
int main(){
	cin>>n;
	for(int i=0;i<n;++i) for(int j=0;j<n;++j){
		cin>>m[i][j];
		if(m[i][j]==1) degree[j]+=1;
	}
	if(Sort()==n){
		int flag=0;
		for(int i=0;i<vec.size();++i){
			if(flag) cout<<' ';
			cout<<vec[i];
			flag=1;
		}
		cout<<endl;
	}
	else cout<<"ERROR\n";
}

确定比赛名次

Description

有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。

Input

输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。

Output

给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。

其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。

Sample Input [Copy](javascript:CopyToClipboard($(’#sampleinput’).text()))
3 2
3 1
3 2
17 16
16 1
13 2
7 3
12 4
12 5
17 6
10 7
11 8
11 9
16 10
13 11
15 12
15 13
17 14
17 15
17 16
0 0
Sample Output [Copy](javascript:CopyToClipboard($(’#sampleoutput’).text()))
3 1 2
17 6 14 15 12 4 5 13 2 11 8 9 16 1 10 7 3
题解

普通的拓扑排序,用到了 p r i o r i t y < i n t , v e c t o r < i n t > , g r e a t e r < i n t > > priority<int,vector<int>,greater<int> > priority<int,vector<int>,greater<int>>,来使得编号小的靠前。

代码
#include<bits/stdc++.h>
#include<queue>
#include<vector>
using namespace std;
vector<int> matrix[501];
int degree[501];
int n,m,a,b;
void Sort(){
	priority_queue<int,vector<int> ,greater<int> > que;
	for(int i=1;i<=n;++i){
		if(degree[i]==0) que.push(i);
	}
	int flag=0;
	while(!que.empty()){
		if(flag==1) cout<<' ';
		int u=que.top();
		cout<<u;
		flag=1;
		que.pop();
		for(int v=0;v<matrix[u].size();++v){
			if((--degree[matrix[u][v]])==0) que.push(matrix[u][v]);
		}
	}
	cout<<endl;
}
int main(){
	cin>>n>>m;
	while(n!=0){
		for(int i=1;i<=n;++i) matrix[i].clear(),degree[i]=0;
		while(m--){
			cin>>a>>b;
			matrix[a].push_back(b);
			degree[b]+=1;
		}
		Sort();
		cin>>n>>m;
	}
}

Problem C: Legal or Not

Description

ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many “holy cows” like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost “master”, and Lost will have a nice “prentice”. By and by, there are many pairs of “master and prentice”. But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?We all know a master can have many prentices and a prentice may have a lot of masters too, it’s legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian’s master and, at the same time, 3xian is HH’s master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. Please note that the “master and prentice” relation is transitive. It means that if A is B’s master ans B is C’s master, then A is C’s master.

Input

The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y’s master and y is x’s prentice. The input is terminated by N = 0.TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,…, N-1). We use their numbers instead of their names.

Output

For each test case, print in one line the judgement of the messy relationship.If it is legal, output “YES”, otherwise “NO”.

Sample Input [Copy](javascript:CopyToClipboard($(’#sampleinput’).text()))
4 3
0 1
1 2
2 3
3 3
0 1
1 2
2 0
0 1
Sample Output [Copy](javascript:CopyToClipboard($(’#sampleoutput’).text()))
YES
NO
题解

简单的拓扑排序,和前面的套路,模板都是一样的,甚至更加简单

代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值