我正在写一个函数get_connected_components
一类Graph
:
def get_connected_components(self):
path=[]
for i in self.graph.keys():
q=self.graph[i]
while q:
print(q)
v=q.pop(0)
if not v in path:
path=path+[v]
return path
我的图是:
{0: [(0, 1), (0, 2), (0, 3)], 1: [], 2: [(2, 1)], 3: [(3, 4), (3, 5)], \
4: [(4, 3), (4, 5)], 5: [(5, 3), (5, 4), (5, 7)], 6: [(6, 8)], 7: [], \
8: [(8, 9)], 9: []}
其中键是节点和值的边缘。 我的功能给了我这个连接的组件:
[(0, 1), (0, 2), (0, 3), (2, 1), (3, 4), (3, 5), (4, 3), (4, 5), (5, 3), \
(5, 4), (5, 7), (6, 8), (8, 9)]
不过,我想有两个不同的连接组件,如:
[[(0, 1), (0, 2), (0, 3), (2, 1), (3, 4), (3, 5), (4, 3), (4, 5), \
(5, 3), (5, 4), (5, 7)],[(6, 8), (8, 9)]]
我不明白的地方我所犯的错误。 谁能帮我?
Answer 1:
让我们简化了图表示:
myGraph = {0: [1,2,3], 1: [], 2: [1], 3: [4,5],4: [3,5], 5: [3,4,7], 6: [8], 7: [],8: [9], 9: []}
在这里,我们的函数返回一个字典的键是根和其值是连接组件:
def getRoots(aNeigh):
def findRoot(aNode,aRoot):
while aNode != aRoot[aNode][0]:
aNode = aRoot[aNode][0]
return (aNode,aRoot[aNode][1])
myRoot = {}
for myNode in aNeigh.keys():
myRoot[myNode] = (myNode,0)
for myI in aNeigh:
for myJ in aNeigh[myI]:
(myRoot_myI,myDepthMyI) = findRoot(myI,myRoot)
(myRoot_myJ,myDepthMyJ) = findRoot(myJ,myRoot)
if myRoot_myI != myRoot_myJ:
myMin = myRoot_myI
myMax = myRoot_myJ
if myDepthMyI > myDepthMyJ:
myMin = myRoot_myJ
myMax = myRoot_myI
myRoot[myMax] = (myMax,max(myRoot[myMin][1]+1,myRoot[myMax][1]))
myRoot[myMin] = (myRoot[myMax][0],-1)
myToRet = {}
for myI in aNeigh:
if myRoot[myI][0] == myI:
myToRet[myI] = []
for myI in aNeigh:
myToRet[findRoot(myI,myRoot)[0]].append(myI)
return myToRet
让我们试一下:
print getRoots(myGraph)
{8:[6,8,9],1:[0,1,2,3,4,5,7]}
Answer 2:
我喜欢这样的算法:
def connected_components(neighbors):
seen = set()
def component(node):
nodes = set([node])
while nodes:
node = nodes.pop()
seen.add(node)
nodes |= neighbors[node] - seen
yield node
for node in neighbors:
if node not in seen:
yield component(node)
它不仅是短期和优雅,但也快。 使用它,像这样(的Python 2.7):
old_graph = {
0: [(0, 1), (0, 2), (0, 3)],
1: [],
2: [(2, 1)],
3: [(3, 4), (3, 5)],
4: [(4, 3), (4, 5)],
5: [(5, 3), (5, 4), (5, 7)],
6: [(6, 8)],
7: [],
8: [(8, 9)],
9: []}
new_graph = {node: set(each for edge in edges for each in edge)
for node, edges in old_graph.items()}
components = []
for component in connected_components(new_graph):
c = set(component)
components.append([edge for edges in old_graph.values()
for edge in edges
if c.intersection(edge)])
print components
其结果是:
[[(0, 1), (0, 2), (0, 3), (2, 1), (3, 4), (3, 5), (4, 3), (4, 5), (5, 3), (5, 4), (5, 7)],
[(6, 8), (8, 9)]]
Answer 3:
以前的答案是伟大的。 无论如何,它花了我有点明白了事情的原委。 所以,我以这种方式更易于阅读我重构代码。 我离开这里万一有人创立很容易过的代码(它在Python 3.6中运行)
def get_all_connected_groups(graph):
already_seen = set()
result = []
for node in graph:
if node not in already_seen:
connected_group, already_seen = get_connected_group(node, already_seen)
result.append(connected_group)
return result
def get_connected_group(node, already_seen):
result = []
nodes = set([node])
while nodes:
node = nodes.pop()
already_seen.add(node)
nodes = nodes or graph[node] - already_seen
result.append(node)
return result, already_seen
graph = {
0: {0, 1, 2, 3},
1: set(),
2: {1, 2},
3: {3, 4, 5},
4: {3, 4, 5},
5: {3, 4, 5, 7},
6: {6, 8},
7: set(),
8: {8, 9},
9: set()}
components = get_all_connected_groups(graph)
print(components)
结果:
Out[0]: [[0, 1, 2, 3, 4, 5, 7], [6, 8, 9]]
另外,我简化了输入和输出。 我认为这是更清晰了一下,打印所有的是一组中的节点
文章来源: Python connected components