-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.py
More file actions
67 lines (60 loc) · 1.47 KB
/
Copy pathgraph.py
File metadata and controls
67 lines (60 loc) · 1.47 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
57
58
59
60
61
62
63
64
65
66
67
import json
def Node:
def __init__(self, name, company, goodness):
self.name = name
self.company = company
self.goodness = goodness
def Edge:
def __init__(self, name1, name2, strength):
self.name1, self.name2 = name1, name2
self.strength = strength
def norm(v, s=None):
s = s or sum(v)
return [e/s for e in v]
#normalizes outgoing probabilities of a directed graph
def preprocess(net):
return [norm(vec) for vec in net]
def makeJSON(nodes, edges):
return json.dumps({
'nodes': list(map(lambda n: [n.name, n.company, n.goodness], nodes)),
'edges': list(map(lambda e: [e.name1, e.name2, e.strength], edges)),
})
'''
graph - weighted adjacency matrix.
start - initial vertex for random walk.
P - probability of stopping walk each step.
k - iterations before quitting.
'''
def markovWalk(graph, start, P, k):
V = len(graph)
vals = [0]*V
vals[start] = 1
probs = [0]*V
for itr in range(k):
nextv = [0]*V
for i in range(V):
val = vals[i]
for j,w in enumerate(graph[i]):
nextv[j] += w*val
vals = nextv
for i in range(V):
probs[i] += P*vals[i]
vals[i] *= 1-P
return [p+v for p,v in zip(probs,vals)]
'''
#usage:
g = [
[0, 0.9, 0.1],
[0.1, 0, 0.9],
[0.1, 0.9, 0]
]
g2 = [
[0, 0.9, 0.1],
[0.1, 0, 0.9],
[0.1, 0.9, 0]
]
#divides all the connection strengths by 3
g2 = [norm(v,3.0) for v in g2]
print(markovWalk(preprocess(g), 0, 0.1, 50))
print(markovWalk(preprocess(g2), 0, 0.1, 50))
'''