-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom Walk_python code
More file actions
56 lines (40 loc) · 1.19 KB
/
Random Walk_python code
File metadata and controls
56 lines (40 loc) · 1.19 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_node('a')
G.add_node('b')
G.add_node('c')
G.add_node('x')
G.add_node('y')
G.add_node('z')
G.add_edges_from([
('a', 'b'), ('b', 'c'), ('a', 'c'), ('c', 'x'),
('x', 'y'), ('y', 'z'), ('x', 'z')
])
nx.draw(G, with_labels=True)
plt.show()
def find(graph, previous, current):
probability = {}
for node in graph.neighbors(current):
probability[node] = 1
probability_sum = sum(probability.values())
probability = {i: j / probability_sum for i, j in probability.items()}
k = [i for i in probability.keys()]
v = [i for i in probability.values()]
next_node = np.random.choice(a=k, p=v)
return next_node
def random_walk(graph, step, length):
answer_path = []
for i in range(step):
path = [np.random.choice(G.nodes())]
next_node = find(G, None, path[-1])
path.append(next_node)
for j in range(2, length):
next_node = find(G, path[-2], path[-1])
path.append(next_node)
answer_path.append(path)
return answer_path
random_walk_value = random_walk(G, 10, 10)
for i in random_walk_value:
print(i)