Python Connected Components edges list

为君一笑 提交于 2020-12-30 03:40:27

问题


I use these algorithms in python for finding connected components from edges.

components = []

def connected_components(pairs):
    for a, b in pairs:
        for component in components:
            if a in component:
                for i, other_component in enumerate(components):
                    if b in other_component and other_component != component: # a, and b are already in different components: merge
                        component.extend(other_component)
                        components[i:i+1] = []
                        break # we don't have to look for other components for b
                else: # b wasn't found in any other component
                    if b not in component:
                        component.append(b)
                break # we don't have to look for other components for a
            if b in component: # a wasn't in in the component 
                component.append(a)
                break # we don't have to look further
        else: # neither a nor b were found
            components.append([a, b])
    return components

This algorithms return components like this :

[ [n1,n2,n4],[n3,n5] ]

I would like to have the list of all edges in connected components like this :

[ [(n1,n2),(n2,n4),(n4,n1)],[(n3,n5)] ] 

in the same order of the previous list but i don't know how creates this list

Thank you for your help.


回答1:


Note: This doesn't require any python dependency.

I will share my approach, with recursive depth-first search. I am assuming graph is bi-directional and the following code can be easily manipulated for directed graph.

pairs = [] // edge list
adj_list = {} // adjacency list
vis = [] // visited_list
connected_components = [] // contains all the connected components
temp_component = [] 

// basic depth first search
def dfs( node ):
     vis[node] = "true"
     temp_component.append(node)
     for neighbour in adj_list[node]:
       if vis[neighbour] == "false":
          dfs(neigbour)

//main
for a,b in pairs:
 if a not in adj_list:
  adj_list[a] = [] 
 if b not in adj_list:
  adj_list[b] = []
 adj_list[a].append(b)
 adj_list[b].append(a)
 vis["a"] = "false"
 vis["b"] = "false" 

for a,b in pairs:
  temp_component = []
  if vis[a] == "false":
   dfs(a)
 if len(temp_component) > 0:
   connected_components.append(temp_component)

 // once you have connected components you can get the edge lists in connected component as well
 answer = [] 
 for component in connected_components:
    temp_pairs = [] // contains the pair of edges for the current connected component
     for node in component:
        for i,j in pairs: 
          if (node == i or node == j) and (i,j) not in temp_node: 
              temp_node.append(i,j)
     answer.append(temp_pairs) 



回答2:


Create a mini graph in python using apgl library. You can use SparseGraph module from apgl. from apgl.graph import SparseGraph

Initiate a sparsegraph with number of nodes required. graph = SparseGraph(num_vertices)

Then you can create a mini graph by adding edges between nodes of graph. graph.addEdge(component1, component2)

Then just use findConnectedComponents function to find connected components. graph.findConnectedComponents()



来源:https://stackoverflow.com/questions/42251308/python-connected-components-edges-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!