MATLAB----2019/7/14,7/15

MATLAB的图论工具箱
命 令 名 功能 g r a p h a l l s h o r t e s t p a t h s 求 所 有 对 点 间 的 最 短 距 离 g r a p h c o n n c o m p 找 无 向 图 的 连 通 分 支 , 或 有 向 图 的 强 ( 弱 ) 连 通 分 支 g r a p h i s d a g 测 试 有 向 图 是 否 含 圈 , 不 含 返 回 1 , 否 则 返 回 0 g r a p h i s s p a n t r e e 确 定 一 个 图 是 否 是 生 成 树 , 是 返 回 1 g r a p h i s s o m o r p h i s m 确 定 两 个 图 是 否 同 构 , 是 返 回 1 g r a p h m a x f l o w 计 算 有 向 图 的 最 大 流 注 : M A T L A B 求 解 最 大 流 命 令 只 能 解 决 权 重 均 为 正 值 且 两 顶 点 之 间 不 能 有 两 条 弧 的 问 题 g r a p m i n s p a n t r e e 图 中 最 小 生 成 树 g r a p h p r e d 2 p a t h 把 前 驱 顶 点 序 列 转 换 为 路 径 的 顶 点 序 列 g r a p h s h o r t e s t p a t h 求 图 中 指 定 一 对 顶 点 间 的 最 短 距 离 和 最 短 路 径 g r a p h t o p o o r d e r 执 行 有 向 无 圈 图 的 拓 扑 排 序 g r a p h t r a v e r s e 求 从 一 定 点 出 发 , 所 能 遍 历 图 中 的 顶 点 \begin{array}{c|} 命令名 & \text{功能} \\ \hline graphallshortestpaths& 求所有对点间的最短距离 \\ \hline graphconncomp & 找无向图的连通分支,或有向图的强(弱)连通分支 \\ \hline graphisdag & 测试有向图是否含圈,不含返回1,否则返回0 \\ \hline graphisspantree & 确定一个图是否是生成树,是返回1 \\ \hline graphissomorphism & 确定两个图是否同构,是返回1 \\ \hline graphmaxflow & 计算有向图的最大流 \quad 注:MATLAB求解最大流命令只能解决权重均为正值且两顶点之间不能有两条弧的问题 \\ \hline grapminspantree &图中最小生成树 \\ \hline graphpred2path & 把前驱顶点序列转换为路径的顶点序列 \\ \hline graphshortestpath & 求图中指定一对顶点间的最短距离和最短路径 \\ \hline graphtopoorder &执行有向无圈图的拓扑排序 \\ \hline graphtraverse & 求从一定点出发,所能遍历图中的顶点 \\ \hline \end{array} graphallshortestpathsgraphconncompgraphisdaggraphisspantreegraphissomorphismgraphmaxflowgrapminspantreegraphpred2pathgraphshortestpathgraphtopoordergraphtraverse功能1011MATLAB


MATLAB代码:
《数学建模算法与应用》
P54页例4.9:(无向图求最短距离)

a(1,2)=2;                                    %从v1点到v2点有为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;
b=a';                                                %b是a的转置矩阵
[i,j,v]=find(b);                                     % i表示非0元素的行下标,j表示列下标,v表示元素数值
c=sparse(i,j,v,11,11)                                %构造系数矩阵的函数,sparse(i, j, k, m, n)中,i,j,k分别是向量的行坐标,向量的列坐标,向量的值,m,n是系数矩阵的行宽
[x,y,z]=graphshortestpath(c,1,11,'Directed',false);  %Directed表示图为有向或无向的标志,无向图对应的属性值为false

例4.10(渡河问题)

a=[1 1 1 1;1 1 1 0;1 1 0 1;1 0 1 1;1 0 1 0;0 1 0 1;0 1 0 0;0 0 1 0;0 0 0 1;0 0 0 0];
b=[1 0 0 0;1 1 0 0;1 0 1 0;1 0 0 1];
w=zeros(10);
for i=1:9
    for j=i+1:10
        for k=1:4
            if findstr(xor(a(i,:),b(k,:)),a(j,:))   %xor:异或,a(i,:)==b(k,:)则返回0
                w(i,j)=1
            end
        end
    end
end
w=w';
c=sparse(w);
[x,y,z]=graphshortestpath(c,1,10,'Directed',0);
h=view(biograph(c,[],'showArrows','off','showWeights','off'));    %生成无向图
edges=getedgesbynodeid(h);
set(edges,'Linecolor',[0,0,0]);
set(edges,'Linecolor',1.5);
b=findstr(1,0)

例4.11:(有向图求最短距离)

a=zeros(7);
a(1,2)=4;
a(1,3)=2;
a(2,3)=3;
a(2,5)=6;
a(2,4)=2;
a(3,4)=5;
a(3,6)=4;
a(4,5)=2;
a(4,6)=7;
a(5,6)=5;
a(5,7)=8;
a(6,7)=3;
a=sparse(a);
[x,y,z]=graphshortestpath(a,1,7,'directed',true,'method','bellman-ford');       %构建有向图
h=view(biograph(a,[],'showweight','on','showarrows','on'));                           %画出有向图
set(h.nodes(y),'color',[1 0.4 0.4]);                                                       %使路径通过的点变色
edges=getedgesbynodeid(h.nodes(y),'ID');  
set(edges,'Linecolor',[1 0 0]);
set(edges,'lineweight',1.5);

4.12(应用)

x=[0 5 16 20 33 23 35 25 10];
y=[15 20 24 20 25 11 7 0 3];
xy=[x;y];
d=mandist(xy)          %求xy的两两列向量间的绝对值距离
d=tril(d);             %tril抽取上三角函数;
b=sparse(d);
[st,pred]=graphminspantree(b,'method','kruskal');
st=full(st);
treelength=sum(sum(st));
view(biograph(st,[],'ShowArrows','off'))

4.13 (最大流问题)

a=zeros(9);
a(1,2)=6;
a(1,3)=4;
a(1,4)=5;
a(2,3)=3;
a(2,5)=9;
a(2,6)=9;
a(3,4)=4;
a(3,5)=6;
a(3,6)=7;
a(3,7)=3;
a(4,7)=5;
a(4,9)=1;
a(5,8)=12;
a(6,5)=8;
a(6,8)=10;
a(7,6)=4;
a(7,8)=15;
a(9,3)=1;
b=sparse(a);
[x,y,z]=graphmaxflow(b,1,8);
h=view(biograph(b,[],'showweight','on'));
edges=getedgesbynodeid(h);
set(edges,'linecolor',[1 0 0]);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值