what is Dijkstra’s Algorithm and A* search
- User
what is Dijkstra’s Algorithm and A* search
- Assistant
Dijkstra's Algorithm
Overview: Dijkstra's Algorithm is a fundamental algorithm in computer science used to find the shortest paths from a single source vertex to all other vertices in a weighted, directed or undirected graph. It was conceived by Edsger W. Dijkstra in 1956 and published in 1959. The algorithm assumes that all edge weights are non-negative, which ensures that once a vertex has been visited with the shortest possible path, there is no need to revisit it.
How It Works:
-
Initialization:
- Assign to every node a tentative distance value: set it to zero for the initial node and to infinity for all other nodes.
- Set the initial node as current. Mark all other nodes unvisited. Create a set of all the unvisited nodes called the unvisited set.
-
Iteration:
- For the current node, consider all of its unvisited neighbors and calculate their tentative distances through the current node. Compare the newly calculated tentative distance to the current assigned value and assign the smaller one.
- After considering all neighbors of the current node, mark the current node as visited. A visited node will not be checked again.
-
Selection of Next Node:
- If the destination node has been marked visited (when planning a route between two specific nodes) or if the smallest tentative distance among the nodes in the unvisited set is infinity (when there is no connection between the initial node and remaining unvisited nodes), then stop. The algorithm has finished.
- Otherwise, select the unvisited node with the smallest tentative distance and set it as the new current node, then go back to step 2.
Features:
- Non-negative Edge Weights: Dijkstra's Algorithm requires that all edge weights be non-negative to guarantee the optimality of the shortest paths found.
- Greedy Approach: It always selects the node with the least tentative distance, which makes it a greedy algorithm.
- Complexity:
- Using a simple implementation with an array, the time complexity is ( O(V^2) ), where ( V ) is the number of vertices.
- With a min-priority queue (e.g., using a Fibonacci heap), the time complexity can be reduced to ( O(E + V \log V) ), where ( E ) is the number of edges.
Applications:
- Routing and Navigation: Used in GPS devices to find the shortest path.
-