-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsourcefile.py
174 lines (119 loc) · 4.55 KB
/
sourcefile.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import heapq
from random import choice
from random import uniform
from time import time
__author__ = 'Rodion "rodde" Efremov'
class HeapEntry:
def __init__(self, node, priority):
self.node = node
self.priority = priority
def __lt__(self, other):
return self.priority < other.priority
def traceback_path(target, parents):
path = []
while target:
path.append(target)
target = parents[target]
return list(reversed(path))
def bi_traceback_path(touch_node, parentsa, parentsb):
path = traceback_path(touch_node, parentsa)
touch_node = parentsb[touch_node]
while touch_node:
path.append(touch_node)
touch_node = parentsb[touch_node]
return path
def bidirectional_dijkstra(graph, source, target):
opena = [HeapEntry(source, 0.0)]
openb = [HeapEntry(target, 0.0)]
closeda = set()
closedb = set()
parentsa = dict()
parentsb = dict()
distancea = dict()
distanceb = dict()
best_path_length = {'value': 1e9}
touch_node = {'value': None}
parentsa[source] = None
parentsb[target] = None
distancea[source] = 0.0
distanceb[target] = 0.0
def update_forward_frontier(node, node_score):
if node in closedb:
path_length = distanceb[node] + node_score
if best_path_length['value'] > path_length:
best_path_length['value'] = path_length
touch_node['value'] = node
def update_backward_frontier(node, node_score):
if node in closeda:
path_length = distancea[node] + node_score
if best_path_length['value'] > path_length:
best_path_length['value'] = path_length
touch_node['value'] = node
def expand_forward_frontier():
current = heapq.heappop(opena).node
closeda.add(current)
for child in graph.get_children_of(current):
if child in closeda:
continue
tentative_score = distancea[current] + graph.get_arc_weight(current, child)
if child not in distancea.keys() or tentative_score < distancea[child]:
distancea[child] = tentative_score
parentsa[child] = current
heapq.heappush(opena, HeapEntry(child, tentative_score))
update_forward_frontier(child, tentative_score)
def expand_backward_frontier():
current = heapq.heappop(openb).node
closedb.add(current)
for parent in graph.get_parents_of(current):
if parent in closedb:
continue
tentative_score = distanceb[current] + graph.get_arc_weight(parent, current)
if parent not in distanceb.keys() or tentative_score < distanceb[parent]:
distanceb[parent] = tentative_score
parentsb[parent] = current
heapq.heappush(openb, HeapEntry(parent, tentative_score))
update_backward_frontier(parent, tentative_score)
while opena and openb:
tmp = distancea[opena[0].node] + distanceb[openb[0].node]
if tmp >= best_path_length['value']:
return bi_traceback_path(touch_node['value'], parentsa, parentsb)
if len(opena) + len(closeda) < len(openb) + len(closedb):
expand_forward_frontier()
else:
expand_backward_frontier()
return []
def path_cost(graph, path):
cost = 0.0
for i in range(len(path) - 1):
tail = path[i]
head = path[i + 1]
if not graph.has_arc(tail, head):
raise Exception("Not a path.")
cost += graph.get_arc_weight(tail, head)
return cost
def main():
graph, node_list = create_random_digraph(1000000, 5000000, 10.0)
source = choice(node_list)
target = choice(node_list)
del node_list[:]
print("Source:", source)
print("Target:", target)
start_time = time()
path1 = dijkstra(graph, source, target)
end_time = time()
print("Dijkstra's algorithm in", 1000.0 * (end_time - start_time), "milliseconds.")
start_time = time()
path2 = bidirectional_dijkstra(graph, source, target)
end_time = time()
print("Bidirectional Dijkstra's algorithm in", 1000.0 * (end_time - start_time), "milliseconds.")
print("Paths are identical:", path1 == path2)
print("Dijkstra path:")
for node in path1:
print(node)
print("Path length:", path_cost(graph, path1))
print("Bidirectional path:")
for node in path2:
print(node)
print("Path length:", path_cost(graph, path2))
if __name__ == "__main__":
main()