您的位置:首页 > 其它

强连通分量、Tarjan与蝴蝶结构

2012-06-14 09:52 686 查看
1.强连通

有向图中强连通分量(stronglyconnected component),指的就是其中的任意顶点与其它任意顶点间有双向路径。求解图中的SCC,主要的算法有Kosaraju、Gabow和Tarjan三种,且时间复杂度都是O(m+n),其中m为图的边数,而n为图的定点数。

2.Tarjan算法

Tarjan算法基于图深度优先搜索算法,每个强连通分量为搜索树中的一颗子树。搜索时,把当前搜索树中未处理的节点加入一个对战,回溯时可以判断栈顶到栈中的某个节点是否为一个强连通分量。

算法流程:

(i):初始化数组DFN和Low,分别用来记录节点首次被访问的时间,以及可追溯的最远父节点;同时还要初始化一个堆栈S,用来保存DFS时形成的子树;时间记录time=1;

(ii):遍历图中的节点i,time加1,并将DFN[i]=time,Low[i]=time;遍历i的邻居j,对于特定的邻居j:

如果j未被访问,则跳转到ii(j),直到j的DFS完毕,将Low[i]设为Low[i]和Low[j]中的较小值

如果j被访问过了,则将i的Low[i]设为Low[i]和DFN[j]中的较小值

(iii):如果DFN[i]==Low[i],则i为强连通分量的根,则对S进行出栈操作,直到出栈的元素DFN和Low再次相等,即一个新的连通分量的根。

3.蝴蝶结构

图中的蝴蝶结构包括三个部分,蝴蝶的身体、翅膀和卷须(Trendil)。蝴蝶的身体是一个SCC;左边翅膀称为IN部分,该部分的特点是可以通过IN中的节点到达SCC,但是从SCC不能到达IN;右边的翅膀称为OUT部分,该部分的特点是可以通过SCC中的节点到达,但是OUT中的节点无法到达SCC;对于Trendil部分来说,是和IN和OUT连通但是和SCC不连通的部分。

利用Tarjan算法,找出SCC,然后通过SCC的节点进行BFS遍历,可以找出OUT部分,然后将图中的边进行变向后,再次使用BFS,可以找出IN部分,然后剪除SCC+OUT+IN部分,就可以得到Trendil部分了。

但是遗留的问题:

1.效率问题。

2.IN和OUT中的Trendil应该区分,此外还可能将孤立点认为是Trendil。

4.范例



import networkx as nx
from collections import deque
import matplotlib.pyplot as plt
def miner(i,j):
if i<j :
return i
return j
def BFS_visit(i,G=nx.Graph()):
visited=[]
j=1
while j<=G.number_of_nodes():
visited.append(0)
j=j+1
visit_list=deque()
visit_sq=[]
visit_list.append(i)
while visit_list:
visit=visit_list.popleft()
visited[visit-1]=1
visit_sq.append(visit)
for node in G:
if G.has_edge(visit, node) and visited[node-1]==0:
visit_list.append(node)
return visit_sq

def tarjan(i,G=nx.DiGraph()):
global dfn
global low
global stack
global instack
global time
global components
dfn[i-1]=time
low[i-1]=time
time=time+1
stack.append(i)
instack[i-1]='True'
neighbors=G.successors(i)
if neighbors:
for node in neighbors:
if dfn[node-1]==-1:
tarjan(node, G)
low[i-1]=miner(low[i-1],low[node-1])
elif instack[node-1]=='True':
low[i-1]=miner(low[i-1],dfn[node-1])
com_list=[]
if dfn[i-1]==low[i-1]:
oo=stack.pop()
instack[oo-1]='False'
com_list.append(oo)
while dfn[oo-1]!=low[oo-1]:
oo=stack.pop()
instack[oo-1]='False'
com_list.append(oo)
if com_list:
components.append(com_list)
def Butterfly(Body=[],G=nx.DiGraph()):
in_body=[]
out_body=[]
for node in Body:
tmp=BFS_visit(node, G)
for node1 in tmp:
if node1 not in out_body:
out_body.append(node1)
for node1 in Body:
if node1 in out_body:
out_body.remove(node1)
G1=nx.DiGraph()
for node1 in G:
for node2 in G:
if G.has_edge(node1,node2):
G1.add_edge(node2,node1)
elif G.has_edge(node2,node1):
G1.add_edge(node1,node2)
for node in Body:
tmp=BFS_visit(node,G1)
for node1 in tmp:
if node1 not in in_body:
in_body.append(node1)
for node1 in Body:
if node1 in in_body:
in_body.remove(node1)
trendil=[]
butterfly=[]
butterfly=Body+in_body+out_body
for node in G.nodes():
if node not in butterfly:
trendil.append(node)
return in_body,out_body,trendil

G=nx.DiGraph()
dfn=[]
low=[]
instack=[]
stack=[]
components=[]
time=1
G.add_edges_from([(1,2),(2,3),(3,4),(4,1),(5,2),(5,7),(4,6),(8,6)])
for node in G.nodes():
dfn.append(-1)
low.append(-1)
instack.append('False')
for node in G.nodes():
if dfn[node-1]==-1:
tarjan(node, G)
sorted_component=[]
i=1
while i<=G.number_of_nodes():
for com in components:
if len(com)==i:
sorted_component.append(com)
i=i+1
print sorted_component
Body=sorted_component.pop()
print Body
In,Out,Trendril=Butterfly(Body,G)
print 'Butterfly_body',Body
print 'Butterfly_In',In
print 'Butterfly_out',Out
print 'Butterfly_trendil',Trendril
pos=nx.shell_layout(G)
labels={1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8}
pos={1:(2,2),2:(1,1),3:(2,0),4:(3,1),5:(0,1),6:(4,1),7:(0,0),8:(4,0)}
nx.draw_networkx_nodes(G,pos,nodelist=Body,node_color='b',node_size=180,alpha=0.8)
nx.draw_networkx_nodes(G,pos,nodelist=In,node_color='g',node_size=120,alpha=0.8)
nx.draw_networkx_nodes(G,pos,nodelist=Out,node_color='y',node_size=120,alpha=0.8)
nx.draw_networkx_nodes(G,pos,nodelist=Trendril,node_color='r',node_size=80,alpha=0.8)
nx.draw_networkx_edges(G,pos,width=1.0,alphs=0.5)
nx.draw_networkx_labels(G,pos,labels,font_size=20)
plt.axis('off')
plt.show()





SCC的相关算法参考:

http://www.byvoid.com/blog/scc-tarjan/

/article/2695642.html

http://rchardx.is-programmer.com/posts/14898.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: