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

边栏推荐
- 电话网络问题
- How to systematically learn machine learning
- matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
- Deep analysis of data storage in memory
- Trois tâches principales: asynchrone, courrier et timing
- 链表内指定区间反转
- 瑞萨RZ/G2L ARM开发板存储读写速度与网络实测
- Assets, vulnerabilities, threats and events of the four elements of safe operation
- 无向图的割点
- Vulkan practice first bullet
猜你喜欢

The difference between tail -f, tail -f and tail

详解RDD基本概念、RDD五大属性

leetcode:701. 二叉搜索树中的插入操作【bst的插入】

数据分析思维分析犯法和业务知识——分析方法(一)

测试右移:线上质量监控 ELK 实战

Trois tâches principales: asynchrone, courrier et timing

FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟

Linear programming of mathematical modeling (including Matlab code)

【AutoSAR 一 概述】

Leetcode-849: maximum distance to the nearest person
随机推荐
机器学习术语
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
Meaning of Tencent cloud free SSL certificate extension file
[love crash] neglected details of gibaro
12_微信小程序之微信视频号滚动自动播放视频效果实现
1038 Recover the Smallest Number
鏈錶內指定區間反轉
【爱死机】《吉巴罗》被忽略的细节
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
First hand evaluation of Reza electronics rz/g2l development board
[daily training] 871 Minimum refueling times
Leetcode-224: basic calculator
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
异步、邮件、定时三大任务
Basic use of sringcloud & use of component Nacos
【AutoSAR 三 RTE概述】
File operation io-part2
The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
matlab 多普勒效应产生振动信号和处理