当前位置:网站首页>图论:拓扑排序
图论:拓扑排序
2022-07-26 04:08:00 【*﹏ℳ๓无情*】
拓扑排序
对于有向无环图(DAG)的顶点进行的一种线性排序,排序序列中每个顶点都会且仅会出现一次,且对于所有的有向边u→v,排完序后u都在v前面。
在拓扑排序中,我们用L记录到目前为止拓扑序列,用集合S记录所有不在L中的入度为0的顶点;
首先遍历整张图上的顶点,如果一个顶点的入度为0,将它加入S;
当S不为空时:
在S中任取一个顶点x,将x加入到L的队尾,并把x从S中删去;
遍历从x出发的边x→y,把这条边删掉,如果y的入度变成了0,则将其加入到S中;
循环结束时,如果所有的点都加入了L,那么我们就找到了一个合法的拓扑序列,否则可以证明图中存在环;
队列L、S一般用同一个队列实现
时间复杂度: 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;
}
字典序最小/最大的拓扑排序
只需要将维护S的数据结构改成堆(优先队列、Set),然后每次从堆中选出最小/最大的元素x进行操作。
时间复杂度: O ( n l o n g n + m ) O(nlongn+m) O(nlongn+m)
字典序最小
priority_queue写法
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写法
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;
}
字典序最大
priority_queue写法
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写法
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;
}
边栏推荐
- STM32 state machine programming example - full automatic washing machine (Part 2)
- In PHP, you can use the abs() function to turn negative numbers into positive numbers
- Inventory the concept, classification and characteristics of cloud computing
- 2.9.4 Ext JS的布尔对象类型处理及便捷方法
- Working ideas of stability and high availability guarantee
- php 实现从1累加到100的算法
- 【云原生】谈谈老牌消息中间件ActiveMQ的理解
- 软考 系统架构设计师 简明教程 | 案例分析解题技巧
- This article takes you to graph transformers
- Write a paper for help, how to write the discussion part?
猜你喜欢

Find my technology | the Internet of things asset tracking market has reached US $6.6 billion, and find my helps the market develop

STM32 state machine programming example - full automatic washing machine (Part 2)

《opencv学习笔记》-- 重映射

基于移位寄存器的同步FIFO

php 查找 session 存储文件位置的方法
![[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

Supervit for deep learning

Verilog implementation of key dithering elimination

Booking.com binke Shanghai noodles
![Acwing game 61 [End]](/img/e3/191f7d888fc8cced608d41ef837a92.png)
Acwing game 61 [End]
随机推荐
AcWing. 102 best cattle fence
This article takes you to graph transformers
Pits encountered by sdl2 OpenGL
[digital ic/fpga] Hot unique code detection
Constructing verb sources for relation extraction
Opencv learning notes - remapping
PHP method to find the location of session storage file
CPU and GPU are out of date, and the era of NPU and APU begins
PHP installation spool supports dtls protocol
零售连锁门店收银系统源码管理商品分类的功能逻辑分享
How mantium uses deepspeed to implement low latency gpt-j reasoning on Amazon sagemaker
Acwing game 61 [End]
firewall 命令简单操作
Booking.com binke Shanghai noodles
MySQL index failure scenarios and Solutions
Asemi rectifier bridge gbu1510 parameters, gbu1510 specifications, gbu1510 package
荐书丨《教育心理学》:送给明日教师的一本书~
Pathmatchingresourcepatternresolver parsing configuration file resource file
《opencv学习笔记》-- 霍夫变换
[Reading Notes - > data analysis] 01 introduction to data analysis