当前位置:网站首页>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 .

边栏推荐
- 1038 Recover the Smallest Number
- RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
- 1.12 - Instructions
- 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
- leetcode-871:最低加油次数
- 异步、邮件、定时三大任务
- R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi
- Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
- Leetcode-871: minimum refueling times
- (C语言)数据的存储
猜你喜欢

这不平凡的两年,感谢我们一直在一起!
![[AUTOSAR + IO Architecture]](/img/cf/9ea42b50bed298c0546764b63bd957.png)
[AUTOSAR + IO Architecture]

leetcode-2280:表示一个折线图的最少线段数

How to systematically learn machine learning

Initial order of pointer (basic)

Explain the basic concepts and five attributes of RDD in detail

First hand evaluation of Reza electronics rz/g2l development board
![[AUTOSAR VI description document]](/img/3d/1382acbc4054ab218485a12b7b4e6b.png)
[AUTOSAR VI description document]

【AutoSAR 一 概述】

【案例分享】让新时代教育发展与“数”俱进
随机推荐
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
Solve the cache problem of reactnative using WebView
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
【AutoSAR 一 概述】
Usage of using clause in kingbases alter table
[daily training] 871 Minimum refueling times
Delete duplicate elements in the ordered linked list -ii
【爱死机】《吉巴罗》被忽略的细节
攻克哈希的基本概念与实现
[introduction to AUTOSAR seven tool chain]
465. 最优账单平衡 DFS 回溯
电话网络问题
Explain the basic concepts and five attributes of RDD in detail
excel去除小数点后面的数据,将数字取整
Key detection and sinusoidal signal output developed by Arduino
leetcode:701. Insertion in binary search tree [BST insertion]
比较版本号
关于Fibonacci数列
瑞萨RZ/G2L ARM开发板存储读写速度与网络实测
What is needed to develop a domestic arm intelligent edge computing gateway