I'm working on reproduce of Ripple Walk Sampler. This is an algorithm to extract subgraph from target graph.
Here we have to set s and r as parameters. s is the size of subgraph and r is an expansion ratio, which means the ration of nodes in neighbor set to be sampled in current step.
For subgraph Gk, it is initialized with a random node vs, then expands along the connections among nodes.
After multiple steps of expansion, the subgraph with a size of s will be returned. During each expansion, the neighbor set contains the potential nodes to be sampled.
Then r of the nodes in neighbor set will be added into the current subgraph.
Here is the original pseudo code and the example of Sampling process.
And here is my attempt:
import random
import networkx as nx
import numpy as np
m = np.matrix([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]])
G = nx.from_numpy_matrix(m) #target graph
r = 0.5 #expansion ratio
S = 4 #subgraph size
Gk = nx.DiGraph() #initialize subgraph
Vk = [] #initialize nodes
vs = random.randint(0, G.size()) #randomly select the initial node from G
Gk.add_node(vs) #add vs to Gk
while len(Vk) < S:
#get neighbor nodes set of Vk
NS = [n for n in G.neighbors(vs)]
print(NS)
#randomly select r of nodes in NS, add them into the Vk
for nodes in NS:
if random.random() < r:
Vk.append(nodes)
I'm struggling with the logic of line 4 in pseudo code, the part of getting neighbor set of Vk. I know this code is wrong but how should I implement this part?
Can someone help me to fix this? Any suggestions/advices would be appreciated.


