Created
March 23, 2023 15:19
-
-
Save victusfate/a3738c1a4cbbe45c7b8b6fb50ddeeede to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dijkstra's algorithm is a popular algorithm used to find the shortest path between two nodes in a graph. It is a greedy algorithm that iteratively expands the frontier of visited nodes from the starting node towards the target node, while updating the cost of each node. The algorithm maintains a priority queue of nodes to visit, with the node with the lowest current cost at the front of the queue. | |
Here are the basic steps of Dijkstra's algorithm: | |
Initialize the cost of the starting node to 0, and the cost of all other nodes to infinity. | |
Add the starting node to the priority queue. | |
While the priority queue is not empty: | |
Remove the node with the lowest cost from the priority queue (call it "current"). | |
For each neighbor of "current": | |
Calculate the cost of getting to that neighbor through "current". | |
If this cost is lower than the current cost of the neighbor, update the cost of the neighbor to the lower value. | |
Add the neighbor to the priority queue if it is not already there. | |
When the target node is removed from the priority queue, the shortest path has been found. | |
Dijkstra's algorithm guarantees that the shortest path is found when all edge weights are non-negative. However, if there are negative edge weights, the algorithm may not find the correct shortest path. In such cases, Bellman-Ford algorithm can be used instead. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import heapq | |
graph = { | |
'A': {'B': 5, 'C': 1}, | |
'B': {'A': 5, 'C': 2, 'D': 1}, | |
'C': {'A': 1, 'B': 2, 'D': 4, 'E': 8}, | |
'D': {'B': 1, 'C': 4, 'E': 3, 'F': 6}, | |
'E': {'C': 8, 'D': 3}, | |
'F': {'D': 6} | |
} | |
def dijkstra(graph, start): | |
distances = {node: float('inf') for node in graph} | |
distances[start] = 0 | |
queue = [(0, start)] | |
while queue: | |
current_distance, current_node = heapq.heappop(queue) | |
if current_distance > distances[current_node]: | |
continue | |
for neighbor, weight in graph[current_node].items(): | |
distance = current_distance + weight | |
if distance < distances[neighbor]: | |
distances[neighbor] = distance | |
heapq.heappush(queue, (distance, neighbor)) | |
return distances | |
print(dijkstra(graph,'A')) | |
# chatgpt predicted output | |
# {'A': 0, 'B': 5, 'C': 1, 'D': 6, 'E': 9, 'F': 12} | |
# actual output | |
# {'A': 0, 'B': 3, 'C': 1, 'D': 4, 'E': 7, 'F': 10} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment