-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.py
70 lines (54 loc) · 1.49 KB
/
distance.py
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
68
69
70
def single_source_shortest_path_length(g, v, depth=None):
if depth is None:
depth = float('inf')
next = {v: 1}
return dict(single_shortest_path_length(g.adj, next, depth))
def single_shortest_path_length(adj, start, depth):
seen = {}
level = 0
next = start
while next and depth >= level:
cur = next
next = {}
for v in cur:
if v not in seen:
seen[v] = level
next.update(adj[v])
yield (v, level)
level += 1
del seen
def eccentricity(g, v=None, sp=None):
e = {}
for n in g.nbunch_iter(v):
if sp is None:
length = single_source_shortest_path_length(g, n)
else:
length = sp[n]
e[n] = max(length.values())
if v in g:
return e[v]
return e
def diameter(g, e=None):
if e is None:
e = eccentricity(g)
return max(e.values())
def periphery(g, e=None):
if e is None:
e = eccentricity(g)
diameter = max(e.values())
p = [v for v in e if e[v] == diameter]
return p
def radius(g, e=None):
if e is None:
e = eccentricity(g)
return min(e.values())
def center(g, e=None):
if e is None:
e = eccentricity(g)
radius = min(e.values())
p = [v for v in e if e[v] == radius]
return p
def average_shortest_path_length(g):
n = g.order()
s = sum(l for u in g for l in single_source_shortest_path_length(g, u).values())
return s / (n * (n - 1))