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

边栏推荐
- Basic use of sringcloud & use of component Nacos
- 异步、郵件、定時三大任務
- 指针初阶(基础)
- 【AutoSAR 十二 模式管理】
- [flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
- 详解RDD基本概念、RDD五大属性
- 全志A40i/T3如何通过SPI转CAN
- [overview of AUTOSAR four BSW]
- 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
- Every k nodes in the linked list are flipped
猜你喜欢

Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)

Initial order of pointer (basic)

leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】

基本远程连接工具Xshell

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

攻克哈希的基本概念与实现

Vulkan performance and refinement

深度剖析数据在内存中的存储
![[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions](/img/ea/ee1ad50a497478b9d080bb5e4bdfb5.png)
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions

Embrace the safety concept of platform delivery
随机推荐
Vulkan performance and refinement
[AUTOSAR eight OS]
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
Trois tâches principales: asynchrone, courrier et timing
R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
Merge K sorted linked lists
Leetcode-224: basic calculator
Embrace the safety concept of platform delivery
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
【AutoSAR 二 AppL概述】
基本远程连接工具Xshell
这不平凡的两年,感谢我们一直在一起!
How to systematically learn machine learning
R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and
[AUTOSAR 11 communication related mechanism]
[daily training] 871 Minimum refueling times
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
Test shift right: Elk practice of online quality monitoring
【AutoSAR 十 IO架构】
Leetcode-871: minimum refueling times