Computer Network Fundamentals (10)---Network Layer-Dijkstra's Algorithm

Article content overview

Dijkstra algorithm

  • Dijkstra (Dijkstra) algorithm is a famous graph theory algorithm
  • Dijkstra algorithm solves the shortest path problem of a weighted graph from one node to other nodes
  • Features: "Centering on the starting point, expanding to outer layers"

Shortest path problem

Suppose there is a network like the following figure. The network has several nodes A, B, C, D, E, F and several edges, and each edge has a corresponding distance. Assuming that the shortest path from A to E is required at this time

Listed below are all possible paths and path lengths from A to E

A->B->C->E = 13
A->C->E = 11
A->D->C->E =13
A->D->E = 9
A->F->E = 10

From the results listed, the shortest path is A->D->E, and the distance is 9. This is artificially searched, but we need to convert it into a programming language to solve the shortest path problem

First introduce the entire process of the algorithm in text, and then explain each process through examples

Dijkstra algorithm

Dijkstra algorithm process description

  1. Initialize two sets (S, U) (S is the set with only the initial vertex point A, and U is the set of other vertices)
  2. If U is not empty, sort the vertices of U set by distance, and take the vertex D closest to A
  • 将顶点D放入S集合
    
  • 更新通过顶点D到达U集合所有点的距离(如果距离更小则更新,否则不更新)
    
  • 重复步骤2
    
  1. Until U set is empty, the whole algorithm process is completed

Understand the above process through the example below

Because the shortest distance from A to E is required, A is included in the S set first, and the distance from A to A is 0. Then the set of other vertices U is B, C, D, E, F, and calculate the distance from A to these vertices, as can be seen from the figure

Because the set U is not empty, sort the distances from A to each vertex in the set U, find the vertex with the shortest distance to A, and put it into the set S. It can be seen from the figure that the distance from A to vertex B is the smallest, so put it into the set S

Then you should calculate: The distance between A and B to reach the remaining vertices

Because the distance from B to C is known to be 5, the distance from A to C through B is: A->B->C = 6+5 = 11, because the original distance from A to C in the set U is 9, 9 <11 , So there is no need to update to the set U, and then get the following result

Then judge U and find that it is not empty. Therefore, sort the distances from A to each vertex in U and find that the distance from A to F is the shortest. At this time, put F into the S set. Then calculate the distance between A and F to reach each vertex at this time. It is found that the distance from A to E through F is 10. At this time, the distance from A to vertex E in U is updated (this distance is not known originally), and the following result is obtained

Then repeat the above steps and find that the shortest distance in U is D, then put D into the set S, and then calculate the distance of A to each vertex through D, and find that the distance from A to C through D is 11, and A through The distance from D to E is 9. At this time, it is found that the distance from A to C through D is greater than the distance from A to C in U, so the distance from A to C in U is not replaced. Then it is found that the distance from A to E through D is 9, which is less than the distance from A to E in U of 10, so replace the value of A to E in U, then the following result is obtained

Then there are two elements left in the set U (the two distance values ​​are the same, choose one to put into the set S), and then repeat the above steps until the set U is empty. The final result is as follows

The distance in the S set obtained is the shortest distance from A to each vertex. The above is the entire process of Dijkstra's algorithm. If you are interested, you can also push a B to the shortest distance to each vertex

It is the core competitiveness of a technical person to find the constant in the rapidly changing technology. Unity of knowledge and action, combining theory with practice

Guess you like

Origin blog.csdn.net/self_realian/article/details/107633620