Dijkstra最短路徑演算法

Dijkstra's shortest path algorithm is a graph traversal algorithm that is used to find the shortest paths from a single source vertex to all other vertices in the graph. It is a greedy algorithm that means it makes the locally optimal choice at each step, which leads to a global optimum.

Here's a step-by-step explanation of how Dijkstra's algorithm works:

  1. Initialization:

    • Choose a starting vertex (or node), called the source.
    • Assign a tentative distance value to every vertex in the graph, known as the distance estimate. Initially, the distance estimate for the source is 0, and the distance estimate for all other vertices is infinity.
  2. Create a set of unvisited vertices:

    • Initially, all vertices are unvisited.
  3. While there is a unvisited vertex with a distance estimate less than infinity:

    • Choose a vertex with the smallest distance estimate.
    • Mark it as visited.
    • For each unvisited neighbor of the chosen vertex:
      • Calculate the distance to the neighbor through the chosen vertex.
      • If the calculated distance is less than the current distance estimate, update the distance estimate.
  4. Repeat step 3 until all vertices are visited or the target vertex is visited:

    • Once a vertex is visited, it is removed from the unvisited set.
  5. The shortest path from the source to a particular vertex is the path corresponding to the shortest distance from the source to that vertex:

    • The shortest path can be reconstructed by backtracking from the target vertex to the source.

Here's a simple example of Dijkstra's algorithm on a small graph:

Graph:
1 -- 2 -- 3 -- 4
   |   |   |   |
   5   6   7   8

Dijkstra's algorithm starting from vertex 1:

1. Initialize distances:
   1: 0
   2: ∞
   3: ∞
   4: ∞
   5: ∞
   6: ∞
   7: ∞
   8: ∞

2. Mark 1 as visited and update distances to its neighbors:
   1: 0
   2: 1
   3: ∞
   4: ∞
   5: ∞
   6: ∞
   7: ∞
   8: ∞

3. Mark 2 as visited and update distances to its neighbors:
   1: 0
   2: 1
   3: 2 (1 + 1)
   4: ∞
   5: ∞
   6: ∞
   7: ∞
   8: ∞

4. Mark 3 as visited and update distances to its neighbors:
   1: 0
   2: 1
   3: 2
   4: 3 (2 + 1)
   5: ∞
   6: ∞
   7: ∞
   8: ∞

5. Mark 4 as visited and update distances to its neighbors:
   1: 0
   2: 1
   3: 2
   4: 3
   5: ∞
   6: ∞
   7: ∞
   8: ∞

6. Mark 5 as visited and update distances to its neighbors:
   1: 0
   2: 1
   3: 2
   4: 3
   5: 4 (2 + 2)
   6: ∞
   7: ∞
   8: ∞

7. Mark 6 as visited and update distances to its neighbors:
   1: 0
   2: 1
   3: 2
   4: 3
   5: 4
   6: 5 (1 + 4)
   7: ∞
   8: ∞

8. Mark 7 as visited and update distances to its neighbors:
   1: 0
   2: 1
   3: 2
   4: 3
   5: 4
   6: 5
   7: 6 (2 + 4)
   8: ∞

9. Mark 8 as visited and update distances to its neighbors:
   1: 0
   2: 1
   3: 2
   4: 3
   5: 4
   6: 5
   7: 6
   8: 7 (3 + 4)

All vertices are visited, and the shortest path from 1 to all other vertices is found.

Dijkstra's algorithm is very efficient for finding the shortest path in a graph with non-negative edge weights. If the graph has negative edge weights, Dijkstra's algorithm may not work correctly, and other algorithms like Bellman-Ford or A* may be used instead.