2021-10-17 11:21:09 +02:00
|
|
|
import heapq as heap
|
|
|
|
|
|
|
|
|
|
|
|
# function which takes graph, vertex set, start and goal, uses djikstra algorithm to establish shortest paths from start
|
|
|
|
# every other vertex in graph and returns shortest path between start and goal
|
2022-01-15 14:29:52 +01:00
|
|
|
def dijkstra_algorithm(graph, start, goal, is_directed=False):
|
2021-10-17 11:21:09 +02:00
|
|
|
# dictionary which will store for every vertex it's distance to start
|
|
|
|
dist = dict()
|
|
|
|
|
|
|
|
# dictionary which keeps if vertex was already popped from queue
|
|
|
|
visited = dict()
|
|
|
|
|
|
|
|
# dictionary which keeps for every vertex which other vertex is previous on the shortest path from start
|
|
|
|
prev = dict()
|
|
|
|
|
|
|
|
# list which is used to keep priority queue with vertexes to be explored by the algorithm
|
|
|
|
queue = []
|
|
|
|
|
2022-01-15 14:29:52 +01:00
|
|
|
point_set = dict()
|
|
|
|
for arc in graph.keys():
|
|
|
|
point_set[arc[0]] = []
|
|
|
|
point_set[arc[1]] = []
|
|
|
|
|
2021-10-17 11:21:09 +02:00
|
|
|
# creating dictionary which for every vertex keeps all it's neighbors
|
2022-01-15 14:29:52 +01:00
|
|
|
for arc in graph.keys():
|
|
|
|
point_set[arc[0]].append(arc[1])
|
|
|
|
if not is_directed:
|
|
|
|
point_set[arc[1]].append(arc[0])
|
2021-10-17 11:21:09 +02:00
|
|
|
|
|
|
|
for vertex in point_set:
|
|
|
|
dist[vertex] = float('inf')
|
|
|
|
prev[vertex] = None
|
|
|
|
visited[vertex] = False
|
|
|
|
|
|
|
|
heap.heappush(queue, (0, start))
|
2021-11-02 15:17:18 +01:00
|
|
|
dist[start] = 0
|
2021-10-17 11:21:09 +02:00
|
|
|
visited[start] = True
|
|
|
|
|
|
|
|
while len(queue) > 0:
|
|
|
|
current = heap.heappop(queue)
|
|
|
|
for neighbor in point_set[current[1]]:
|
|
|
|
new_dist = 0
|
2022-01-15 14:29:52 +01:00
|
|
|
if not is_directed:
|
|
|
|
if neighbor > current[1]:
|
|
|
|
new_dist = dist[current[1]] + graph[(neighbor, current[1])]
|
|
|
|
else:
|
|
|
|
new_dist = dist[current[1]] + graph[(current[1], neighbor)]
|
2021-10-17 11:21:09 +02:00
|
|
|
else:
|
|
|
|
new_dist = dist[current[1]] + graph[(current[1], neighbor)]
|
|
|
|
if new_dist < dist[neighbor]:
|
|
|
|
dist[neighbor] = new_dist
|
2021-11-02 15:17:18 +01:00
|
|
|
for k in range(len(queue)):
|
|
|
|
if queue[k][1] == neighbor:
|
|
|
|
queue[k] = (new_dist, neighbor)
|
|
|
|
heap.heapify(queue)
|
2021-10-17 11:21:09 +02:00
|
|
|
prev[neighbor] = current[1]
|
|
|
|
|
|
|
|
if not visited[neighbor]:
|
|
|
|
visited[neighbor] = True
|
|
|
|
heap.heappush(queue, (new_dist, neighbor))
|
|
|
|
temp = goal
|
|
|
|
shortest_path = [goal]
|
|
|
|
while prev[temp] is not None:
|
|
|
|
shortest_path.append(prev[temp])
|
|
|
|
temp = prev[temp]
|
2021-11-02 15:17:18 +01:00
|
|
|
shortest_path.reverse()
|
2021-10-17 11:21:09 +02:00
|
|
|
return shortest_path, dist[goal]
|
2021-10-08 18:16:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-10-17 11:21:09 +02:00
|
|
|
g = {
|
|
|
|
(2, 1): 3,
|
|
|
|
(3, 2): 2,
|
|
|
|
(5, 3): 1,
|
|
|
|
(9, 5): 5,
|
|
|
|
(10, 9): 4,
|
|
|
|
(9, 4): 3,
|
|
|
|
(4, 1): 4,
|
|
|
|
(7, 1): 6,
|
|
|
|
(3, 1): 4,
|
|
|
|
(6, 2): 3,
|
|
|
|
(8, 6): 8,
|
2021-11-02 15:17:18 +01:00
|
|
|
(8, 3): 2,
|
|
|
|
(9, 1): 8
|
2021-10-17 11:21:09 +02:00
|
|
|
}
|
|
|
|
|
2022-01-15 14:29:52 +01:00
|
|
|
g2 = {
|
|
|
|
(4, 1): 6,
|
|
|
|
(1, 3): 4,
|
|
|
|
(1, 2): 2,
|
|
|
|
(2, 4): 5,
|
|
|
|
(3, 4): 1,
|
|
|
|
(5, 3): 1
|
|
|
|
}
|
2021-10-17 11:21:09 +02:00
|
|
|
|
2022-01-15 14:29:52 +01:00
|
|
|
print('')
|
|
|
|
print(dijkstra_algorithm(g, 7, 10))
|
|
|
|
print(dijkstra_algorithm(g2, 1, 5, is_directed=True))
|