Dijkstra's Algorithm is a method for finding the shortest path between nodes in a graph, akin to navigating the shortest route between cities on a map.
The Big Picture
Imagine you have a map with various cities connected by roads. You want to find the shortest path from your home city to a destination city. Dijkstra's Algorithm helps you determine this shortest path by systematically exploring routes from the starting point, considering the smallest distance to reach each city, and updating paths as shorter routes are found.
Core Concepts
- Graph: A collection of nodes (vertices) and edges (paths) connecting pairs of nodes.
- Weights: Values assigned to each edge representing the cost or distance between nodes.
- Priority Queue: A data structure used to manage the nodes to be explored based on their current shortest distance from the starting node.
- Relaxation: The process of updating the shortest path estimate to a node when a shorter path is found.
Detailed Walkthrough
Initialization:
- Assign a tentative distance value to every node: set it to zero for the initial node and to infinity for all other nodes.
- Set the initial node as current and mark all other nodes as unvisited.
Visit Unvisited Nodes:
- For the current node, consider all its unvisited neighbors. Calculate their tentative distances through the current node.
- If this tentative distance is less than the currently assigned value, update the shortest distance.
- Once considered, mark the current node as visited. A visited node will not be checked again.
Select the Next Node:
- From the unvisited nodes, select the node that is marked with the smallest tentative distance and set it as the new current node.
- Repeat the process until the destination node is marked visited or all nodes have been visited.
Understanding Through an Example
Let's say we have the following graph:
- Nodes: A, B, C, D, E
- Edges with weights:
- A-B (4), A-C (2)
- B-C (5), B-D (10)
- C-E (3)
- D-E (4)
We want to find the shortest path from A to E.
Initialization:
- A: 0 (starting point)
- B: ∞
- C: ∞
- D: ∞
- E: ∞
First Iteration (starting at A):
- From A, we update B and C:
- B = min(∞, 0 + 4) = 4
- C = min(∞, 0 + 2) = 2
- Current distances: A (0), B (4), C (2), D (∞), E (∞)
- Mark A as visited.
- From A, we update B and C:
Second Iteration (current node C):
- From C, update E:
- E = min(∞, 2 + 3) = 5
- Current distances: A (0), B (4), C (2), D (∞), E (5)
- Mark C as visited.
- From C, update E:
Third Iteration (current node B):
- From B, update D:
- D = min(∞, 4 + 10) = 14 (not updated since next current node with smallest tentative distance is E)
- Current distances: A (0), B (4), C (2), D (14), E (5)
- Mark B as visited.
- From B, update D:
Fourth Iteration (current node E):
- No updates since E is the destination.
- Current distances: A (0), B (4), C (2), D (14), E (5)
- Mark E as visited.
Conclusion and Summary
Dijkstra's Algorithm effectively finds the shortest path in a weighted graph by:
- Initializing distances and setting the starting node's distance to zero.
- Exploring neighboring nodes and updating paths as shorter routes are found.
- Using a priority queue to always expand the least costly node next.
- Continuing this process until the shortest path to the destination is found.
Test Your Understanding
- Explain the purpose of the priority queue in Dijkstra's Algorithm.
- What happens if the graph contains a negative weight edge?
- Can Dijkstra's Algorithm be used on graphs with negative weights? Why or why not?
Reference
For further reading, refer to:
'200===Dev Language > DS And Algorithm' 카테고리의 다른 글
Merge Sort Introduced (0) | 2024.05.27 |
---|---|
Merge Sort Introduced (0) | 2024.05.27 |
Insertion Sort Introduced (0) | 2024.05.27 |
Bellman-Ford Algorithm (0) | 2024.05.27 |
자료구조 알고리즘 (배열 연산) (0) | 2020.11.24 |