Tarjan Algorithm for extremely large connected components

Source: Internet
Author: User

Tarjan Algorithm for Finding extremely large strongly connected components

First, in directed graph G, if two vertices vi and vj (VI <> VJ) both have a directed path from VI to vj, at the same time, there is a directed path from VJ to VI, which is called two vertices strongly connected ). If each vertex of directed graph G is strongly connected, G is a strongly connected graph. A very large strongly connected subgraph of A digraph in a non-strongly connected graph. It is called a strongly connected component. If a strongly connected component can no longer be added to any vertex, the strongly connected component is a very large strongly connected component.

From the definition above, we can know that each point in the strongly connected component can reach each other, then we can find the "Root Node" of each extremely large force-connected component ", then, we can use the method of pushing up the underlying node to find a very large strongly connected component, so the key is to find the root node. So here we add two arrays, dfn, and low, where dfn is the search serial number, it indicates the number of nodes found at this point, while low indicates the node that can return up to the farthest. Obviously, when dfn [I] = low [I] of a node, it is the root of this extremely strong connected component, and the points found later are contained in this extremely strong connected component. Next we need to consider how to retrieve this extremely strong connected component, so we maintain a stack. Every time we search for a node, we press it into the stack. When we find the root point, after it is written to the stack, it represents the nodes in the extremely strong connected component. You only need to roll up the stack until the root node is out of the stack.

[Program pseudocode]

DFS (k)

INC (time); // The serial number variable for search

Low [x] = Time // The timestamp of the point at which the current node can return as far as possible

Dfn [x] = Time // Timestamp

Push (x) // inbound Stack

Instack [x] = true // mark the x node as true in the stack

For I = all sons of X

If you have not accessed x // you have not searched

DFS (I)

Low [x] = min (low [X], low [I])

// After searching, it is found that the earliest timestamp that the son node can reach is earlier than that of X.

Else // already searched

If (I in stack) and (dfn [I] <low [x])

// Indicates that the stack belongs to the same strongly connected component. If the timestamp is earlier, it must be updated.

Low [x] = dfn [I]

If dfn [x] = low [x] // If the node is a strongly connected Root Node

INC (Num) // increase in the number of strong connections

While true do

Pop // pop up the strongly connected Node

Instack [stack [Top] = false // mark as false in the stack

If stack [top + 1] = x // if the root node has been output

Break // jump out

View code

 1 program targan(input,output);
2 type
3 node = ^link;
4 link = record
5 goal : longint;
6 next : node;
7 end;
8 var
9 stack : array[1..1000] of longint;
10 l : array[1..1000] of node;
11 instack : array[1..1000] of boolean;
12 dfn : array[1..1000] of longint;
13 low : array[1..1000] of longint;
14 ans : array[1..1000] of longint;
15 num,e : longint;
16 m,n,top,cnt : longint;
17 procedure add(x,y: longint );
18 var
19 t : node;
20 begin
21 new(t);
22 t^.goal:=y;
23 t^.next:=l[x];
24 l[x]:=t;
25 end; { add }
26 procedure init;
27 var
28 i,x,y : longint;
29 begin
30 readln(n,m);
31 cnt:=0;
32 for i:=1 to n do
33 l[i]:=nil;
34 fillchar(instack,sizeof(instack),false);
35 for i:=1 to m do
36 begin
37 readln(x,y);
38 add(x,y);
39 end;
40 end; { init }
41 procedure dfs(x :longint );
42 var
43 t : node;
44 begin
45 inc(cnt);
46 dfn[x]:=cnt;
47 low[x]:=cnt;
48 inc(top);
49 stack[top]:=x;
50 instack[x]:=true;
51 new(t);
52 t:=l[x];
53 while t<>nil do
54 begin
55 if dfn[t^.goal]=0 then
56 begin
57 dfs(t^.goal);
58 if low[t^.goal]<low[x] then
59 low[x]:=low[t^.goal];
60 if dfn[t^.goal]<low[x] then
61 low[x]:=dfn[t^.goal];
62 end
63 else
64 if (instack[t^.goal]=true)and(dfn[t^.goal]<low[x]) then
65 begin
66 low[x]:=dfn[t^.goal];
67 end;
68 t:=t^.next;
69 end;
70 if low[x]=dfn[x] then
71 begin
72 inc(num);
73 while true do
74 begin
75 ans[stack[top]]:=num;
76 instack[stack[top]]:=false;
77 dec(top);
78 if stack[top+1]=x then
79 break;
80 end;
81 end;
82 end; { dfs }
83 procedure print;
84 var
85 i : longint;
86 begin
87 for i:=1 to n do
88 write(ans[i],'');
89 end; { print }
90 begin
91 init;
92 for e:=1 to n do
93 if dfn[e]=0 then
94 dfs(e);
95 print;
96 End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.