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 18:07:49 +01:00
|
|
|
# dictionary for keeping neighbors of vertexes.
|
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
|
|
|
|
2022-01-15 18:07:49 +01:00
|
|
|
# initialization of needed lists
|
2021-10-17 11:21:09 +02:00
|
|
|
for vertex in point_set:
|
2022-01-15 18:07:49 +01:00
|
|
|
# setting distance of every vertex from the starting vertex to infinity
|
2021-10-17 11:21:09 +02:00
|
|
|
dist[vertex] = float('inf')
|
2022-01-15 18:07:49 +01:00
|
|
|
# setting previous vertex in currently found shortest path for every vertex to None
|
2021-10-17 11:21:09 +02:00
|
|
|
prev[vertex] = None
|
2022-01-15 18:07:49 +01:00
|
|
|
# setting flag for every vertex keeping track if it was already visited my the algorithm
|
2021-10-17 11:21:09 +02:00
|
|
|
visited[vertex] = False
|
|
|
|
|
2022-01-15 18:07:49 +01:00
|
|
|
# pushing start vertex to priority queue
|
2021-10-17 11:21:09 +02:00
|
|
|
heap.heappush(queue, (0, start))
|
2022-01-15 18:07:49 +01:00
|
|
|
# setting distance to start for start vertex to 0
|
2021-11-02 15:17:18 +01:00
|
|
|
dist[start] = 0
|
2022-01-15 18:07:49 +01:00
|
|
|
# setting start vertex not to be added to priority queue again
|
2021-10-17 11:21:09 +02:00
|
|
|
visited[start] = True
|
|
|
|
|
2022-01-15 18:07:49 +01:00
|
|
|
# main loop
|
2021-10-17 11:21:09 +02:00
|
|
|
while len(queue) > 0:
|
2022-01-15 18:07:49 +01:00
|
|
|
# getting first vertex from the priority queue and saving it in current variable
|
2021-10-17 11:21:09 +02:00
|
|
|
current = heap.heappop(queue)
|
2022-01-15 18:07:49 +01:00
|
|
|
# iterating trough all neighbors of the current vertex
|
2021-10-17 11:21:09 +02:00
|
|
|
for neighbor in point_set[current[1]]:
|
2022-01-15 18:07:49 +01:00
|
|
|
# initializing potential distance to be replaced with the current neighbors
|
2021-10-17 11:21:09 +02:00
|
|
|
new_dist = 0
|
2022-01-15 18:07:49 +01:00
|
|
|
#
|
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(dijkstra_algorithm(g, 7, 10))
|
2022-01-18 13:38:48 +01:00
|
|
|
print(dijkstra_algorithm(g2, 1, 4, is_directed=True))
|
2022-01-15 14:29:52 +01:00
|
|
|
print(dijkstra_algorithm(g2, 1, 5, is_directed=True))
|