Is there a convenient way to create a clique out of a list of objects (such as strings) representing vertices, instead of generating each edge manually?
1 Answer
This is probably a duplicate of How to generate a fully connected subgraph from node list using python's networkx module, but I'm going to give a different answer than what is in there.
The approach is to generate a clique and then use networkx's method for relabeling nodes.
import networkx as nx L=["hello", "world", "how", "are", "you"] G=nx.complete_graph(len(L)) H=nx.relabel_nodes(G,dict(enumerate(L))) H.nodes() > ['how', 'are', 'world', 'you', 'hello'] G.nodes() > [0,1,2,3,4] nx.relabel_nodes(G,dict(enumerate(L)), copy=False) #you can also change G in place G.nodes() > ['how', 'are', 'world', 'you', 'hello'] 1 Comment
Joel
I've added a line at the end to show how to modify
G in place
Gin place as well.