Solving the shortest path in graph theory - teaching you mathematical modeling


Many friends will encounter the shortest path problem when learning graph theory or mathematical modeling. This lecture will start with how to make a graph, and teach you the shortest path problem in graph theory. Depending on the graph, we will introduce two different algorithms: Dijkstra's algorithm and Bellman‐Ford's algorithm.

How to draw a picture?

  1. Online graphing: https://csacademy.com/app/graph_editor/

  2. MatLab plotting:

    % 函数graph(s,t):可在 s 和 t 中的对应节点之间创建边,并生成一个图
    G1 = graph(s1, t1);
    plot(G1)  % plot函数用于MatLab中图的展示
    
    % 函数graph(s,t,w):可在 s 和 t 中的对应节点之间以w的权重创建边,并生成一个图
    G2 = graph(s2, t2);
    plot(G2, 'linewidth', 2)  % 设置线的宽度
    
    % 下面的命令是在画图后不显示坐标
    set( gca, 'XTick', [], 'YTick', [] ); 
    
    % 有权重的图(如 s = [1,2,3,4]; t = [2,3,1,1]; w = [3,8,9,2]; )
    G3 = graph(s3, t3, w)
    plot(G, 'EdgeLabel', G.Edges.Weight, 'linewidth', 2)  % 同时展示权重
    set( gca, 'XTick', [], 'YTick', [] );  
    

The above are all undirected graphs. To make a directed graph, you only need to change graph to digraph.

Note: The graph made by Matlab is not very beautiful. If there are few nodes, it is recommended to use online graphing

shortest path algorithm

Dijkstra's Algorithm - Greedy Algorithm

  • Principle : Divide the nodes into whether they are visited or not, and increase the shortest path between visited nodes and unvisited nodes each time.

  • Scope of application : directed graph, undirected graph.

  • Cons : Cannot handle negative weights.

Bellman‐Ford algorithm

  • Principle : The cycle is used to update the weight, and every cycle, the Bellman-Ford algorithm will update the information of all nodes. (No longer distinguishing nodes as visited or not)

  • Scope of application : Directed graphs with negative weights are supported , but graphs with negative weighted circuits are not supported.

  • Cons : slower runtime

Matlab function solver

Calculate the shortest path

[P,d] = shortestpath(G,start,end [,'Method',algorithm] )  %注意:该函数matlab2015b之后才有哦

Function: Return the shortest path from the start node to the end node in graph G

Input parameters:
(1) G - input graph (graph object - undirected graph | digraph object - directed graph)
(2) start node
(3) end target node
(4) [,'Method' ,algorithm] is an optional parameter, indicating the algorithm for calculating the shortest path. Generally, we don't need to set it manually, the default is "auto". MatLab will automatically match the corresponding algorithm for us

Output parameters:
(1) P - the node that the shortest path passes through
(2) d - the shortest distance

Note: The graph nodes in Matlab should be numbered from 1, so all 0 should be changed to 9

Returns the distance matrix between any two points

d = distances(G [,'Method',algorithm])  %注意:该函数matlab2015b之后才有哦

Find all points within a given range

[nodeIDs,dist] = nearest(G,s,d [,'Method',algorithm])  %注意:该函数matlab2016a之后才有哦

Returns all nodes in graph G that are within distance d of node s.
nodeIDs are the eligible nodes
Dist is the distance of these nodes from s

Come to example

topic

The figure below shows a one-way traffic network, and the numbers next to each arc indicate the cost of passing this line. Now someone wants to start from v1, go to v8 through this traffic network, and seek the travel route that minimizes the total cost.
One-way traffic network

answer

  • code:
s = [1 1 1 2 3 3 4 5 5 5 5 6 6 7 9 9]; % 在 s 和 t 中的对应节点之间创建边
t = [2 3 4 5 2 4 6 4 6 7 8 5 7 8 5 8];
w = [6 3 1 1 2 2 10 6 4 3 6 10 2 4 2 3]; % 线路费用 —— 边的权重

G = digraph(s, t, w);  % 生成有向图
myplot = plot(G, 'EdgeLabel', G.Edges.Weight, 'linewidth', 2) % 通过MatLab画图
set( gca, 'XTick', [], 'YTick', [] );  % 画图后不显示坐标

[P,d] = shortestpath(G, 1, 8) % 求出最短路径(P)及其距离(d)
highlight(myplot, P, 'EdgeColor', 'r') % 高亮最短路径
  • Answer:
P 	=	1     3     2     5     8

d 	=	12
  • MatLab outputs graphical results:
    output result

Guess you like

Origin blog.csdn.net/qq_61539914/article/details/126330348