Introduction
Dijkstra's algorithm was proposed by Dutch computer scientist Ezhir Dijkstra in 1956 to solve the shortest path problem in a directed graph. In Dijkstra's algorithm, each segment is given A weight is assigned, and Dijkstra's algorithm finds the path with the smallest total weight
Using Dijkstra's Algorithm
think
The figure below is a weighted group, and the numbers represent the weights
In order to facilitate understanding, we can regard point A as home, point F as the company, and weight as the time spent, so how can we find the shortest route from home to the company?
use
Dijkstra's algorithm consists of 4 steps
- Find the optimal node, that is, the node with the smallest weight value from the current node
- The cost of updating the neighbors of this node
- Repeat this process directly for each node in the graph
- Calculate the final path
Taking Figure 0_1 as an example, let's take a look at the calculation process
Find the optimal node and record the parent node, the unknown node is temporarily considered to be infinite
first step
Base | B | C | D | E | F |
---|---|---|---|---|---|
first step | A | A/3 | A/7 | ∞ | ∞ |
The optimal node is B with a weight of 3
second step
Base | B | C | D | E | F |
---|---|---|---|---|---|
first step | A | A/3 | A/7 | ∞ | ∞ |
second step | B | A/3 | A/7 | B/15 | B/12 |
The optimal node is C with a weight of 7
third step
Base | B | C | D | E | F |
---|---|---|---|---|---|
first step | A | A/3 | A/7 | ∞ | ∞ |
second step | B | A/3 | A/7 | B/15 | B/12 |
third step | C | A/3 | A/7 | B/15 | B/12 |
The optimal node is E with a weight of 12
the fourth step
Base | B | C | D | E | F |
---|---|---|---|---|---|
first step | A | A/3 | A/7 | ∞ | ∞ |
second step | B | A/3 | A/7 | B/15 | B/12 |
third step | C | A/3 | A/7 | B/15 | B/12 |
the fourth step | E | A/3 | A/7 | B/15 | B/12 |
E: 12+4 = 16 < F: 17 < D: 15 + 3 = 18 So the weight of the F node is replaced by 16 The
optimal node is D with a weight of 16