|
| 1 | +from .__init__ import print_msg_box |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | + |
| 6 | +def dijkstra_algorithm(graph, hint=False): |
| 7 | + |
| 8 | + global visited_and_distance |
| 9 | + |
| 10 | + # Providing the graph |
| 11 | + |
| 12 | + vertices = [] |
| 13 | + for x in graph: |
| 14 | + vertices.append([int(y) for y in input().split()]) |
| 15 | + |
| 16 | + edges = [] |
| 17 | + for x in graph: |
| 18 | + edges.append([int(y) for y in input().split()]) |
| 19 | + |
| 20 | + # Find which vertex is to be visited next |
| 21 | + |
| 22 | + to_be_visited() |
| 23 | + |
| 24 | + num_of_vertices = len(vertices[0]) |
| 25 | + |
| 26 | + visited_and_distance = [[0, 0]] |
| 27 | + for i in range(num_of_vertices-1): |
| 28 | + visited_and_distance.append([0, sys.maxsize]) |
| 29 | + |
| 30 | + for vertex in range(num_of_vertices): |
| 31 | + |
| 32 | + # Find next vertex to be visited |
| 33 | + to_visit = to_be_visited() |
| 34 | + for neighbor_index in range(num_of_vertices): |
| 35 | + |
| 36 | + # Updating new distances |
| 37 | + if vertices[to_visit][neighbor_index] == 1 and \ |
| 38 | + visited_and_distance[neighbor_index][0] == 0: |
| 39 | + new_distance = visited_and_distance[to_visit][1] \ |
| 40 | + + edges[to_visit][neighbor_index] |
| 41 | + if visited_and_distance[neighbor_index][1] > new_distance: |
| 42 | + visited_and_distance[neighbor_index][1] = new_distance |
| 43 | + |
| 44 | + visited_and_distance[to_visit][0] = 1 |
| 45 | + |
| 46 | + i = 0 |
| 47 | + |
| 48 | + # Printing the distance |
| 49 | + for distance in visited_and_distance: |
| 50 | + print("Distance of ", chr(ord('a') + i), |
| 51 | + " from source vertex: ", distance[1]) |
| 52 | + i = i + 1 |
| 53 | + |
| 54 | + |
| 55 | +# Function to Find which vertex is to be visited next |
| 56 | + |
| 57 | +def to_be_visited(num_of_vertices): |
| 58 | + v = -10 |
| 59 | + for index in range(num_of_vertices): |
| 60 | + if visited_and_distance[index][0] == 0 \ |
| 61 | + and (v < 0 or visited_and_distance[index][1] <= visited_and_distance[v][1]): |
| 62 | + v = index |
| 63 | + return v |
| 64 | + |
| 65 | + |
| 66 | +def print_Dijkstras_hint(self): |
| 67 | + message = """ |
| 68 | + Dijkstra's algorithm allows us to find the shortest path between any two vertices of a graph. |
| 69 | + -+-+-+-+-+-+-+-+-+-+-+-+-+--+-+-+-+-+-+-+-+-+-+-+-+-+--+-+-+-+-+-+-+-+-+-+-+-+-+--+-+-+-+-+-+-+-+-+ |
| 70 | + |
| 71 | + Dijkstra's Algorithm works on the basis that any subpath Point 2 ⇒ Point 4 of the shortest path Point 1 ⇒ Point 4 between vertices Point 1 and Point 4 is also the shortest path between vertices Point 2 and Point 4. |
| 72 | + Djikstra used this property in the opposite direction i.e we overestimate the distance of each vertex from the starting vertex. Then we visit each node and its neighbors to find the shortest subpath to those neighbors. |
| 73 | + The algorithm uses a greedy approach in the sense that we find the next best solution hoping that the end result is the best solution for the whole problem. |
| 74 | + -+-+-+-+-+--+-+-+-+--+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 75 | +
|
| 76 | + PseudoCode: |
| 77 | +
|
| 78 | + * We need to take care of the trail distance of each vertex. we will store that in an array of size v, where v is that the number of vertices. |
| 79 | +
|
| 80 | + * We also want to be ready to get the shortest path, not only know the length of the shortest path. For this, we map each vertex to the vertex that last updated its path length. |
| 81 | +
|
| 82 | + * Once the algorithm is over, we will backtrack from the destination vertex to the source vertex to seek out the trail . |
| 83 | +
|
| 84 | + * A minimum priority queue are often wont to efficiently receive the vertex with the smallest amount path distance. |
| 85 | + |
| 86 | + Code: |
| 87 | +
|
| 88 | + function dijkstra(G, S) |
| 89 | + for each vertex V in G |
| 90 | + distance[V] <- infinite |
| 91 | + previous[V] <- NULL |
| 92 | + If V != S, add V to Priority Queue Q |
| 93 | + distance[S] <- 0 |
| 94 | + |
| 95 | + while Q IS NOT EMPTY |
| 96 | + U <- Extract MIN from Q |
| 97 | + for each unvisited neighbour V of U |
| 98 | + tempDistance <- distance[U] + edge_weight(U, V) |
| 99 | + if tempDistance < distance[V] |
| 100 | + distance[V] <- tempDistance |
| 101 | + previous[V] <- U |
| 102 | + return distance[], previous[] |
| 103 | +
|
| 104 | + -+-+-+-+-+-+-+-+-+-+--+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |
| 105 | +
|
| 106 | + Complexity: |
| 107 | +
|
| 108 | + Time Complexity: O(E Log V) |
| 109 | + where, E is the number of edges and V is the number of vertices. |
| 110 | +
|
| 111 | + Space Complexity: O(V) |
| 112 | + -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 113 | + |
| 114 | + More Info Here: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm |
| 115 | + """ |
| 116 | + print_msg_box(message) |
0 commit comments