Connected component as a subgraph
Hi The min_spanning_tree function works only for connected graph. So in ordrer to find a minimum spanning forest of a undirected weighted graph (say G), I need to retrieve the connected components of G. I can do it with the label_components function. The problem I have now is I don't know how to build the subgraph induced by each connected component. Here is the piece of code I am working from : # ---------------------------- import graph_tool as gt from graph_tool.topology import min_spanning_tree, label_components import numpy as np wedges=[(1, 2, 3.), (0, 2, 6.), (3, 4, 1.)] g= gt.Graph(directed=False) weight = g.new_edge_property("double") g.add_edge_list(np.array(wedges), eprops=[weight]) u, v=label_components(g) print(u.a) # ---------------------------- outputting [0 0 0 1 1] Can somebody explain how to retrieve the subgraph of g induced by the connected component labeled 0? Thanks in advance
Am 07.07.19 um 15:33 schrieb elastica@laposte.net:
Hi The min_spanning_tree function works only for connected graph. So in ordrer to find a minimum spanning forest of a undirected weighted graph (say G), I need to retrieve the connected components of G. I can do it with the label_components function. The problem I have now is I don't know how to build the subgraph induced by each connected component. Here is the piece of code I am working from :
# ---------------------------- import graph_tool as gt from graph_tool.topology import min_spanning_tree, label_components import numpy as np
wedges=[(1, 2, 3.), (0, 2, 6.), (3, 4, 1.)] g= gt.Graph(directed=False) weight = g.new_edge_property("double") g.add_edge_list(np.array(wedges), eprops=[weight]) u, v=label_components(g) print(u.a) # ----------------------------
outputting
[0 0 0 1 1]
Can somebody explain how to retrieve the subgraph of g induced by the connected component labeled 0?
The easiest is to create a graph view: sub = GraphView(g, vfilt=u.a == 0) # sub contains only the vertices of # the component with label 0 Best, Tiago -- Tiago de Paula Peixoto <tiago@skewed.de>
participants (2)
-
elastica@laposte.net -
Tiago de Paula Peixoto