{"id":216,"date":"2023-04-04T15:32:47","date_gmt":"2023-04-04T15:32:47","guid":{"rendered":"https:\/\/vmlogger.com\/algorithms\/?p=216"},"modified":"2023-04-04T17:10:14","modified_gmt":"2023-04-04T17:10:14","slug":"dijkstras-shortest-path-algorithm","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/algorithms\/2023\/04\/04\/dijkstras-shortest-path-algorithm\/","title":{"rendered":"Dijkstra’s shortest path algorithm"},"content":{"rendered":"
Dijkstra’s shortest path algorithm is a widely used algorithm in computer science that finds the shortest path between nodes in a graph with non-negative edge weights. It was developed by Edsger W. Dijkstra in 1956 and has since become a fundamental algorithm in network routing protocols, map applications, and many other areas of computer science. In this article, we will discuss the working of the algorithm, its implementation, and some interesting facts behind it.<\/p>\n
Dijkstra’s algorithm works by maintaining a set of unexplored nodes and a tentative distance for each node. Initially, all nodes have a tentative distance of infinity, except for the starting node which has a tentative distance of zero. The algorithm then selects the node with the smallest tentative distance and explores all its neighbors. For each neighbor, the algorithm updates its tentative distance if the new distance is smaller than the current tentative distance. This process continues until the destination node is explored or all nodes have been explored.<\/p>\n
Dijkstras Algorithm Animation (Source: Wiki)<\/p><\/div>\n
The algorithm can be implemented using a variety of data structures, but the most common implementation uses a priority queue to keep track of unexplored nodes sorted by their tentative distances. The algorithm can also be implemented using an array. This is the most efficient implementation of Dijkstra’s algorithm using the minHeap<\/a> data structure. <\/p>\n The time complexity of this implementation is
\nHere is an implementation of Dijkstra’s algorithm using Python in both data structures – minHeap<\/a> and Array.<\/p>\nDijkstra’s Algorithm implementation using
minHeap<\/code><\/h2>\n
Info:<\/span><\/h4>\n
\nO((V+E)*log V) <\/code>
\nwhere: <\/code>
\n V – is the number of Vertexes (total number of cities) and E – is the total number of connections [or edges].<\/p>\n
\nVertex:<\/code> Vertex is a point where two or more edges or lines meet in a graph.
\nEdge:<\/code> Edges are connecting lines in a graph.\n<\/div>\n
\r\n\r\ndef dijkstraUsingHeap(connections, start):\r\n \"\"\"\r\n Dijkstra's algorithm using minHeap Data Strcuture\r\n\r\n Parameters:\r\n ---------------\r\n connections : nested dictionary with all possible connections and distances [weight]\r\n start : starting city name [starting node]\r\n\r\n returns:\r\n ---------------\r\n returns a dictionary with the shortest distances to reach each place from starting city\r\n\r\n \"\"\"\r\n connections = self.connections\r\n start = self.start\r\n # first all distances to infinity in the disctionary\r\n distances = {connection: float(\"inf\") for connection in connections}\r\n # distance from start to start point = 0\r\n distances[start] = 0\r\n minHeap = [(0, start)]\r\n\r\n # loop through until heap is empty\r\n # heap will be empty when all the cities are traversed\r\n # once which are reachable from starting city.\r\n\r\n while minHeap:\r\n current_distance, current_city = heapq.heappop(minHeap)\r\n # if min distance is already found - do nothing - continue\r\n if current_distance > (distances[current_city]):\r\n continue\r\n # else get the distances and find the minimum one\r\n for childCity, distance in connections[current_city].items():\r\n NewDistance = current_distance + distance\r\n if NewDistance < distances[childCity]:\r\n distances[childCity] = NewDistance\r\n heapq.heappush(minHeap, (NewDistance, childCity))\r\n return distances\r\n<\/pre>\n
Dijkstra's Algorithm implementation using
Array<\/code><\/h2>\n