Have you ever heard of pins for villas? Do you know Dijkstra, the greatest computer scientist of our time (47)

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

What is a directed weighted graph?

How to find the minimum path?

How to find the minimum path using Dijkstra's algorithm.

Have you ever heard the story of the pin for the house?

Beautiful deliveryman Kyle Macdonald uses the internet to start an exchange with a red paper clip, and ends up getting back a beautiful duplex apartment for nothing!

The swap path looks like this:

A red paper clip -> a fish pen -> a ceramic doorknob -> an oven -> a generator -> a Budweiser keg -> an old snowmobile -> a convertible -> a record Contract -> One-year residence right for a two-storey villa

here comes the problem

We are going to do a similar topic today. It is not a direct exchange, but an exchange that requires additional money.

Pictured below, you have a pin that you eventually want to change to a motorcycle.

When you exchange a pin for a limited-edition poster, you need to add 5 yuan. Other similar.

How should I change it?

The graph above is called a weighted graph, each edge has a direction, and the number on each edge is called cost or weight.

Directed weighted graph, search for minimum cost route, shortest path algorithm.

It is introduced in detail as the basic content in many professional courses, such as data structure, graph theory, operations research and so on. Note that this algorithm requires that there are no negative weight edges in the graph.


 

Representation of directed graph

We can represent directed graphs as dictionaries in Python:

shortest path

Dijkstra's algorithm is a typical single-source shortest path algorithm, which is used to calculate the shortest path from one node to all other nodes.

Dijkstra's algorithm is a very representative shortest path algorithm.

The main feature is that the starting point is the center and expands outward layer by layer until it reaches the end point.

Dijkstra

Edsger Wybe Dijkstra (May 11, 1930 – August 6, 2002) was a Dutch computer scientist and mathematician, considered one of the most important figures in computer science. He has made outstanding contributions in the fields of programming languages, programming methods, operating systems, distributed systems, concurrency, and software engineering.

In 1951, Dijkstra entered the Free University of Amsterdam to study theoretical physics and received his doctorate in 1956. He was once director of the Netherlands Mathematical Center and has taught at universities in Amsterdam, Illinois, Texas, New South Wales, and more.

Dijkstra is widely regarded as one of the founders of the field of algorithms and data structures. He invented many famous algorithms such as Dijkstra's Algorithm, Banker's Algorithm, and Priority Queue. These algorithms are widely used in graph theory, operating system, network routing and other fields.

In addition, Dijkstra also proposed the concept of structured programming, encouraging programmers to decompose the program into small modules, so that it is easier to understand and maintain. This idea still has a profound influence on the practice of software engineering today.

Dijkstra is also a member of the Royal Netherlands Academy of Sciences and the US National Academy of Engineering, and has won many honors such as the Turing Award and the ACM Distinguished Scientist Award.

On August 6, 2002, after a long battle with cancer, he died at his home in Nuenen, the Netherlands, at the age of 72.

One of the pioneers of computing, he developed a framework for programming.

Edsger Wybe Dijkstra was born in Rotterdam on May 11, 1930. His father, Douwe Wybe Dijkstra, was a chemist, and his mother, Brechtje Cornelia Kruyper, was a mathematician. His career and even his entire life have had a profound impact.

Major achievements

1. Put forward the "goto harmful theory";

2. Propose semaphore and PV primitives;

3. Solved the "Dining Philosophers" problem;

4. Creator of Dijkstra's shortest path algorithm and banker's algorithm;

5. Designer and implementer of the first Algol 60 compiler;

6. Designer and developer of THE operating system;

Along with Knuth, one of the greatest computer scientists of our time.

Many of the above achievements can be learned in college computer textbooks! Required content!

Manual calculation

First, list the pin's cost of reaching each node and the forward node in a table.

For things that cannot be obtained directly, they are all unknowns. As the exchange progresses, there will be fewer and fewer unknowns on this table.

When starting an exchange, choose the item with the least cost each time.

In the picture above, Sea World tickets cost the least, 0.

The first update and forward nodes are as follows (choose the smallest 0):

Pin -> Sea World Ticket -> Scooter: 30

Pin -> Sea World Tickets -> Transformers Toys: 35

The second update and forward nodes are as follows (choose the smallest 5):

Pins -> Limited Edition Poster -> Scooters: 20

Pins -> Limited Edition Poster -> Transformers Toys: 25

To sum up the above two pictures, the current minimum spending is 20 (pin -> limited edition poster -> scooter)

The third update and forward nodes are as follows:

Pin->Limited Edition Poster->Scooter->Motorcycle: It is the smallest path and costs 220.

Pin->Limited Edition Poster->Transformers->Motor: This is the smallest path, costing 300.

Amazing, right?

Implemented in Python

The code is more brain-intensive, you can read it several times, combined with comments, as follows:

The execution results are as follows:

current node: pin, total cost: 0

Current node: Ocean World tickets, total cost: 0

Current node: limited edition poster, total cost: 5

Current node: scooter, total cost: 20

Current node: Transformers toy, total cost: 25

Current node: motorcycle, total cost: 220

Minimum spend: 220

{'Limited Edition Poster': 'Pin', 'Sea World Ticket': 'Pin', 'Scooter': 'Limited Edition Poster', 'Transformers Toys': 'Limited Edition Poster', 'Motorcycle': 'Skateboard car'}

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/130856795