MCPcopy Create free account
hub / github.com/microsoft/JARVIS / sample_subgraph_random_walk

Method sample_subgraph_random_walk

taskbench/graph_sampler.py:139–167  ·  view source on GitHub ↗
(self, seed_node, num_nodes)

Source from the content-addressed store, hash-verified

137 return sub_G
138
139 def sample_subgraph_random_walk(self, seed_node, num_nodes):
140 # Create a list to store the sub-graph nodes
141 sub_graph_nodes = [seed_node]
142 edges = []
143
144 # Keep adding nodes until we reach the desired number
145 while len(sub_graph_nodes) < num_nodes:
146 # Randomly select a node from the current sub-graph
147 node = random.choice(sub_graph_nodes)
148 neighbors = list(self.graph.successors(node))
149
150 # If the node has neighbors, randomly select one and add it to the sub-graph
151 if neighbors:
152 neighbor = random.choice(neighbors)
153 if neighbor not in sub_graph_nodes:
154 edges.append((node, neighbor))
155 sub_graph_nodes.append(neighbor)
156 # If the node has no neighbors, select a new node from the original graph
157 else:
158 node = random.choice(list(self.graph.nodes))
159 if node not in sub_graph_nodes:
160 sub_graph_nodes.append(node)
161
162 # Create the sub-graph
163 sub_G = nx.DiGraph()
164 sub_G.add_nodes_from(sub_graph_nodes)
165 sub_G.add_edges_from(edges)
166
167 return sub_G
168
169 def sample_subgraph_random_walk_with_restart(self, seed_node, num_nodes, restart_prob=0.15):
170 # Create a list to store the sub-graph nodes

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected