how to select two nodes (pairs of nodes) randomly from a graph that are NOT connected, Python, networkx -


i want extract 2 nodes graph, catch being shouldnt connected i.e. no direct edge exists between them. know can random edges using "random.choice(g.edges())" give me random nodes connected. want pairs of nodes not connected (a pair of unconnected edges). me out guys...thanx

simple! :)

grab random node - pick random node list of nodes excluding neighbours , itself. code illustrate below. :)

import networkx nx random import choice  # consider graph # #     3 #     | # 2 - 1 - 5 - 6 #     |  #     4  g = nx.graph() g.add_edge(1,2) g.add_edge(1,3) g.add_edge(1,4) g.add_edge(1,5) g.add_edge(5,6)  first_node = choice(g.nodes())                  # pick random node possible_nodes = set(g.nodes()) neighbours = g.neighbors(first_node) + [first_node] possible_nodes.difference_update(neighbours)    # remove first node , neighbours candidates second_node = choice(list(possible_nodes))      # pick second node        print first_node, second_node 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -