Matlab最短路学习

1.无向图最短路引例

求无向图的最短路径:从v1到v11(最左边到最右边)
在这里插入图片描述

matlab代码

clc ,clear;
a(1,2)=2;a(1,3)=8;a(1,4)=1;
a(2,3)=6;a(2,5)=1;
a(3,4)=7;a(3,5)=5;a(3,6)=1;a(3,7)=2;
a(4,7)=9;
a(5,6)=3;a(5,8)=2;a(5,9)=9;
a(6,7)=4;a(6,9)=6;
a(7,9)=3;a(7,10)=1;
a(8,9)=7;a(8,11)=9;
a(9,10)=1;a(9,11)=2;
a(10,11)=4;
a=a';%matlab工具箱要求数据是下三角矩阵
[i,j,v]=find(a);
b=sparse(i,j,v,11,11);%构造稀疏矩阵
%b稀疏矩阵,111表示两个结点间最短路,
%Directed是标志图为有向图或者无向图的属性,该图是无向图,对于属性为false
[x,y,z]=graphshortestpath(b,1,11,'Directed',false)


运行之后得到结果
其中x表示最短路的长度,y表示最短路的节点编号,z表示最短路径的前驱节点

x =13


y =  1     2     5     6     3     7    10     9    11


z = 0     1     6     1     2     5     3     5    10     7     9

2.有向图最短路引例

在这里插入图片描述

该图中共有7个顶点,
邻接矩阵为
在这里插入图片描述
matlab代码

clc,clear;
a=zeros(7);
a(1,2)=4;a(1,3)=2;
a(2,3)=3;a(2,4)=2;a(2,5)=6;
a(3,4)=5;a(3,6)=4;
a(4,5)=2;a(4,6)=7;
a(5,6)=4;a(5,7)=8;
a(6,7)=3;
b=sparse(a);%构造稀疏矩阵
%有向图对应Directed为true,求最短路的方法是bellman-ford法
[x,y,z]=graphshortestpath(b,1,7,'Directed',true,'Method','Bellman-Ford');
h=view(biograph(b,[]))%画图

运行结果

x =9

y = 1     3     6     7

z =  0     1     1     2     4     3     6

绘图
在这里插入图片描述

最短路绘图
添加如下代码

h=view(biograph(b,[]))

set(h.Nodes(y),'Color',[1 0.4 0.4])
edges = getedgesbynodeid(h,get(h.Nodes(y),'ID'));
set(edges,'LineColor',[1 0 0])
set(edges,'LineWidth',1.5)

在这里插入图片描述

3.单源最短路函数graphshortestpath

graphshortestpath:求图中指定的一对顶点间的最短距离和最短路径
用法如下:

[dist, path, pred] = graphshortestpath(G, S)determines the single-source shortest paths from node (单源最短路)S to all other nodes in the graph represented by matrix G. Input G is an N-by-N sparse matrix that represents a graph. Nonzero entries in matrix G represent the weights of the edges.
三个输出变量解释:

  • dist are the N distances from the source to every node (using Infs for nonreachable nodes and 0 for the source node).
  • path contains the winning paths to every node.
  • pred contains the predecessor nodes of the winning paths.

[dist, path, pred] = graphshortestpath(G, S, T) determines the single source-single destination shortest path from node S to node T.求节点S到节点T的单源最短路

[...] = graphshortestpath(..., 'PropertyName', PropertyValue, ...)calls graphshortestpath with optional properties that use property name/property value pairs(使用属性名/属性值对). You can specify one or more properties in any order. Each PropertyName must be enclosed in single quotes and is case insensitive. These property name/property value pairs are as follows:

[...] = graphshortestpath(..., 'Directed', DirectedValue, ...) indicates whether the graph is directed (有向图)or undirected(无向图). Set DirectedValue to false for an undirected graph. This results in the upper triangle of the sparse matrix being ignored. Default is true.

[...] = graphshortestpath(..., 'Method', MethodValue, ...)lets you specify the algorithm used to find the shortest path. Choices are:

最短路方法 解释
Bellman-Ford Assumes weights of the edges to be nonzero entries in sparse matrix G. Time complexity is O(N*E), where N and E are the number of nodes and edges respectively.
BFS Breadth-first search. Assumes all weights to be equal, and nonzero entries in sparse matrix G to represent edges. Time complexity is O(N+E), where N and E are the number of nodes and edges respectively.
Acyclic无环 Assumes G to be a directed acyclic graph(有向无环图) and that weights of the edges are nonzero entries in sparse matrix G. Time complexity is O(N+E), where N and E are the number of nodes and edges respectively.
Dijkstra Default algorithm. Assumes weights of the edges to be positive values in sparse matrix G. Time complexity is O(log(N)*E), where N and E are the number of nodes and edges respectively.

[...] = graphshortestpath(..., 'Weights', WeightsValue, ...) lets you specify custom weights for the edges. WeightsValue is a column vector having one entry for every nonzero value (edge) in matrix G. The order of the custom weights in the vector must match the order of the nonzero values in matrix G when it is traversed column-wise. This property lets you use zero-valued weights. By default, graphshortestpath gets weight information from the nonzero entries in matrix G.

1)对函数graphshortestpath进行解释

[x,y,z]=graphshortestpath(b,1,11,'Directed',false)
%b稀疏矩阵,
%111表示两个结点间最短路,
%Directed是标志图为有向图或者无向图的属性,该图是无向图,对于属性为false
%x表示最短路的长度,y表示最短路的节点编号,z表示最短路径的前驱节点


2)对于find函数解释

>> help find
find - Find indices and values of nonzero elements

    This MATLAB function returns a vector containing the linear indices of each
    nonzero element in array X.

具体而言

[i,j,v]=find(a);

指定三个输出返回行下标、列下标和元素值

3)对于sparse函数解释

通过挤出任何零元素将满矩阵转换为稀疏格式。如果矩阵包含许多零,将矩阵转换为稀疏存储空间可以节省内存

>> help sparse
sparse - Create sparse matrix

    This MATLAB function converts a full matrix into sparse form by squeezing out
    any zero elements.
    

具体而言

b=sparse(i,j,v,11,11);%构造稀疏矩阵

解释:根据 i、j 和 v 三元数生成 11×11 的稀疏矩阵。
sparce函数运行结果如下:

b =

   (2,1)        2
   (3,1)        8
   (4,1)        1
   (3,2)        6
   (5,2)        1
   (4,3)        7
   (5,3)        5
   (6,3)        1
   (7,3)        2
   (7,4)        9
   (6,5)        3
   (8,5)        2
   (9,5)        9
   (7,6)        4
   (9,6)        6
   (9,7)        3
  (10,7)        1
   (9,8)        7
  (11,8)        9
  (10,9)        1
  (11,9)        2
  (11,10)       4

sparse函数补充

A = eye(10000);
whos A

在这里插入图片描述
该矩阵占内存800MB。
转换为稀疏矩阵

S = sparse(A);
whos S

在这里插入图片描述
采用稀疏形式时,同一矩阵只使用约 0.24 MB 内存。在这种情况下,可以使用 sparse 函数来避免满存储。

4.绘制最短路图形

下面给出绘图代码

clc ,clear;
a(1,2)=2;a(1,3)=8;a(1,4)=1;
a(2,3)=6;a(2,5)=1;
a(3,4)=7;a(3,5)=5;a(3,6)=1;a(3,7)=2;
a(4,7)=9;
a(5,6)=3;a(5,8)=2;a(5,9)=9;
a(6,7)=4;a(6,9)=6;
a(7,9)=3;a(7,10)=1;
a(8,9)=7;a(8,11)=9;
a(9,10)=1;a(9,11)=2;
a(10,11)=4;
a=a';%matlab工具箱要求数据是下三角矩阵
[i,j,v]=find(a);
b=sparse(i,j,v,11,11)%构造稀疏矩阵

[x,y,z]=graphshortestpath(b,1,11,'Directed',false)%Directed是标志图为有向图或者无向图的属性,该图是无向图,对于属性为false

h = view(biograph(b,[],'ShowArrows','off','ShowWeights','on'));%绘图
%无向图的用法
set(h.Nodes(y),'Color',[1 0.4 0.4])%y是路径
 fowEdges = getedgesbynodeid(h,get(h.Nodes(y),'ID'));
 revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(y)),'ID'));
 edges = [fowEdges;revEdges];
 set(edges,'LineColor',[1 0 0])
 set(edges,'LineWidth',1.5)

绘出图形
在这里插入图片描述

5.matlab图论工具箱

附上Matlab图论工具箱中的函数

命令名 功能
graphallshortestpaths 求图中所有顶点对之间的最短距离
graphconncomp 找无向图的连通分支,或有向图的强弱连通分支
graphisdag 测试有向图是否含有圈,不含圈返回1,否则返回0
graphisomorphism 确定两个图是否同构,同构返回1,否则返回0
graphisspantree 确定一个图是否是生成树,是返回1,不是返回0
graphmaxflow 计算有向图的最大流
graphminspantree 在图中找最小生成树
graphpred2path 把前驱顶点序列变成路径的顶点序列
graphshortestpath 求图中指定的一对顶点间的最短路径和最短距离
graphtopoorder 执行有向无圈图的拓扑排序
graphtraverse 求从一顶点出发,所能遍历图中的顶点
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章