-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateEdgeDist.py
73 lines (53 loc) · 2.09 KB
/
calculateEdgeDist.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
71
72
73
import shortestDistances as sd
import buildNetworkX as bnx
def calculateEdgeDist(G, edges, nodes, sources, length):
#return value
distances = {}
i = 1
#convert candidate edges to list
#find shortest distances adding each edge
for edge in range(len(edges)):
G.add_edge(edges[edge][0], edges[edge][1])
result = sd.shortestDistances(G, sources)
i += 1
#update edge distances for each node
for node in range(len(nodes)):
if nodes[node] in result:
distanceOnEdge = result[nodes[node]]
#add edge to node not yet in distances
if not node in distances:
distances[node] = {}
if distanceOnEdge <= length:
distances[node][distanceOnEdge] = [edge]
#add edge to node already in distances
else:
if distanceOnEdge <= length:
#new distance for node
if not distanceOnEdge in distances[node]:
distances[node][distanceOnEdge] = [edge]
#add aedge to distance for node
else:
distances[node][distanceOnEdge].append(edge)
G.remove_edge(edges[edge][0], edges[edge][1])
return distances
def getNodeNeighbors(nodes, edges):
nodeNeighbors = {}
for node in range(len(nodes)):
nodeNeighbors[node] = []
for edge in range(len(edges)):
if edges[edge][0] == nodes[node]:
nodeNeighbors[node].append(edge)
return nodeNeighbors
def test():
adjacencyLists = {}
adjacencyLists['1'] = {'2', '3'}
adjacencyLists['2'] = {'1', '5'}
adjacencyLists['3'] = {'2', '4'}
G = bnx.buildNetworkXFromAM(adjacencyLists)
nodes = ['1','2','3','4','5']
edges = [('1', '4'), ('1', '5'), ('3', '5')]
sources = ['1']
result = calculateEdgeDist(G, edges, nodes, sources, 3)
print(result)
result = getNodeNeighbors(nodes, edges)
print (result)