Dijkstra's algorithm

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
Figure 0_1

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

Figure step_1

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

Figure step_2

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

Figure step_3

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

Figure step_4

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324894381&siteId=291194637