当前位置:网站首页>Graph theory: topological sorting
Graph theory: topological sorting
2022-07-26 04:09:00 【*Ruthless*】
List of articles
A topological sort
For directed acyclic graphs (DAG) A linear sort of vertices of , Each vertex in the sort sequence will appear and only appear once , And for all directed edges u→v, After finishing the sequence u All in v front .
In topological ordering , We use it L Record the topological sequence so far , Using set S Record all absence L The penetration in is 0 The summit of ;
First, traverse the vertices of the whole graph , If the penetration of a vertex is 0, Add it to S;
When S Isn't empty :
stay S Take any vertex in x, take x Add to L At the end of the team , And put x from S Delete from ;
Ergodic x The departure side x→y, Delete this edge , If y It becomes 0, Then add it to S in ;
At the end of the loop , If all points are added L, Then we find a legal topological sequence , Otherwise, it can be proved that there are rings in the graph ;
queue L、S It's usually used The same queue Realization
Time complexity : O ( n + m ) O(n+m) O(n+m)
vector<int> edge[N];
int n, m, d[N];
queue<int> q;
inline bool TopoSort()
{
int tot = 0;
for (int i = 1; i <= n; i++)
if (!d[i])
q.push(i);
while (!q.empty())
{
int x = q.front();
++tot;
q.pop();
for (auto y : edge[x])
if (--d[y] == 0)
q.push(y);
}
if (tot == n)
return true;
else
return false;
}
Dictionary order is the smallest / The largest topological sort
Only need to maintain S The data structure of is changed into a heap ( Priority queue 、Set), Then select the smallest from the heap each time / Largest element x To operate .
Time complexity : O ( n l o n g n + m ) O(nlongn+m) O(nlongn+m)
Dictionary order is the smallest
priority_queue How to write it
vector<int> edge[N];
int n, m, d[N], l[N];
priority_queue<int, vector<int>, greater<int>> q;
inline bool TopoSort()
{
int tot = 0;
for (int i = 1; i <= n; i++)
if (!d[i])
q.push(i);
while (!q.empty())
{
int x = q.top();
q.pop();
l[++tot] = x;
for (auto y : edge[x])
if (--d[y] == 0)
q.push(y);
}
if (tot == n)
return true;
else
return false;
}
set How to write it
vector<int> edge[N];
int n, m, d[N], l[N];
set<int> q;
inline bool TopoSort()
{
int tot = 0;
for (int i = 1; i <= n; i++)
if (!d[i])
q.insert(i);
while (!q.empty())
{
int x = *q.begin();
q.erase(x);
l[++tot] = x;
for (auto y : edge[x])
if (--d[y] == 0)
q.insert(y);
}
if (tot == n)
return true;
else
return false;
}
Dictionary order maximum
priority_queue How to write it
vector<int> edge[N];
int n, m, d[N], l[N];
priority_queue<int> q;
inline bool TopoSort()
{
int tot = 0;
for (int i = 1; i <= n; i++)
if (!d[i])
q.push(i);
while (!q.empty())
{
int x = q.top();
q.pop();
l[++tot] = x;
for (auto y : edge[x])
if (--d[y] == 0)
q.push(y);
}
if (tot == n)
return true;
else
return false;
}
set How to write it
vector<int> edge[N];
int n, m, d[N], l[N];
set<int> q;
inline bool TopoSort()
{
int tot = 0;
for (int i = 1; i <= n; i++)
if (!d[i])
q.insert(i);
while (!q.empty())
{
int x = *q.rbegin(); // int x = *(--q.end());
q.erase(x);
l[++tot] = x;
for (auto y : edge[x])
if (--d[y] == 0)
q.insert(y);
}
if (tot == n)
return true;
else
return false;
}
边栏推荐
- In PHP, you can use the abs() function to turn negative numbers into positive numbers
- [深入研究4G/5G/6G专题-42]: URLLC-13-《3GPP URLLC相关协议、规范、技术原理深度解读》-7-低延时技术-1-子载波间隔扩展
- JS upload avatar (you can understand it after reading it, trust me)
- Share | 2022 big data white paper of digital security industry (PDF attached)
- Brief tutorial for soft exam system architecture designer | case analysis and problem solving skills
- What format should be adopted when the references are foreign documents?
- Use of rule engine drools
- (translation) the button position convention can strengthen the user's usage habits
- Implementation of distributed lock
- STM32 state machine programming example - full automatic washing machine (Part 2)
猜你喜欢

General test case writing specification

Dracoo Master天龙卡牌大师
![Acwing game 61 [End]](/img/e3/191f7d888fc8cced608d41ef837a92.png)
Acwing game 61 [End]

(translation) the button position convention can strengthen the user's usage habits

Connect external MySQL databases in istio Service Grid

华为高层谈 35 岁危机,程序员如何破年龄之忧?

零售连锁门店收银系统源码管理商品分类的功能逻辑分享

The era of smart clothes has come. Zhinar invites you to discuss swords in Yangcheng. Guangya Exhibition is waiting for you on August 4

Acwing第 61 场周赛【完结】
![[cloud native] talk about the understanding of the old message middleware ActiveMQ](/img/70/fe2ef0bea10d8275c0fe4c8139b027.png)
[cloud native] talk about the understanding of the old message middleware ActiveMQ
随机推荐
[binary tree] the longest interleaved path in a binary tree
《opencv学习笔记》-- 重映射
Worked overtime for a week to develop a reporting system. This low code free it reporting artifact is very easy to use
PHP method to find the location of session storage file
Opencv learning notes - edge detection and Canny operator, Sobel operator, lapiacian operator, ScHARR filter
The second article, which is still unfinished, will be introduced again, and continue to explain oracledb_ Exporter monitors Oracle, a very low intrusive monitoring scheme.
Acwing第 61 场周赛【完结】
Trust sums two numbers
零售连锁门店收银系统源码管理商品分类的功能逻辑分享
Use of rule engine drools
php 查找 session 存储文件位置的方法
Wechat applet to realize music player (4) (use pubsubjs to realize inter page communication)
HelloWorld case analysis
《opencv学习笔记》-- 霍夫变换
【SVN】一直出现 Please execute the ‘Cleanup‘ command,cleanup以后没有反应的解决办法
规则引擎Drools的使用
PathMatchingResourcePatternResolver解析配置文件 资源文件
【读书笔记->数据分析】BDA教材《数据分析》书籍介绍
operator new、operator delete补充讲义
在 Istio 服务网格内连接外部 MySQL 数据库