Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get node list from random walk in networkX

I am new to networkX. I created a graph as follows:

G = nx.read_edgelist(filename,
                     nodetype=int,
                     delimiter=',',
                     data=(('weight', float),))

where the edges are positive, but do not sum up to one.

Is there a built-in method that makes a random walk of k steps from a certain node and return the node list? If not, what is the easiest way of doing it (nodes can repeat)?

Pseudo-code:

node = random
res = [node]
for i in range(0, k)
    read edge weights from this node
    an edge from this node has probability weight / sum_weights
    node = pick an edge from this node 
    res.append(node)
like image 305
Bob Avatar asked May 18 '16 23:05

Bob


People also ask

How can I get a list of the nodes in NetworkX?

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. 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.

Is there a list of all nodes in a graph?

If your node data is not needed, it is simpler and equivalent to use the expression for n in G, or list (G). There are two simple ways of getting a list of all nodes in the graph:

How to find all nodes of a directed acyclic graph?

We can use shortest_path () to find all of the nodes reachable from a given node. Alternatively, there is also descendants () that returns all nodes reachable from a given node (though the document [1] specified input G as directed acyclic graph.

How to get the node data along with the nodes?

To get the node data along with the nodes: If some of your nodes have an attribute and the rest are assumed to have a default attribute value you can create a dictionary from node/attribute pairs using the default keyword argument to guarantee the value is never None:


1 Answers

The easiest way of doing it is by using the transition matrix T and then using a plain Markovian random walk (in brief, the graph can be considered as a finite-state Markov chain).

Let A and D be the adjacency and degree matrices of a graph G, respectively. The transition matrix T is defined as T = D^(-1) A.
Let p^(0) be the state vector (in brief, the i-th component indicates the probability of being at node i) at the beginning of the walk, the first step (walk) can be evaluated as p^(1) = T p^(0).
Iteratively, the k-th random walk step can be evaluated as p^(k) = T p^(k-1).

In plain Networkx terms...

import networkx
import numpy
# let's generate a graph G
G = networkx.gnp_random_graph(5, 0.5)
# let networkx return the adjacency matrix A
A = networkx.adj_matrix(G)
A = A.todense()
A = numpy.array(A, dtype = numpy.float64)
# let's evaluate the degree matrix D
D = numpy.diag(numpy.sum(A, axis=0))
# ...and the transition matrix T
T = numpy.dot(numpy.linalg.inv(D),A)
# let's define the random walk length, say 10
walkLength = 10
# define the starting node, say the 0-th
p = numpy.array([1, 0, 0, 0, 0]).reshape(-1,1)
visited = list()
for k in range(walkLength):
    # evaluate the next state vector
    p = numpy.dot(T,p)
    # choose the node with higher probability as the visited node
    visited.append(numpy.argmax(p))
like image 164
AlessioX Avatar answered Oct 10 '22 04:10

AlessioX