Matlab图论工具箱

Matlab图论工具箱

稀疏矩阵与普通矩阵的转化

使用工具箱中的命令时,需要将普通矩阵转化为稀疏矩阵

sparse
普通矩阵使用sparse命令变成稀疏矩阵

sparse(S)	%将矩阵S转化为稀疏矩阵
sparse(m,n)	%生成一个m*n的所有元组都为0的稀疏矩阵
sparse(i,j,v)	%i,j,v为3个等长向量,其中i,j为元素v的行标和列表
sparse(i,j,v,m,n)	%m,n为满矩阵时的行列值
%i,j,v可由find获得	[i,j,v]=find(A)	%A为普通矩阵
tril(a)	%截取下三角矩阵

full
稀疏矩阵使用full命令变成普通矩阵

注:有向图和无向图转换为稀疏矩阵方法似乎不同

有向图最大流
graphmaxflow
[max_flow,gra]=graphmaxflow(graph,s,t)

参数

​ graph 为稀疏矩阵

​ s 起始节点

​ t 目的节点

返回值

​ max_flow 最大流

​ gra 满足最大流的稀疏矩阵

注意事项

只能解决权重都为正值,且两个顶点之间没有两条弧的问题。若不满足则进行适当处理。

假如两节点之间 v i , v j v_i,v_j vi,vj有两条弧,权重(容量)为 w i j , w j i w_{ij},w_{ji} wij,wji,则可以在 v i , v j v_i,v_j vi,vj之间添加一个虚拟的节点 v k v_k vk,并添加两条弧 w i k , w k j ( 或 w j k , w k , i ) w_{ik},w_{kj}(或w_{jk},w_{k,i}) wik,wkj(wjk,wk,i),删除= w i j ( 或 w j i ) w_{ij}(或w_{ji}) wij(wji),其中 w i k = w k j = w i j ( 或 w j k = w k i = w j i ) w_{ik}=w_{kj}=w_{ij}(或w_{jk}=w_{ki}=w_{ji}) wik=wkj=wij(wjk=wki=wji)

图最小生成树
graphminspantree
[tree,pred]=graphminspantree(graph[,v])

参数

​ graph为图的稀疏矩阵

​ v为根节点

返回值

​ tree为最小生成树

​ pred为每个节点的的父节点(默认第一个节点为根节点)

其他参数
[Tree, pred] = graphminspantree(..., 'Method', MethodValue, ...)
[Tree, pred] = graphminspantree(..., 'Weights', WeightsValue, ...)

method 默认为prim,可选’Kruskal’

两节点最短路
graphshortestpath
[dist,path,pred]=graphshortestpath(graph,S,T)

graph为稀疏矩阵(可能要求是下三角)

S为起始节点

T为目的节点

dist为最短距离

path为路径

pred为S到每个节点的最短路径中,目标节点的先驱

其他参数
	[...] = graphshortestpath(..., 'Directed', DirectedValue, ...)
    [...] = graphshortestpath(..., 'Method', MethodValue, ...)
    [...] = graphshortestpath(..., 'Weights', WeightsValue, ...)

其中,’Directed’是标志图为有向图或无向图的属性,若图为无向图,属性值为false,有向图为true

‘Method’是使用的算法,默认为’Dijkstra’,还可设’Bellman-Ford’

每对节点间的最短路径
graphallshortestpaths
dist=graphallshortestpaths(DG)

DG为稀疏矩阵

dist为第i个结点到第j个节点的最短距离组成的矩阵(i*J)

其他参数
[dist] = graphallshortestpaths(G, ...'Directed', DirectedValue, ...)
[dist] = graphallshortestpaths(G, ...'Weights', WeightsValue, ...)

其中,’Directed’是标志图为有向图或无向图的属性,若图为无向图,属性值为false

其他工具
命令功能
graphconncomp找无向图的连通分支,或有向图的强(弱)连通分支
graphisdag测试有向图是否含有圈,不含圈返回1,含圈返回0
graphisomorphism确定两个图是否同构,同构返回1,否则返回0
graphisspantree确定一个图是否是生成树,是返回1,否则返回0
graphpred2path把前驱节点序列变成路径的顶点序列
graphtopoorder执行有向图无圈图的拓扑排序
graphtraverse求从一顶点出发,所能遍历的所有图中顶点
视图
[h=]view(biograph(tree,nodestr,'ShowArrows','off',ShowWeights,'on'))

其中,nodestr为节点对应的名字,可以[]默认

其他操作

h.EdgeType='segmented'; %边的连接为线段
h.LayoutType='equilibrium';  %设置图形布局属性
dolayout(h);	%刷新图形布局
  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GrTheory - Graph Theory Toolbox. 内含40个图论问题的matlab代码,包括最短径问题等。对数学建模,2012美赛ICM的帮助尤其大。欢迎下载。 Functions: grBase - find all bases of digraph; grCoBase - find all contrabases of digraph; grCoCycleBasis - find all independent cut-sets for a connected graph; grColEdge - solve the color problem for graph edges; grColVer - solve the color problem for graph vertexes; grComp - find all components of graph; grCycleBasis - find all independent cycles for a connected graph; grDecOrd - solve the problem about decomposition of the digraph to the sections with mutually accessed vertexes (strongly connected components); grDistances - find the distances between any vertexes of graph; grEccentricity - find the (weighted) eccentricity of all vertexes, radius, diameter, center vertexes and the periphery vertexes; grIsEulerian - find the Eulerian cycle of graph; grIsomorph - solve the problem about isomorphism for two graphs; grMaxComSu - solve the maximal complete sugraph problem for the graph; grMaxFlows - solve the maximal flow problem for the digraph; grMaxMatch - solve the maximal matching problem for the graph; grMaxStabSet - solve the maximal stable set problem for the graph; grMinAbsEdgeSet - solve the minimal absorbant set problem for the graph edges; grMinAbsVerSet - solve the minimal absorbant set problem for the graph vertexes; grMinCutSet - solve the minimal cut-set problem for the digraph; grMinEdgeCover - solve the minimal edge cover problem for the graph; grMinSpanTree - solve the minimal spanning tree problem for the graph; grMinVerCover - solve the minimal vertex cover problem for the graph; grPERT - solve the project evaluation research task; grPlot - draw the plot of the graph (digraph); grShortPath - solve the shortest path problem for the digraph; grShortVerPath - for digraph with weighted vertexes solve the problem about the path with minimal weight of verticies; grTranClos - built the transitive closure for the digraph; grTravSale - solve
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值