Mathematical Modeling: 8 Graph Theory

Online drawing webpage:
        https://csacademy.com/app/graph_editor/
       The limitation of Dijkstra is that it cannot handle graphs with negative weights.
        Floyd and Bellman-Ford algorithms can solve graphs with negative weights (conditionally):
  • Undirected graph with negative edges - unsolvable (if you walk infinitely on the negative edges, it will be infinitely small)
  • There is a ring, there are negative edges in the ring, and the sum of the weights of the edges in the ring is negative - unsolvable (going in circles on the ring, infinitely small)
       Dijkstra, Bellman-Ford: single source shortest path
       Bellman-Ford: the shortest path between any two points

Matlab plotting

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%无向图graph
% 点的编号必须从1开始连续编号
s1 = [1,2,3,4];
t1 = [2,3,1,1];
G1 = graph(s1, t1);
plot(G1)

s2 = {'学校','电影院','网吧','酒店'};
t2 = {'电影院','酒店','酒店','KTV'};
G2 = graph(s2, t2);

% 设置线的宽度
plot(G2, 'linewidth', 2)  

% 下面的命令是在画图后不显示坐标
set( gca, 'XTick', [], 'YTick', [] ); 

% 有权重
w = [3,8,9,2];
G = graph(s, t, w);
% 在边上标明权重
plot(G, 'EdgeLabel', G.Edges.Weight, 'linewidth', 2) 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%有向图digraph
% 有权图 digraph(s,t,w)
s = [1,2,3,4];
t = [2,3,1,1];
w = [3,8,9,2];
G = digraph(s, t, w);
plot(G, 'EdgeLabel', G.Edges.Weight, 'linewidth', 2) 
% G.Edges.Weight 显示边的权重
set( gca, 'XTick', [], 'YTick', [] );

Matlab finds the shortest path

% Matlab中的图节点要从1开始编号
% 编号从1开始连续编号
s = [9 9 1 1 2 2 2 7 7 6 6  5  5 4];
t = [1 7 7 2 8 3 5 8 6 8 5  3  4 3];
w = [4 8 3 8 2 7 4 1 6 6 2 14 10 9];

G = graph(s,t,w);
plot(G, 'EdgeLabel', G.Edges.Weight, 'linewidth', 2) % 作图G
set( gca, 'XTick', [], 'YTick', [] ); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 两点间最短路径
[P,d] = shortestpath(G, 9, 4)  % 9到4的最短路径
% P是最短路径向量 d是最短路径值

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 高亮最短路径
myplot = plot(G, 'EdgeLabel', G.Edges.Weight, 'linewidth', 2);  %首先将图赋给一个变量
highlight(myplot, P, 'EdgeColor', 'r')   %对这个变量进行高亮处理(给边加上r红色)

%% 求出任意两点的最短路径矩阵
D = distances(G) 
D(1,2)  % 1 -> 2的最短路径
D(9,4)  % 9 -> 4的最短路径

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 找出给定范围内的所有点  nearest(G,s,d)
% 返回图形 G 中与节点 s 的距离在 d 之内的所有节点
[nodeIDs,dist] = nearest(G, 2, 10)   %注意:该函数matlab2016a之后才有哦
% nodeIDs:符合条件的节点  dist:距离

Dijkstra

Three data structures: Visited - records whether the point has been visited; Distance - the distance to the starting point; Parent - the point of the previous step

Two point sets: selected point set O, unselected point set N

  • Each time, select the point in N with the smallest distance from the point set O and add it to O

Bellman-Ford

       The Bellman-Ford algorithm no longer distinguishes nodes into visited or unvisited states because the Bellman-Ford model uses loops to perform
Each time the weight is updated, the Bellman Ford algorithm will update the information of all nodes.
principle:
https://blog.csdn.net/a8082649/article/details/81812000
https://www.bilibili.com/video/av43217121

Guess you like

Origin blog.csdn.net/m0_54625820/article/details/128661907