1

I want to compare the performance of two graphs, the second one being a Kn complete graph. Let's call G the first graph (the non-complete one) and H the second one (the Kn graph). I want the nodes from H to have the exact same attributes (and relative values) as the ones from G, so (apparently) I can't just run networkx.complete_graph(n). My first idea was to use this code:

H = G.copy()
nh = H.nodes()
eh = H.edges() # I create those variables in hope to save some speed
for u in nh:
    for v in filter(lambda x: x > u, nh): # it's an undirected graph, saves some speed
        if (u,v) not in eh:
            H.add_edge(u,v)

However, since my graphs have 10,000+ nodes, this becomes quite a slow process. Is there a way to speed it up?

1
  • 1
    I think the answer you've got is likely the best option, but just a couple comments on this code - testing if something is in a list is slow. Instead you would want to make eh a set. Also, I don't think there's much value in testing whether something is already an edge - I'm not sure that adding an edge which already exists is significantly slower than first testing if the edge exists. Commented Nov 11, 2016 at 21:39

1 Answer 1

1

Ok, I may have found an answer:

H = networkx.complete_graph(n)
labels = nx.get_node_attributes(G,attribute)
nx.set_node_attributes(H,attribute,labels)

Does anyone have a faster alternative?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.