本文主要是介绍有向图的强连通算法 -- tarjan算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
(画图什么真辛苦)
强连通分量:
在有向图 G 中,若两个顶点相互可达,则称两个顶点强连通(strongly connected)。如果有向图G的每两个顶点都强连通,称G是一个强连通图。非强连通图有向图的极大强连通子图,称为强连通分量(strongly connected components)。比如上面这幅图( a, b, e ), ( d, c, h ), ( f, g ) 分别为三个 SCC。
tarjan算法伪代码:
该算法由 Robert Tarjan 发明,原论文:Tarjan1972
时间复杂度是深搜的时间复杂度 O( N + E )。
BEGININTERGER i;PROCEDURE STRONGCONNECT(v);BEGINLOWLINK(v):= NUMBER(v):= i := i + 1;put v on stack of points;FOR w in the adjacency list of v DOBEGINIF w is not yet numbered THENBEGIN comment( v, w ) is a tree arc;STRONGCONNECT(w);LOWLINK(V) := min( LOWLINK(V),LOWLINK(W));END;ELSE IF NUMBER(W) < NUMBER(V) DOBEGIN comment( v, w ) is a frond or cross-link;if w is on stack of points THENLOWLINK(v) := min( LOWLINK(v),NUMBER(w));END;END;if( LOWLINK(v) = NUMBER(v) ) THENBEGIN comment v is the root of a compont;start new strongly connected compont;WHILE w on top of point stack satisfiesNUMBER(w) >= NUMBER(v) DOBEGINdelete w from point stack and put win current component;END;END;END;i := 0;empty stack of points;FOR w a vertex IF w is not yet numbered THEN STRONGCONNECT(w);
END
tarjan算法的执行动态图:
1.建图,每个顶点有三个域,第一个是顶点名,第二个空格是发现该点的时间戳 DFN ,第三个空格是该点能够追溯到的最早的栈中节点的次序号 LOW。
2. 深搜,比如搜索路径为 a --> b --> c --> g --> f, 沿途记下自身的 DFN 和 LOW
3.达到 f 点后,f 点只有一个可达顶点 g, 但 g 不是 f 的后继顶点,且 g 在栈中,则更新 f 的 LOW 变为 g 的发现时间。
4.这时候回到 g 点,但是 f 点还得再栈中,只有发现时间 DFN 与 LOW 相同的顶点才能从栈中弹出(和压在上面的节点一起弹出构成SCC)
5.这时候 g 点的 DFN == LOW
6.于是将 f 和 g 都弹出
7.如虚线所示,他们构成了一个SCC
8.下面就是一样的了,( 图画的好辛苦 )
=========================================================================================
========================================================================================
=========================================================================================
==========================================================================================
==========================================================================================
python代码:
def strongly_connected_components( graph ):dfn_count = [0]result = []stack = []low = {}dfn = {}def stronglyconnected( node ):dfn[node] = dfn_count[0]low[node] = dfn_count[0]dfn_count[0] += 1stack.append( node )if node not in graph:successors = []else:successors = graph[node]for successor in successors:if successor not in dfn:stronglyconnected( successor )low[node] = min( low[successor], low[node] )elif successor in stack:low[node] = min( low[node], dfn[successor] )if low[node] == dfn[node]:li = []item = Nonewhile True:item = stack.pop()li.append( item )if item == node: breakresult.append( li )for node in graph:if node not in low:stronglyconnected( node )return resultif __name__ == '__main__':graph = {'a': [ 'b' ],'b': [ 'c', 'e', 'f' ],'c': [ 'g', 'd' ],'d': [ 'c', 'h' ],'e': [ 'a', 'f' ],'f': [ 'g' ],'g': [ 'f' ],'h': [ 'g', 'd' ]}print strongly_connected_components( graph )
运行结果:
C++代码:
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;#define MAX_SIZE 100bool Graph[MAX_SIZE][MAX_SIZE];int Stack[MAX_SIZE];
bool nodeIsInStack[MAX_SIZE];
int stack_pointer = 0;int Lows[MAX_SIZE];
int Dfns[MAX_SIZE];int node_num = 0; // 图中节点得到个数
int find_time = 0; // 每个节点的发现时间int scc_num = 0; // 记录 scc 的个数void find_scc( int start_node ){find_time++;Lows[start_node] = Dfns[start_node] = find_time;stack_pointer++;Stack[stack_pointer] = start_node;nodeIsInStack[start_node] = true;for( int end_node = 1; end_node <= node_num; ++end_node ){//若start_node和end_ndoe之间存在边if( Graph[start_node][end_node] ){//若是end_node尚未被访问if( Dfns[end_node] == 0 ){find_scc( end_node );Lows[start_node] = min( Lows[start_node], Lows[end_node] );}//若end_node在栈中,也就是start_node -> end_node是返祖边else if( nodeIsInStack[end_node] ){Lows[start_node] = min( Lows[start_node], Dfns[end_node] );}}}//若是start_node的时间戳与Lows相等在构成SCCif( Dfns[start_node] == Lows[start_node] ){scc_num++;cout << "scc_num: " << scc_num << endl;int pop_node_index = Stack[stack_pointer];stack_pointer--;nodeIsInStack[pop_node_index] = false;cout << pop_node_index << " ";while( start_node != pop_node_index ){pop_node_index = Stack[stack_pointer];nodeIsInStack[pop_node_index] = false;stack_pointer--;cout << pop_node_index << " ";}cout << endl;}
}void init_values(){memset( Graph, false, sizeof( Graph ) );memset( Stack, 0, sizeof( Stack ) );memset( nodeIsInStack, false, sizeof( nodeIsInStack ) );memset( Lows, 0, sizeof( Lows ) );memset( Dfns, 0, sizeof( Dfns ) );
}int main(){init_values();// 初始化图//这里用数字代替节点的名字int start_node, end_node;cin >> node_num;while( true ){cin >> start_node >> end_node;//起始点终止点都为 0 的时候结束if( start_node == 0 && end_node == 0 )break;Graph[start_node][end_node] = true;}for( int start_node = 1; start_node <= node_num; ++start_node ){//该节点尚未被访问到if( Dfns[start_node] == 0 ){find_scc( start_node );}}}
这篇关于有向图的强连通算法 -- tarjan算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!