当前位置:网站首页>[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;
}
边栏推荐
- Redis introduction complete tutorial: slow query analysis
- [try to hack] wide byte injection
- Redis入门完整教程:Bitmaps
- Redis入门完整教程:有序集合详解
- Redis: redis configuration file related configuration and redis persistence
- Common methods in string class
- 9 - 类
- sobel过滤器
- Attack and defense world misc advanced grace-50
- Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
猜你喜欢

集群的概述与定义,一看就会

攻防世界 MISC 高手进阶区 001 normal_png

位运算符讲解

Redis入门完整教程:事务与Lua

浅聊一下中间件

Redis introduction complete tutorial: slow query analysis

PMO: compare the sample efficiency of 25 molecular optimization methods

Redis入门完整教程:键管理
![P2181 diagonal and p1030 [noip2001 popularization group] arrange in order](/img/79/36c46421bce08284838f68f11cda29.png)
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order

Logo special training camp section II collocation relationship between words and graphics
随机推荐
Redis入门完整教程:GEO
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
Install the gold warehouse database of NPC
Redis入门完整教程:哈希说明
SQL中MAX与GREATEST的区别
Common methods in string class
[try to hack] wide byte injection
SHP data making 3dfiles white film
mamp下缺少pcntl扩展的解决办法,Fatal error: Call to undefined function pcntl_signal()
Redis getting started complete tutorial: publish and subscribe
MySQL Architecture - logical architecture
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
The overview and definition of clusters can be seen at a glance
Redis getting started complete tutorial: Geo
页面关闭前,如何发送一个可靠请求
Redis入门完整教程:客户端通信协议
Breakpoint debugging under vs2019 c release