当前位置:网站首页>Strongly connected components of digraph
Strongly connected components of digraph
2022-07-03 01:07:00 【chengqiuming】
One Algorithm steps
1 Depth first traversal node , On the first visit to the node x when , take x Push , And dfn[x]=low[x]=++num
2 Traverse x All the adjacency points of y.
if y Not visited , Then recursively access y, Update on return low[x]=min(low[x],low[y])
if y Has been accessed and is on the stack , Then order low[x]=min(low[x],dfn[y])
3 stay x Before backtracking , If you judge low[x]=dfn[x], Then the nodes are constantly popped out of the stack , until x Stop when leaving the stack . The pop-up node is a connected component .
Two give an example
1 From the node 1 Set out for depth first search ,dfn[1]=low[1]=1,1 Push ;
2 Traverse 1 All the adjacency points of
2 Not interviewed , Recursive access 2,2 Push , return , to update dfn[2]=low[2]=2.
3 In retrospect , because dfn[2]=low[2],2 Out of the stack , Get strongly connected components 2.

4 Go back to 1 after , Continue to visit 1 Next adjacent point of 3, Then visit 3-4-5,5 The adjacency point 1 Of has been visited , And 1 In the stack , to update low[5]=min(low[5],dfn[1])=1, Update on Backtracking low[4]=min(low[4],low[5])=1, low[3]=min(low[3],low[4])=1,low[1]=min(low[1],low[3])=1. node 1 All adjacency points of have been visited , because dfn[1]=low[1], So it began to come out of the stack , Until I met 1, Get strongly connected components 5 4 2 1.

3、 ... and Code
package graph.tarjanscc;
import java.util.Scanner;
import java.util.Stack;
public class TarjanSCC {
static final int maxn = 1000 + 5;
static int n;
static int m;
static int head[];
static int cnt;
static int root;
static int low[];
static int dfn[];
static int num;
static Edge e[] = new Edge[maxn << 1];
static Stack<Integer> s = new Stack<>();
static boolean ins[] = new boolean[maxn];
static {
for (int i = 0; i < e.length; i++) {
e[i] = new Edge();
}
}
static void add(int u, int v) { // Add an edge u--v
e[++cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
}
static void tarjan(int u) { // Find the strongly connected component
low[u] = dfn[u] = ++num;
System.out.println("low[" + "]=" + low[u] + "\tdfn[" + u + "]=" + dfn[u]);
ins[u] = true;
s.push(u);
for (int i = head[u]; i != 0; i = e[i].next) {
int v = e[i].to;
if (dfn[v] == 0) {
tarjan(v);
low[u] = Math.min(low[u], low[v]);
System.out.println("update1:low[" + u + "]=" + low[u]);
} else if (ins[v]) {
low[u] = Math.min(low[u], dfn[v]);
System.out.println("update2:low[" + u + "]=" + low[u]);
}
}
if (low[u] == dfn[u]) {
int v;
System.out.println(" Strong connected components :");
do {
v = s.peek();
s.pop();
System.out.print(v + " ");
ins[v] = false;
} while (v != u);
System.out.println();
}
}
static void init() {
head = new int[maxn];
low = new int[maxn];
dfn = new int[maxn];
ins = new boolean[maxn];
cnt = num = 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
m = scanner.nextInt();
init();
int u, v;
while (m-- > 0) {
u = scanner.nextInt();
v = scanner.nextInt();
add(u, v);
}
for (int i = 1; i <= n; i++)
if (dfn[i] == 0) {
tarjan(i);
}
}
}
class Edge {
int to;
int next;
}Four test
Green is the input , White is the output .

边栏推荐
- Lex & yacc & bison & flex configuration problems
- Basic use of sringcloud & use of component Nacos
- Embrace the safety concept of platform delivery
- File operation io-part2
- [AUTOSAR twelve mode management]
- Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
- Infrared thermography temperature detection system based on arm rk3568
- How to convert Quanzhi a40i/t3 to can through SPI
- 2022.2.14 resumption
- FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
猜你喜欢
随机推荐
[AUTOSAR + IO Architecture]
[AUTOSAR XIII NVM]
异步、邮件、定时三大任务
Initial order of pointer (basic)
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
Specified interval inversion in the linked list
数学建模之线性规划(含MATLAB代码)
删除有序链表中重复的元素-II
[overview of AUTOSAR four BSW]
Leetcode-849: maximum distance to the nearest person
465. 最优账单平衡 DFS 回溯
安全运营四要素之资产、脆弱性、威胁和事件
Advanced pointer (I)
How to systematically learn machine learning
Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
【AutoSAR 四 BSW概述】
What is needed to develop a domestic arm intelligent edge computing gateway
解决ReactNative使用webView存在缓存问题
【C语言】分支和循环语句(上)









