[NetworkX-discuss] removing unconnected nodes

1,869 views
Skip to first unread message

Frederic Back

unread,
Jun 9, 2006, 4:28:35 AM6/9/06
to networkx...@lists.sourceforge.net
Hello,

I'm trying to remove solitary nodes from my graph, ie. nodes which are
unconnected to the main network of nodes.

To achieve this, I'm creating a new graph from the original one. See
this example (where node 1000 would be my 'central node').

new_graph = XGraph()
for node in node_connected_component( original_graph, 1000 ):
for edge in original_graph.edges(node):
new_graph.add_edge(*edge)

Is there a simpler way I overlooked? If not, what about an API function
to create a new graph from a bunch of connected nodes?

Thanks,
Fred

_______________________________________________
NetworkX-discuss mailing list
NetworkX...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/networkx-discuss

Dan Schult

unread,
Jun 9, 2006, 5:45:56 AM6/9/06
to Frederic Back, networkx...@lists.sourceforge.net
If you really only want to get rid of solitary nodes, you could
delete nodes that have zero degree. Something like:

>>> solitary=[ n for n,d in G.degree_iter(with_labels=True) if d==0 ]
>>> G.delete_nodes_from(solitary)


For more general node removal, I would suggest the subgraph() method.
If you can get a list of the nodes you want to keep, use that as an
argument
to the subgraph routine. Something like:

>>> keepers=node_connected_component(original_graph, 1000)
>>> new_graph=original_graph.subgraph(keepers)

Note that this is getting rid of more than just solitary nodes
if you have two nonsolitary connected components.
Dan

Frederic Back

unread,
Jun 9, 2006, 5:59:07 AM6/9/06
to Dan Schult, networkx...@lists.sourceforge.net
Hi Dan,

I was just discussing it with Sverre, but our discussion didn't make the
list.

My problem is that I have 'nonsolitary connected components' hanging
around, nodes that are not connected to the 'main' network. I am not
trying to get rid of completely unconnected nodes as I first posted,
because my nodes are connected by definition. Sorry for the mistake.

Sverre just proposed this solution, which is almost the same as yours:
new_graph = old_graph.subgraph([x for x in node_connected_component(
old_graph, 1000 ))

It works like a charm, but using subgraph is sensibly slower than
my original approach, so I guess I'll stick with it.

Thanks!
Fred

Pieter Swart

unread,
Jun 9, 2006, 11:35:37 AM6/9/06
to Frederic Back, Dan Schult, networkx...@lists.sourceforge.net
Fred, for improved speed and readability, also try

important_nodes=node_connected_component(old_graph, 1000 )
new_graph = old_graph.subgraph(important_nodes)

and the last can also be
new_graph = old_graph.subgraph(important_nodes, inplace=True)

note that
1) subgraph can use any iterator/iterable
2) list comprehension is great, but you might end up
with a large list in memory
3) inplace=True demolish edges in old_graph and then
return old_graph

test for timing

best
Pieter

Reply all
Reply to author
Forward
0 new messages