-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathshortestPathWithEdge.py
More file actions
46 lines (30 loc) · 1.05 KB
/
shortestPathWithEdge.py
File metadata and controls
46 lines (30 loc) · 1.05 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
from heapq import heappush, heappop
from itertools import permutations
def shortestPathWithEdge(start, finish, weight, graph):
start -= 1
finish -= 1
def dijkstra(source):
visited = set()
shortest = {source: 0}
queue = [(0, source)]
while queue:
curr_dist, curr_loc = heappop(queue)
if curr_loc in visited:
continue
for dest, dist in enumerate(graph[curr_loc]):
if not dist:
continue
new_dist = curr_dist + dist
if new_dist < shortest.get(dest, new_dist + 1):
shortest[dest] = new_dist
heappush(queue, (new_dist, dest))
visited.add(curr_loc)
return shortest
front = dijkstra(start)
back = dijkstra(finish)
temp = front[finish]
for f, b in permutations(range(len(graph)), 2):
if not graph[f][b]:
bridge_dist = front[f] + back[b] + weight
temp = min([temp, bridge_dist])
return temp