Matlab中圖論工具箱的應用


Matlab圖論工具箱的命令見表1

1  matlab圖論工具箱的相關命令

命令名

功能

graphallshortestpaths

求圖中所有頂點對之間的最短距離

graphconncomp

找無向圖的連通分支,或有向圖的強弱連通分支

graphisdag

測試有向圖是否含有圈,不含圈返回1,否則返回0

graphisomorphism

確定兩個圖是否同構,同構返回1,否則返回0

graphisspantree

確定一個圖是否是生成樹,是返回1,否則返回0

graphmaxflow

計算有向圖的最大流

graphminspantree

在圖中找最小生成樹

graphpred2path

把前驅頂點序列變成路徑的頂點序列

graphshortestpath

求圖中指定的一對頂點間的最短距離和最短路徑

graphtopootder

執行有向無圈圖的拓撲排序

graphtraverse

求從一頂點出發,所能遍歷圖中的頂點

1.圖的最短路徑graphallshortestpaths函數的命令格式:

[dist]=graphallshortestpaths(G)

[dist]=graphallshortestpaths(G,...’Directed’,DirectedValue,...)

[dist]=graphallshortestpaths(G,...’Weights’,WeightsValue,...)

G是代表一個圖的N*N稀疏矩陣,矩陣中的非零值代表一條邊的權值:DirectedValue屬性表示圖是否為有向圖,false代表無向圖,true代表有向圖,默認為true。WeightsValue是矩陣G中邊的自定義權值列向量,該屬性可以使我們使用零權值。輸出[dist]是一個N*N的矩陣,每一個元素代表兩點之間最短距離,對角線上的元素總為零,不在對角線上的零表示起點和終點的距離為零,inf值表示沒有路徑。

1)創建並顯示一個含有6個結點11條邊的有向圖

w=[41 99 51 32 15 45 38 32 36 29 21];%權值向量

dg=sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],w)%構造的稀疏矩陣表示圖

h=view(biograph(dg,[],'ShowWeights','on'))%顯示圖的結構

dist=graphallshortestpaths(dg)%顯示圖中每對結點之間的最短路徑  

dist =

     0   136    53    57    21    95

   111     0    51    66    32   104

    60    94     0    15    81    53

    45    79    67     0    66    38

    81   115    32    36     0    74

 89    41    29    44    73     0

 

(2)創建無向圖,結點信息同上

ug=tril(dg+dg');%要求生成的無向圖是一個下三角矩陣

view(biograph(ug,[],'ShowArrows','off','ShowWeights','on'));

dist=graphallshortestpaths(ug,'directed',false)

dist =

     0    53    53    45    21    82

    53     0    51    66    32    41

    53    51     0    15    32    29

    45    66    15     0    36    38

    21    32    32    36     0    61

    82    41    29    38    61     0

 

2.最小生成樹graphminspantree函數的調用格式:

[Tree,pred]=graphminspantree(G)

[Tree,pred]=graphminspantree(G,R)

[Tree,pred]=graphminspantree(...,’Method’,MethofValue,...)

[Tree,pred]=graphminspantree(...,’Weights’,WeightsValue,...)

該函數用來尋找一個無循環的節點集合,連接無詳圖的全部節點,並且總的權值最小。Tree是一個代表生成樹的稀疏矩陣,pred是包含最小生成的祖先節點的向量。G是無向圖,R代表根節點,取值為1到節點數目,Method可以選擇‘Kruskal’,’Prim’算法。

(1)構造無向圖

w=[ 41    29    51    32    50    45    38    32    36    29    21];

dg=sparse([1 1 2 2 3 4 4 5 5 6 6],[2 6 3 5 4 1 6 3 4 2 5],w)

ug=tril(dg+dg');

view(biograph(ug,[],'ShowArrows','off','ShowWeights','on'));

[st,pred]=graphminspantree(ug)%生成最小生成樹

h=view(biograph(st,[],'ShowArrows','off','ShowWeights','on'));

Edges=getedgesbynodeid(h);%提取無向圖h的邊集

set(Edges,'LineColor',[0 0 0]);%設置顏色屬性

set(Edges,'LineWidth',1.5)%設置邊值屬性

  

3.計算有向圖的最大流graphmaxflow,函數格式為:

[MaxFlow,FlowMatrix,Cut]=graphmaxflow(G,SNode,TNode)

[...]=graphmaxflow(G,SNode,TNode,...’Capacity’,CapacityValue,...)

[...]=graphmaxflow(G,SNode,TNode,...’Method’,MethodValue,...)

G是N*N的稀疏矩陣,表示有向圖,SNode和TNode都是G中的節點,分別表示起點和目標點,CapacityValue為每條邊自定義容量的列向量;MethodValue可以取‘Edmonds’和‘Goldberg’算法。輸出MaxFlow表示最大流,FlowMatrix是表示每條邊數據流值的稀疏矩陣,Cut表示連接SNode到TNode的邏輯向量,如果有多個解時,Cut是一個矩陣。

(1)構造帶有節點和邊的有向圖

 cm=sparse([1 1 2 2 3 3 4 5],[2 3 4 5 4 5 6 6],[2 3 3 1 1 1 2 3 ],6,6);

%6個節點8條邊

[M,F,K]=graphmaxflow(cm,1,6);%計算第1個到第6個節點的最大流

h=view(biograph(cm,[],'ShowWeights','on'));%顯示原始有向圖的圖結構

 

h1=view(biograph(F,[],'ShowWeights','on'));%顯示計算最大流矩陣的圖結構

4.圖的遍歷函數graphtraverse,命令格式如下:

[disc,pred,closed]=graphtraverse(G,S)

[...]=graphtraverse(G,S,...’Directed’,DirectedValue,...)

[...]=graphtraverse(G,S,...’Depth’,DepthValue,...)

[...]=graphtraverse(G,S,...’Method’,MethodValue,...)

G是有向圖,S為起始節點,disc是節點索引向量,pred是祖先節點索引向量

(1)創建一個有向圖

DG=sparse([1 2 3 4 5 5 5 6 7 8 8 9],[2 4 1 5 3 6 7 9 8 1 10 2],true,10,10)

h1=view(biograph(DG));

order=graphtraverse(DG,4)%使用深度優先算法從第4個節點開始遍歷

order2=graphtraverse(DG,4,'Method','BFS')%使用廣度優先遍歷

index=graphtraverse(DG,4,'depth',2)%標記與節點4鄰近的深度為2的節點

order =

     4     5     3     1     2     6     9     7     8    10

order2 =

     4     5     3     6     7     1     9     8     2    10

index =

     4     5     3     6     7

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM