当前位置:网站首页>BFS - topology sort
BFS - topology sort
2022-07-03 17:45:00 【Want no boat at the bottom of the sea】
The topological sort is bfs A simple application of , The specific implementation ideas are as follows ;
First, the degree of entry is 0 Put the point in the queue , Then take out the counterpart , Update the penetration of other points with the corresponding head , If you find a new point, the penetration is 0, Then put the point in the queue , So circular , Until the queue is empty ;
it is to be noted that , A ring graph must have no topological ordering , So a simple application of topological sorting is to judge whether a graph is a ring graph .
This can be proved by contradiction ;
Suppose a ring graph has topological ordering ;
At some point in the team , remember A In ring Q The point is a queued point in this ring , It indicates that the penetration at this point is 0, Because Q Point is the first to join the team , That points to it in the ring P I'm not in the team ,P Instructions for not joining the team Q The penetration of the point >=1, Conflict with the conditions for joining the team and the first one to join the team ;
Therefore, there is no topological ordering in a ring graph ;
Next is the code and comments .

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 100010;
struct poi {
vector<int> ne;//ne Store the subscript of the next point in
int d = 0;// The penetration of the point
}pos[N];
int p[N], fur;// Topological sorting of storage points
int n, m;//n A little bit ,m side
bool bfs() {
queue<int> que;
for (int i = 1; i <= n; i++) {// Initialize queue , Set all entries to 0 Put the point in the queue
if (!pos[i].d)
que.push(i);
}
while (que.size()) {
int t = que.front();
p[fur++] = t;// Add the header to the topology sorting
que.pop();
for (int i = 0; i < pos[t].ne.size(); i++) {// Depth of update point
int m = pos[t].ne[i];
pos[m].d--;
if (!pos[m].d) {
que.push(m);
}
}
}
if (fur != n) return 0;//fur Is the number of points in the sort , If a little is not added to the sorting , It shows that there is a ring , That is, there is no topological ordering
else return 1;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {// Receiving diagram
int a, b; scanf("%d%d", &a, &b);
pos[a].ne.push_back(b); pos[b].d++;
}
if (bfs()) {
for (int i = 0; i < n; i++)
cout << p[i] << ' ';
}
else cout << -1;
}边栏推荐
- POM in idea XML graying solution
- Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
- Leetcode13. Roman numeral to integer (three solutions)
- Analyse ArrayList 3: suppression d'éléments
- TCP congestion control details | 3 design space
- [combinatorics] recursive equation (four cases where the non-homogeneous part of a linear non-homogeneous recursive equation with constant coefficients is the general solution of the combination of po
- SWM32系列教程4-端口映射及串口应用
- When absolutely positioned, the element is horizontally and vertically centered
- [RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
- 企业级自定义表单引擎解决方案(十一)--表单规则引擎1
猜你喜欢

Vs2013 has blocked the installer, and ie10 needs to be installed

面试官:值为 nil 为什么不等于 nil ?

Baiwen.com 7 days Internet of things smart home learning experience punch in the next day

聊聊支付流程的設計與實現邏輯

Test your trained model

Embedded-c language-7

UE4 official charging resources, with a total price of several thousand

How to install PHP on Ubuntu 20.04

Type conversion, variable

Implementation of Tetris in C language
随机推荐
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
Analyse ArrayList 3: suppression d'éléments
[combinatorics] recursive equation (solution of linear non-homogeneous recursive equation with constant coefficients | standard form and general solution of recursive equation | proof of general solut
PHP processing - watermark images (text, etc.)
How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?
Website with JS doesn't work in IE9 until the Developer Tools is activated
Leetcode Valentine's Day Special - looking for a single dog
How to purchase Google colab members in China
What is the difference between cloud server and cloud virtual machine
UE4 official charging resources, with a total price of several thousand
Create a new file from templates with bash script - create new file from templates with bash script
Test your trained model
PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
Leetcode540: a single element in an ordered array
Leetcode13. Roman numeral to integer (three solutions)
国内如何购买Google Colab会员
【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
Luogu: p2685 [tjoi2012] Bridge
ArrayList分析3 : 删除元素
[combinatorics] recursive equation (special solution form | special solution solving method | special solution example)