Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a fully connected subgraph from node list using python's networkx module

Tags:

I need to generate a fully connected subgraph with networkx, starting from the list of nodes I want to connect. Basically, I want all the nodes in the list I pass to the function to be all connected with each other.

I wonder if there is any built-in function to achieve this (which I haven't found)? Or should I think of some algorithm?

Thank you very much.

like image 677
Wilco Avatar asked May 18 '12 09:05

Wilco


People also ask

How do I create a complete graph in NetworkX?

The command G_ex = nx. complete_graph(4) creates a complete graph G whose nodes are 0, 1, 2, and 3. You then add more to G , but it has those nodes.

How do you create a subgraph in Python?

We can draw the subgraph of those nodes by using a familiar function, nx-dot-draw.

What is Nbunch in NetworkX?

nbunch. An nbunch is a single node, container of nodes or None (representing all nodes). It can be a list, set, graph, etc.. To filter an nbunch so that only nodes actually in G appear, use G.


1 Answers

I don't know of any method which does this, but you can easily mimic the complete_graph() method of networkx and slightly change it(almost like a builtin):

import networkx import itertools  def complete_graph_from_list(L, create_using=None):     G = networkx.empty_graph(len(L),create_using)     if len(L)>1:         if G.is_directed():             edges = itertools.permutations(L,2)         else:             edges = itertools.combinations(L,2)         G.add_edges_from(edges)     return G  S = complete_graph_from_list(["a", "b", "c", "d"]) print S.edges() 
like image 132
zubinmehta Avatar answered Sep 19 '22 23:09

zubinmehta