Interesting pictures (5)(59)

Hello children, hello big friends!

I'm Cat Girl, a primary school student who fell in love with Python programming.

Learn Python with Cat Girl and learn programming together.

today's theme

Let's continue to study the application of graphs today. These algorithms are abstracted from practical problems.

Give an example!

The following 6 cities have different distances between different cities.

As shown in the picture, if you start from Beijing and arrive in Hainan, which route is the shortest?

This requires an understanding of Dijkstra's algorithm (Dijkstra).

Dijkstra's Algorithm

Suppose we need to calculate the shortest distance between any two points in the figure below.

Assuming starting from point a, we assume that the distance from a to other points is infinite.

The first step is to update: update the starting side of a if the distance is greater than infinity.

The second step of updating: from the adjacent nodes of a, find the closest point, that is, point c, and do similar operations.

The distance to point d is 1+2<4, update 4 to 3.

The distance to point f is 1+8<10, update 10 to 9.

Repeat this until all points are visited.

This algorithm is Dijkstra algorithm (Dijkstra).

Python implementation

Code implementation ( full code, see the official account with the same name, the next tweet ):

We use a dictionary to represent the graph, and dictionaries can be nested in the dictionary:

We define a function to implement the core algorithm:

Line 11: G represents the graph, s represents the starting node

Lines 12~14: This list saves the distance from the starting node to this node, except for the distance between itself and itself is 0, others are initialized to infinity

Line 16: dictionary, path, previous node (previous node) passed by the minimum path from the starting node to this node

Line 17: collection, visited nodes

Line 18: In the priority team, the function is to select the smallest one among the multiple edges starting from the current node

Line 21: The number of loops is the number of nodes minus 1, where _ means that you don’t care about the loop variable

Line 22, processing from the adjacent nodes and edges of the current node, one is to see whether the distance between adjacent nodes needs to be updated, and the other is to select the next node

Line 24: If the new path d is smaller than the previous path D[u]

Line 25: Update D[u], D[u] saves the minimum distance from the starting node to this node

Line 26: The predecessor node of the added node u is v, that is ->v->u

Line 27: Put the distance and nodes into the priority queue. From the updated nodes (the path is currently the smallest), in order to select the next node as the current node

Line 28: there is a break in the code, exit the current loop according to the break

Line 29: At this time, the weight and minimum value of v, the minimum path, preselect it

Lines 30~32: v has not been visited, put it into the visited queue

Lines 33~34: If there is an isolated node and is not connected to any node, it will enter this branch, so don’t care about this logic for now

Line 35: return D, P. D represents the minimum distance from the starting node s to the current node, and P represents the path from s to the current node.

function call:

operation result:

The key in the first line of dictionary is the node (abcdef), and the value is the minimum path from a to this node.

The keys and values ​​in the second line of the dictionary are nodes. The predecessor node of 1 is 0, the predecessor node of 2 is 0, and so on.

Have you learned it?

This algorithm is really amazing!

Dijkstra algorithm (Dijkstra) was proposed by Dutch computer scientist Dijkstra in 1959, so it is also called Dijkstra algorithm.

He is a genius programmer who has made great contributions to the development of computers.

Well, let's learn this today!

If you encounter any problems, let's communicate and solve them together.

I'm Cat Girl, see you next time!

Guess you like

Origin blog.csdn.net/parasoft/article/details/131270610