当前位置:网站首页>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;
}
边栏推荐
- APISIX 在 API 和微服务领域的探索
- Connect external MySQL databases in istio Service Grid
- (翻译)按钮位置约定能强化用户使用习惯
- How does redis implement persistence? Explain the AOF trigger mechanism and its advantages and disadvantages in detail, and take you to quickly master AOF
- Working ideas of stability and high availability guarantee
- [Reading Notes - > data analysis] Introduction to BDA textbook data analysis
- [cloud native kubernetes] how to use configmap under kubernetes cluster
- [SVN] please execute the 'cleanup' command appears all the time. The solution is that there is no response after cleanup
- ZK snark: about private key, ring signature, zkksp
- PathMatchingResourcePatternResolver解析配置文件 资源文件
猜你喜欢

Connect external MySQL databases in istio Service Grid
![Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130](/img/84/e5cb5199fe4602440b50dfc4afe963.gif)
Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130

Retail chain store cashier system source code management commodity classification function logic sharing

redux

This article takes you to graph transformers

APISIX 在 API 和微服务领域的探索

微信小程序实现音乐播放器(4)(使用pubsubjs实现页面间通信)

Pat class a 1039 course list for student

1311_硬件设计_ICT概念、应用以及优缺点学习小结

华为再发“天才少年”全球召集令,有人曾放弃360万年薪加入
随机推荐
How engineers treat open source -- the heartfelt words of an old engineer
荐书|《DBT技巧训练手册》:宝贝,你就是你活着的原因
Seat / safety configuration upgrade is the administrative experience of the new Volvo S90 in place
微信小程序实现音乐播放器(4)(使用pubsubjs实现页面间通信)
PHP method to find the location of session storage file
Tactile intelligent sharing-rk3568 application in scenic spot navigation robot
Summary of senior report development experience: understand this and do not make bad reports
2.9.4 Ext JS的布尔对象类型处理及便捷方法
VM虚拟机 没有未桥接的主机网络适配器 无法还原默认配置
PathMatchingResourcePatternResolver解析配置文件 资源文件
Opencv learning notes - remapping
Firewall command simple operation
The PHP Eval () function can run a string as PHP code
[Reading Notes - > data analysis] Introduction to BDA textbook data analysis
Chapter 18: explore the wonders of the mean in the 2-bit a~b system, specify the 3x+1 conversion process of integers, specify an interval to verify the angular Valley conjecture, explore the number of
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.
[SVN] please execute the 'cleanup' command appears all the time. The solution is that there is no response after cleanup
redux
Wechat applet to realize music player (4) (use pubsubjs to realize inter page communication)
2022 Hangzhou Electric Multi school bowcraft