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)
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.
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:
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.
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:
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With