this one? Dijkstra algorithm is so simple? (Recursive)

this one? The algorithm of Dijkstra and Floyd is so simple? (Recursive)

Introduction to Dijkstra

The Dijkstra algorithm (Dijkstra) was proposed by the Dutch computer scientist Dijkstra in 1959, so it is also called the Dijkstra algorithm. It is the shortest path algorithm from one vertex to the other vertices, and it solves the shortest path problem in the right graph. The main feature of Dijkstra's algorithm is that it starts from the starting point and adopts the strategy of greedy algorithm. Each time it traverses to the adjacent node of the vertex that is closest to the starting point and has not been visited until it expands to the end point.
Insert picture description here
The above is the flow chart of the algorithm. If the reader feels that it is not so easy to understand, then the author shows an example, and everyone can clearly understand the process.
Problem: Find the shortest path from node v1 to node v5.
Insert picture description here

Algorithms and ideas

First, for example, we start from node v0 and find the shortest path to v5. At this point, we set v0 as the starting point, traverse the nodes that v0 can reach, record the node with the shortest distance, and then use this as the starting point to continue traversing until we reach v5. The sum of the distances on this road is the length of the shortest path. You can also refer to the explanation of the flowchart to understand.

Because every time a new starting point is established, the steps in between are repeated, and when the loop reaches the destination, it exits. This is similar to the idea of ​​recursion, so the author uses recursion to achieve it.

Of course, the premise here is that there is a path between the corresponding points. The example cited here is an undirected graph. The algorithmic ideas of directed graphs are also roughly the same, except for a little difference in implementation, but the overall idea is the same. If you have any doubts, you can leave a message in the comment area. I know everything I can to discuss and make progress together.

Don't say much, lookMatlabCode (implemented with other software is also similar):

Code

% Dijkstra算法
global record;
global M
M = [0 1 4 1000 1000 1000
    1 0 2 4 5 1000
    4 2 0 1000 1 1000
    1000 7 1000 0 3 2
    1000 5 1 3 0 6
    1000 1000 1000 2 6 0];
start = 1;  % 起始点
global destination; % 目的地
destination = 6;
record = zeros(1,6);
s = 0;      % 路程
[start,s] = dijkstra(start,s);

function [start,s] = dijkstra(start,s)
% Dijkstra算法
global record;
global M
global destination
temp = s;
k = 0;
tip = start;
for i = 1:6
    if M(start,i) ~= 1000 && s+M(start,i) < 1000 && k ==0 && record(i) == 0
        temp = s + M(start,i);
        k = 1;
        tip = i;
    end
    if M(start,i) ~= 1000 && s+M(start,i) < 1000 && record(i) == 0
        if temp > s + M(start,i)
            temp = s + M(start,i);
            tip = i;
        end
    end
end
record(tip) = 1;
s = temp;
start = tip;
if start == destination
    disp("the result is")
    s
else
    [start,s] = dijkstra(start,s);
end

Of course, the data here only corresponds to this topic. For other situations, readers can change the parameters or improve the code. The code is not very well written, please give me some suggestions.
Below isMatlabThe result of the operation: the
Insert picture description here
reader can also calculate the result by hand, s=9 is correct.

Insert picture description here

Guess you like

Origin blog.csdn.net/wlfyok/article/details/107676986