文章目录

1.无向图最短路引例

2.有向图最短路引例

3.单源最短路函数graphshortestpath

1)对函数graphshortestpath进行解释

2)对于find函数解释

3)对于sparse函数解释

4.绘制最短路图形

5.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稀疏矩阵,1,11表示两个结点间最短路,

%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稀疏矩阵,

%1,11表示两个结点间最短路,

%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

求从一顶点出发,所能遍历图中的顶点

matlab求最短路,Matlab最短路学习相关推荐

  1. 运用数学软件matlab求无穷积分,matlab积分的计算及其简单应用论文.doc

    积分的计算及其简单应用 摘要:本文简要的概述了MATLAB 在高等数学中积分的计算及应用:利用MATLAB 中符号积分和数值积分的命令,计算定积分和不定积分.同时,也可以通过这些命令来解决一些实际问题 ...

  2. matlab求微分方程精确解,matlab求微分方程精确解及近似解.ppt

    matlab求微分方程精确解及近似解.ppt 还剩 24页未读, 继续阅读 下载文档到电脑,马上远离加班熬夜! 亲,喜欢就下载吧,价低环保! 内容要点: 求微分方程的解q 自牛顿发明微积分以来,微分方 ...

  3. matlab 求曲面体积,matlab求两曲面之间的体积

    MATLAB求曲面相交所成空间曲线的图形 放在你程序后也可,单独运行也行:t=-0.1:0.1:2*pi;x=2*cos(t);%交线参数方程z=2*sin(t);y1=sqrt(5)*ones(si ...

  4. matlab 求留数,用matlab求留数

    <用matlab求留数>由会员分享,可在线阅读,更多相关<用matlab求留数(3页珍藏版)>请在金锄头文库上搜索. 1.收稿日期: 2006) 05- 29作者简介: 贾新民 ...

  5. matlab求表达式绝对值,matlab绝对值怎么表示

    Matlab 的内部常数 Matlab 的常用内部数学函数 指数函数 exp(x) log(x) 对数函数 log10(x) log2(x) 开方函数 sqrt(x) 绝对值函数 abs(x) sin ...

  6. matlab求方程实根,matlab怎么求方程的根

    MATLAB解方程_IT/计算机_专业资料.一般的代数方程函数solve用于求解一般代数方程的根,假定S为符 号表达式,命令solve (S)求解表达式等于0的根,也 可以再输入一个...... MA ...

  7. 用matlab求累次极限,Matlab笔记——数值计算—高数篇015

    15. 数值计算-高数篇 一.求极限 limit(f,x,a)--求极限lim ()x a f x → limit(f,x,a,'right')--求右极限lim ()x a f x +→ limit ...

  8. 试用matlab求e值,matlab中如何求e精确到20位

    MATLAB语言基础 第一节 使用MATLAB的窗口环境 一.MATLAB语言的显著特点 1.具有强大的矩阵运算能力:Matrix Laboratory(矩阵实验室),使得矩阵运算非常简单. 2.是一 ...

  9. matlab求最小割,matlab實現圖割算法中的最大流最小割Max-flow/min-cut問題(一)

    本篇主要介紹matlab實現Max-flow/min-cut的方法,介紹一種只實現了Max-flow/min-cut的工具箱Bk_matlab. 一:最大流最小割的由來 了解這個問題之前先說說這個問題 ...

  10. 用matlab求残余误差,matlab在测量误差分析中的应用

    matlab在测量误差分析中的应用 MATLAB在测量误差分析中的应用 在技术测量中,按照误差的特点与性质,误差可分为:系统误差,粗大误差和随机误差.在假定不含有系统误差的情况下,可借助MATLAB对 ...

最新文章

  1. axios post body参数_Vue开发中的一些问题(axios封装)
  2. 虚拟示波器OSC802介绍、拆机
  3. java 多线程经典例子——生产者与消费者的问题
  4. “相对论“ 2019-07-10
  5. c语言中fr,关于frwite()函数的一个问题,弄了好久就是不行,求解啊!
  6. 原型(Prototype)的场景是不支持循环依赖的
  7. mysql java驱动 ibm_JDBC驱动汇总
  8. failed to execute ‘dot‘, make sure the Graphviz executables are on your systems‘ PATH
  9. 中仪股份管道机器人_中仪股份中仪股份cctv检测管道机器人X5-HSX5-HS
  10. 搭建一个 软件授权码管理系统
  11. mysql索引填充因子_处理索引碎片,填充因子(FILLFACTOR)
  12. 测试工程师个人年终总结
  13. 鲲鹏微认证的 一些知识点
  14. mysql string agg_【转】SQL Server一个字段串拆分成多行显示或者多行数据合并成一个字符串(STRING_AGG、STRING_SPLIT)...
  15. 影响云服务器租用价格的因素有哪些
  16. H.266: PDPC相关提案整理
  17. ubuntu vscode 终端字体设置(字体间隔过大)
  18. NProgress.js - 前端全站进度条插件 - 给你的网站添加一个加载进度条
  19. 简单三步, 搭建全平台私有同步网盘
  20. 数控加工中心中编程技巧及常用指令

热门文章

  1. Oracle VARCHAR2超过4000字节-数据库修改
  2. 效率(1)Excel常用操作技巧
  3. 二进制求和和x的平方根
  4. Photoshop把漂亮湖景照片调成唯美雪景效果
  5. 回忆一年前的快乐时光
  6. Verilog描述有限状态机(一段式、二段式、三段式)
  7. 业务层 、服务层、数据层、表现层
  8. A,B两个整数集合,设计一个算法求他们的交集,尽可能的高效(牛客网)
  9. OpenCV小例程——分区域不同的显示视频
  10. 如何写一份优秀的英文简历?