当前位置:网站首页>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 .
边栏推荐
- 信息熵的基础
- 异步、邮件、定时三大任务
- Illustrated network: what is virtual router redundancy protocol VRRP?
- Every k nodes in the linked list are flipped
- The difference between relational database and non relational database
- First hand evaluation of Reza electronics rz/g2l development board
- 【爱死机】《吉巴罗》被忽略的细节
- Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
- excel去除小数点后面的数据,将数字取整
- Leetcode-2280: represents the minimum number of line segments of a line graph
猜你喜欢
数学建模之线性规划(含MATLAB代码)
全志A40i/T3如何通过SPI转CAN
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
tail -f 、tail -F、tailf的区别
excel IF公式判断两列是否相同
What is needed to develop a domestic arm intelligent edge computing gateway
异步、邮件、定时三大任务
Vulkan performance and refinement
[overview of AUTOSAR three RTE]
信息熵的基础
随机推荐
深度剖析数据在内存中的存储
RK3568开发板评测篇(二):开发环境搭建
12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
数组与集合性能比较
[AUTOSAR + IO Architecture]
Lex & yacc & bison & flex configuration problems
tail -f 、tail -F、tailf的区别
On Fibonacci sequence
1038 Recover the Smallest Number
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
Trois tâches principales: asynchrone, courrier et timing
Embrace the safety concept of platform delivery
基本远程连接工具Xshell
1.11 - bus
信息熵的基础
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
[AUTOSAR five methodology]
瑞萨RZ/G2L ARM开发板存储读写速度与网络实测