强连通Kosaraju算法--不知道哪里错了?请教高手~~
有问题,调试了很久也没有发现错在哪里。。。高手们,请指教
源码附件:
https://files.cnblogs.com/btchenguang/scc.zip
概念:
有向图中的强连通指的是可以相互访问到的顶点的集合,简而言之是组成
环的顶点的集合,在一个有向图中可能有很多个不同的强连通部分
算法思想:
1. Let G be a directed graph and S be an empty stack.
2. While S does not contain all vertices
Choose an arbitrary vertex v not in S. Perform a depth-first search starting at v.
Each time that depth-first search finishes expanding a vertex u, push u onto S.
3. Reverse the directions of all arcs to obtain the transpose graph.
4. While S is nonempty
Pop the top vertex v from S. Perform a depth-first search starting at v.
The set of visited vertices will give the strongly connected component containing v,
record this and remove all these vertices from the graph G and the stack S.
翻译:
1. G为有向图, S为空栈
2. 当S没有全部包含所有顶点的时候
选取任意不属于S的顶点v,对其进行深度优先查找
把查找到的点逐一添加到S中
3. 对有向图G进行反转,就是对每一条边的方向进行反转
4. 当S不为空的时候
取出最后S中是后一个顶点的值,对其进行深度优先查找
一次性能访问到的所有的点,包括顶点v组成一个强连通
纪录下这个集合,把这些点从S和反向图G中移除)
代码如下:
import pprint
import copy
import collections
import time
explored = 1
unexplored = 0
def scc(adjList):
start = time.time()
top_order = []
sccs = []
flags = collections.defaultdict(lambda :0)
print "Initialization time %d sec." % (time.time() - start)
# first dfs
start = time.time()
vertice = adjList.keys()
for v in vertice:
if flags[v] == unexplored:
dfs(adjList, flags, top_order, v)
print "First round of DFS, Cost %d sec." % (time.time() - start)
# reserve graph
start = time.time()
reserve_adjList = buildAdjListFromFile("testcase/graph0n9scc3.txt", 1)
flags = collections.defaultdict(lambda :0)
print "Reserve graph, Cost %d sec." % (time.time() - start)
# second round of DFS
start = time.time()
while len(top_order) != 0:
v = top_order.pop()
strongly_connected_component = []
dfs(reserve_adjList, flags, strongly_connected_component, v)
# remove all these vertices in scc
for item in strongly_connected_component:
if item in top_order:
top_order.remove(item)
if len(reserve_adjList.get(item)) == 0:
del reserve_adjList[item]
sccs.append(strongly_connected_component)
print "Second round of DFS, Cost %d sec." % (time.time() - start)
return sccs
visited = []
def dfs(adjList, flags, scc_list, v):
flags[v] = explored
global visited
visited.append(v)
while len(visited) != 0:
vertex = visited.pop()
# add to the same component
scc_list.append(vertex)
for m in adjList.get(vertex, []):
if flags[m] == unexplored:
flags[m] = explored
visited.append(m)
def buildAdjListFromFile(file, flag=0):
adjList = collections.defaultdict(list)
f = open(file)
for line in f:
v_info = line.strip().split()
v = int(v_info[0])
m = int(v_info[1])
# origin graph
if flag == 0:
adjList[v].append(m)
elif flag == 1:
# reverse graph
adjList[m].append(v)
f.close()
return adjList
if __name__ == "__main__":
adjList = buildAdjListFromFile("1.txt")
scc_list = scc(adjList)
print scc_list
答案应该为[[1,4,7], [3,6,9], [2,8,5]]
但我这个得出的是[[1,4,7], [2,3,5,6,8,9]]

浙公网安备 33010602011771号