当前位置:网站首页>Strongly connected component
Strongly connected component
2022-07-24 14:34:00 【beyond+myself】
Connected component : Any two points can reach each other .( notes : Any point itself is strongly connected )
Strong connected components : Maximal connected component , After adding any point, it is not a connected component
Directed acyclic graph (DAG): That is, topology .
dfs order : according to dfs Number each point in the order of
Beside the branches :(x,y),x yes y Parent node
Forward edge :(x,y),x yes y The ancestor node of
Back to the side :(x,y),y yes x The ancestor node of
Cross edge :(x,y), One of them has been searched
It's usually on the left , The one on the right is not called cross edge
Judge whether a point is in the strongly connected component :
①: Is it possible to return to the ancestor node , That is, there is a backward edge pointing to the ancestor node
②: Go to a horizontal fork first , Then go to the ancestor node
Tarjan Algorithm for strong connected components :
Time stamp : adopt dfs Set a number for each point .
Define two timestamps for each point :
dfn[u] Means to traverse to u Time for
low[u] From u Start walking , The smallest timestamp that can be traversed .
If u Is the highest point of the strongly connected component , be dfn[u]=low[u]
tarjan Algorithm : encounter dfn[u]==low[u] Is the strongly connected component , This means that when the smallest point he can reach is itself, it is a strongly connected component . Because strongly connected components , It must be that every two points can reach each other . So it is bound to form a ring , Because we are dfs It must be from top to bottom, that is, the timestamp is also from small to large , So we think the smallest point on this ring is the key point . Because in the specific traversal process, it must be after each point traversal is completed to judge whether it conforms to strongly connected variables , When judging in this way, the variable that cannot be reached has been out of the stack , What can be reached is still in the stack , And is the largest ( This point has been traversed ). In this way, all points including itself are put out of the stack , Form a strongly connected component .
Shrinkage point : Because the connected components can reach each other , That is, after reaching this connected component through the outer point, you can reach any point in the connected component , Therefore, this connected component can be reduced to a point , After shrinking the point, the whole graph will be called a directed acyclic graph .
tarjan Algorithm code template :
void tarjan(int u)
{
dfn[u]=low[u]=++timestamp;
stk[++top]=u,in_stk[u]=true;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(!dfn[j])
{
tarjan(j);
low[u]=min(low[u],low[j]);
}
else if(in_stk[j]) low[u]=min(low[u],dfn[j]);
}
if(dfn[u]==low[u])
{
++scc_cnt;// The number of connected components plus 1
int y;
do
{
y=stk[top--];
in_stk[y]=false;
id[y]=scc_cnt;//y In this connected component
Size[scc_cnt]++;
}while(y!=u);
}
}
Shrink point template :
for(int i=1;i<=n;i++)
{
for(int j=h[i];~j;j=ne[j])
{
int k=e[j];
int a=id[i],b=id[k];
if(a!=b) dout[a]++;//a The output plus 1, That is, he came here connected , But after finding the connected component, it is not connected , They should be connected
}
}
Topic link
kosaraju Algorithm ( two dfs): Positive direction can reach , What can also be reached in the reverse direction is the strongly connected component , Here is the first time dfs The order of stacking is in the order of traversal , So the first traversal must be the last stack , So after the node enters the stack completely , From the top of the stack to the bottom of the stack is actually in accordance with dfs Sequential execution .
Algorithm template :
void dfs1(int u)
{
st[u] = true;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (!st[j])
dfs1(j);
}
stk[ ++ top] = u;
}
void dfs2(int u)
{
id[u] = scc_cnt;
sz[scc_cnt] ++ ;
st[u] = true;
for (int i = hs[u]; ~i; i = ne[i])
{
int j = e[i];
if (!st[j])
dfs2(j);
}
}
Here is an example :
Topic link
边栏推荐
- Automated penetration scanning tool
- LeetCode·每日一题·1184.公交站间的距离·模拟
- Concurrent programming ----------- set
- Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
- 北京一卡通以35288.8529万元挂牌出让68.45%股权,溢价率为84%
- Kotlin class and inheritance
- Not configured in app.json (uni releases wechat applet)
- Introduction to Xiaoxiong school
- Solve the problem that uni starter can log in to wechat with local functions, but fails to log in with cloud functions
- Complete set of JS array common operations
猜你喜欢

不要灰心,大名鼎鼎的YOLO、PageRank影响力爆棚的研究,曾被CS顶会拒稿

Mini examination - examination system

Bibliometrix: dig out the one worth reading from thousands of papers!
![[NLP] next stop, embossed AI](/img/fc/4997309d0d53c5b6eb441ac39e6929.jpg)
[NLP] next stop, embossed AI
![[C language note sharing] - dynamic memory management malloc, free, calloc, realloc, flexible array](/img/3f/35c9ff3be5c0ef781ffcb537287a20.png)
[C language note sharing] - dynamic memory management malloc, free, calloc, realloc, flexible array

Decrypt "sea Lotus" organization (domain control detection and defense)

Leetcode · daily question · 1184. distance between bus stops · simulation

【NLP】下一站,Embodied AI

Beijing all in one card listed and sold 68.45% of its equity at 352.888529 million yuan, with a premium rate of 84%

Isprs2018/ cloud detection: cloud/shadow detection based on spectral indexes for multi/hyp multi / hyperspectral optical remote sensing imager cloud / shadow detection
随机推荐
Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release
[C language note sharing] - dynamic memory management malloc, free, calloc, realloc, flexible array
C language -- program environment and preprocessing
Native asynchronous network communication executes faster than synchronous instructions
Data analysis and mining 2
Introduction to Xiaoxiong school
本机异步网络通信执行快于同步指令
不要灰心,大名鼎鼎的YOLO、PageRank影响力爆棚的研究,曾被CS顶会拒稿
Number of bytes occupied by variables of type char short int in memory
sql server语法—创建数据库
Class loading mechanism and parental delegation mechanism
The sliding window of Li Kou "step by step" (209. The smallest sub array, 904. Fruit baskets)
MySQL community download address
对话框管理器第二章:创建框架窗口
Beijing all in one card listed and sold 68.45% of its equity at 352.888529 million yuan, with a premium rate of 84%
IEEE Transaction期刊模板使用注意事项
达梦实时主备集群搭建
OC sets the image fillet, and the image is not deformed
CSDN garbage has no bottom line!
多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题