Reputation: 123
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.
Upvotes: 11
Views: 21713
Reputation: 1045
With somewhat recent versions of NetworkX (>= 2.5
I think), you can use random.sample()
directly on the node view to get a sample of either just the labels/indexes of the nodes, or the labels and the data of the nodes.
import networkx as nx
import random as rd
# Generate the example Karate club graph provided in NetworkX
g = nx.karate_club_graph()
print(g.nodes) # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
# Get a random sample (without replacement) of the node labels/indexes:
sample = rd.sample(g.nodes, 3)
print(sample) # Output: [22, 18, 6]
# Get a random sample (without replacement) of the node labels and data:
sample = rd.sample(g.nodes.items(), 3)
print(sample) # Output: [(24, {'club': 'Officer'}), (27, {'club': 'Officer'}), (31, {'club': 'Officer'})]
In slightly older versions (from 2.0
but before 2.5
), you needed to convert the node view to a list before using random.sample
.
Note: You do not have to worry about this if you have an up-to-date NetworkX package installed.
# Get a random sample (without replacement) of the node labels/indexes
# in older version of NetworkX.
sample = rd.sample(list(g.nodes), 3)
Upvotes: 2
Reputation: 311
Instead of using the choice
, please check the sample
method.
from random import sample
import os
import networkx as nx
# load a graph from local
working_path = r'XXX'
graph = nx.read_gexf(os.path.join(working_path, 'XXX.gexf'))
# random sample 3 nodes from the graph
random_nodes = sample(list(graph.nodes()), 3)
If you have a graph g
with different node types, you could use the followig function to count the number of nodes having a specific type:
def count_nodes_with_type(graph, attribute_name, query):
"""
Count the number of nodes with specific type
:param graph: a networkx graph whose nodes have several different types
:param attribute_name: the attribute name used to access different type of nodes
:param query: the search query
:return: number of nodes satisfying the query
"""
node_attribute_dict = nx.get_node_attributes(graph, attribute_name)
filtered_nodes = {key: value for (key, value) in node_attribute_dict.items() if value == query}
return len(filtered_nodes)
You could use the same logic to count the number of specific type of edges using the nx.get_edge_attributes
method.
Upvotes: 4
Reputation: 4643
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
Upvotes: 5