MATLAB的图论工具箱
命令名功能graphallshortestpaths求所有对点间的最短距离graphconncomp找无向图的连通分支,或有向图的强(弱)连通分支graphisdag测试有向图是否含圈,不含返回1,否则返回0graphisspantree确定一个图是否是生成树,是返回1graphissomorphism确定两个图是否同构,是返回1graphmaxflow计算有向图的最大流注:MATLAB求解最大流命令只能解决权重均为正值且两顶点之间不能有两条弧的问题grapminspantree图中最小生成树graphpred2path把前驱顶点序列转换为路径的顶点序列graphshortestpath求图中指定一对顶点间的最短距离和最短路径graphtopoorder执行有向无圈图的拓扑排序graphtraverse求从一定点出发,所能遍历图中的顶点
\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功能求所有对点间的最短距离找无向图的连通分支,或有向图的强(弱)连通分支测试有向图是否含圈,不含返回1,否则返回0确定一个图是否是生成树,是返回1确定两个图是否同构,是返回1计算有向图的最大流注:MATLAB求解最大流命令只能解决权重均为正值且两顶点之间不能有两条弧的问题图中最小生成树把前驱顶点序列转换为路径的顶点序列求图中指定一对顶点间的最短距离和最短路径执行有向无圈图的拓扑排序求从一定点出发,所能遍历图中的顶点
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]);