当前位置:网站首页>[graph theory] topological sorting
[graph theory] topological sorting
2022-07-04 22:58:00 【Which bug are you?】
List of articles
The concept of topological sorting will not be repeated .
The method of topological sorting
These structures are mainly maintained in the diagram describing topological sorting , The adjacency table is responsible for describing the relationship between points and edges .
Topological sorting operation 
Code implementation
A topological sort (Topological Sorting)_ Shenyi's blog -CSDN Blog _ A topological sort
I followed his introduction to the code
subject
2367 – Genealogical tree (poj.org)
The template questions , Build a graph , Then sort the output .
oj I won't support it C++11, You have to auto Replace with iterators …
#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;
#define N 1000
queue<int>output;
class Graph
{
public:
Graph(int v)// Initialize the number of vertices ,new Adjacency list , Initialize the in degree set to 0
{
_v = v;
_adj = new list<int>[v];//new One has v Adjacency table of vertices
_indegree.resize(v, 0);// Initialize the in degree set
}
~Graph()
{
delete[] _adj;
}
void addEdge(int val, int w)// The value is val Point of w
{
_adj[val].push_back(w);
_indegree[w]++;
}
bool topological_sort()
{
for (int i=1;i<_indegree.size();i++)// In fact, there is a layer of mapping at the join node
{
if (!_indegree[i])
{
_q.push(i);
}
}
int count = 0;
while (!_q.empty())
{
int v = _q.front();
_q.pop();
output.push(v);
count++;
// Update the in degree set , Set your entry as 0 The point of joining the team
list<int>::iterator it = _adj[v].begin();
for (;it!=_adj[v].end();it++)
{
_indegree[*it]--;
if (!_indegree[*it])
{
_q.push(*it);
}
}
}
if (count<_v)// Graph with loop
{
return false;
}
else
{
return true;
}
}
private:
int _v;// Number of vertices
list<int>* _adj;// Adjacency list
queue<int>_q;// The maintenance degree is 0 The set of vertices of
vector<int> _indegree;// Record the depth of each vertex
};
int main()
{
int n;
cin >> n;
Graph g(n+1);
for (int i = 1; i <= n; i++)
{
int x;
while (cin >> x)
{
if (x == 0)
{
break;
}
g.addEdge(i, x);
}
}
g.topological_sort();
bool flag = 1;
while (!output.empty())
{
int x = output.front();
output.pop();
if (flag)
{
cout << x;
flag = 0;
}
else
{
cout << " " << x;
}
}
return 0;
}
边栏推荐
- 集群的概述与定义,一看就会
- Sword finger offer 67 Convert a string to an integer
- PMO: compare the sample efficiency of 25 molecular optimization methods
- Sword finger offer 68 - I. nearest common ancestor of binary search tree
- MySQL Architecture - logical architecture
- Redis入门完整教程:初识Redis
- Duplicate ADMAS part name
- NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
- 环境加密技术解析
- sobel过滤器
猜你喜欢

SPSS installation and activation tutorial (including network disk link)
![P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列](/img/79/36c46421bce08284838f68f11cda29.png)
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列

Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)

Logo special training camp section II collocation relationship between words and graphics

Detailed explanation of heap sort code

攻防世界 MISC 进阶区 hit-the-core

Three stage operations in the attack and defense drill of the blue team

Tla+ introductory tutorial (1): introduction to formal methods

Redis getting started complete tutorial: Geo

Redis入门完整教程:有序集合详解
随机推荐
Redis introduction complete tutorial: client communication protocol
Sword finger offer 68 - ii The nearest common ancestor of binary tree
The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
攻防世界 MISC 进阶区 can_has_stdio?
【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
页面关闭前,如何发送一个可靠请求
Redis入门完整教程:HyperLogLog
leetcode 72. Edit distance edit distance (medium)
Redis démarrer le tutoriel complet: Pipeline
The overview and definition of clusters can be seen at a glance
MySQL Architecture - logical architecture
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
剑指Offer 68 - II. 二叉树的最近公共祖先
Breakpoint debugging under vs2019 c release
【剑指Offer】6-10题
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
Persistence mechanism of redis
模拟摇杆控制舵机
集群的概述与定义,一看就会
Redis入门完整教程:键管理