当前位置:网站首页>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;
}
边栏推荐
- CPU and GPU are out of date, and the era of NPU and APU begins
- LeetCode. 6115 count the number of ideal arrays
- Implementation of distributed lock
- 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.
- [oi knowledge] dichotomy, dichotomy concept, integer dichotomy, floating point dichotomy
- PHP 对象转换数组
- PHP installation spool supports dtls protocol
- What format should be adopted when the references are foreign documents?
- 规则引擎Drools的使用
- STM32 state machine programming example - full automatic washing machine (Part 2)
猜你喜欢

Inventory the concept, classification and characteristics of cloud computing

How does redis implement persistence? Explain the AOF trigger mechanism and its advantages and disadvantages in detail, and take you to quickly master AOF

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

redux

VM虚拟机 没有未桥接的主机网络适配器 无法还原默认配置
![[SVN] please execute the 'cleanup' command appears all the time. The solution is that there is no response after cleanup](/img/44/69c434fc9564078e11735881d9bf34.png)
[SVN] please execute the 'cleanup' command appears all the time. The solution is that there is no response after cleanup

如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?

Introduction to UFS CLK gate

加班一周开发了报表系统,这个低代码免费IT报表神器太好用了

Worked overtime for a week to develop a reporting system. This low code free it reporting artifact is very easy to use
随机推荐
Web测试方法大全
Dracoo master
Where does international crude oil open an account, a formal, safe and secure platform
Sentinel fusing and current limiting
软模拟光栅化渲染器
[question 019: what is the understanding of spherecastcommand in unity?]
Find my technology | the Internet of things asset tracking market has reached US $6.6 billion, and find my helps the market develop
荐书丨《教育心理学》:送给明日教师的一本书~
(translation) timing of website flow chart and user flow chart
测试用例设计方法之——招式组合,因果判定
[Reading Notes - > data analysis] 01 introduction to data analysis
How engineers treat open source -- the heartfelt words of an old engineer
I.MX6U-ALPHA开发板(GPIO中断实验)
Pits encountered by sdl2 OpenGL
Basic principles of iptables
Mantium 如何在 Amazon SageMaker 上使用 DeepSpeed 实现低延迟 GPT-J 推理
Method of test case design: introduction, trial recruit, preliminary exploration of equivalence boundary
【SVN】一直出现 Please execute the ‘Cleanup‘ command,cleanup以后没有反应的解决办法
Opencv learning notes - edge detection and Canny operator, Sobel operator, lapiacian operator, ScHARR filter
General test case writing specification