Python data structure --- the shortest path algorithm (Dijkstra algorithm and Freud algorithm)

Shortest path algorithm (Dijkstra algorithm and Freud algorithm)

Dijkstra's Algorithm

In a graph G with weights, given a source point V, find the shortest path from V to the rest of the vertices in the graph G.

For example, in the figure below, starting from vertex 1, the shortest path between each vertex.

img

First, an S set records the vertices of the shortest path from vertex 1 that has been found, and dis[i] represents the shortest distance from vertex 1 to vertex i.

S = {1,}

S\vertex 2 3 4 5 6 7 8
{1,5,} 3 2 1
S = {1,5,3,} 3 2 5 4
S = {1,5,3,2,} 3 3 5 4
S = {1,5,3,2,4,} 3 5 5 4
S = {1,5,3,2,4,8,} 5 5 4
S = {1,5,3,2,4,8,6,} 5 5
S = {1,5,3,2,4,8,6,7} 5

We get dis=[0, 3, 2, 3, 1, 5, 5, 4] , so the shortest distances from vertex 1 to vertex 1 2 3 4 5 6 7 8 are 0, 3, 2, 3, 1 , 5, 5, 4.

Specific steps

First; S = {1,}

first step:

Find the distance between all the vertices and 1, if it cannot be reached, it is ∞, see the second row of the above table. The vertex corresponding to the smallest number in the middle is the vertex with the shortest distance from vertex 1, which is 5, and is added to the set S.

S = {1,5}

The second step:

Starting from 5, find the shortest distance between 1->5-> all other vertices. If it is smaller than the originally recorded value, update it, otherwise it will not change.

For example, at vertex 6, starting from 1 to 6 is ∞, now it changes from 1->5->6 to 5, so it is updated to 5.

If 1->5-> the vertex is unreachable, the original value remains unchanged.

Find the vertex corresponding to the minimum value 2 is 3, and add it to the set of vertices with the shortest distance found

S = {1,5,3}

third step:

Starting from vertex 3, the same is true. Let’s talk about vertex 6, 1->3->6, the distance is 6. There is already a distance of 5, 6>5, so it’s not updated.

At this time, from the two minimums, we can choose one vertex. For example, select vertex 2.

S = {1,5,3,2,}

Next

S = {1,5,3,2,4}

S = {1,5,3,2,4,8,}

S = {1,5,3,2,4,8,6,}

S = {1,5,3,2,4,8,6,7}

dis=[0, 3, 2, 3, 1, 5, 5, 4]

Practice questions

Given a graph as shown above, the edges in the graph represent the distance between two nodes. If the Dijkstra algorithm is used to find the shortest path between node 1 and node 8, then when the calculation is completed, nodes 1 to What is the shortest path to node 8? At the same time, when the shortest path calculation from node 1 to node 8 is completed, the shortest path from node 1 to which nodes (except 1 and 8) have also been calculated?

A. The shortest path: 4; the shortest node that has been calculated: 5

B. The shortest path: 4; the nodes that have been calculated as the shortest path: 2, 3, 4, 5

C. The shortest path: 4; the nodes that have been calculated as the shortest path: 5,6

D. The shortest path: 7; the nodes that have been calculated as the shortest path: 3, 5, 6

Reference answer (B)

Analysis: When finding the shortest distance of vertex 8 S = {1,5,3,2,4,8,}, the sixth row of the above table shows that the shortest distance is 4. The shortest node has been calculated: 2,3,4,5 divided by 1,8.

Python code implementation

To be added

Freud Algorithm

Guess you like

Origin blog.csdn.net/qq_31910669/article/details/115215493