Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract random nodes from networkx graph?

How to extract random nodes from networkx graph? I have a map in the form of a networkx graph and I have to extract 5 random nodes from it and get the data associated to each of them and their edges. I suppose I can do that with "np.random.choice" but I still can't get any results.

like image 587
Lora Kostova Avatar asked Nov 05 '14 22:11

Lora Kostova


1 Answers

import networkx as nx
from random import choice

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)

random_node = choice(g.nodes())

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

like image 92
chishaku Avatar answered Oct 28 '22 13:10

chishaku