0

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
  • 1
    Note - I've added an edit showing how to modify G in place as well. Commented Nov 26, 2015 at 12:40

1 Answer 1

2

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'] 
Sign up to request clarification or add additional context in comments.

1 Comment

I've added a line at the end to show how to modify G in place

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.