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稀疏矩陣,111表示兩個結點間最短路,
%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稀疏矩陣,
%111表示兩個結點間最短路,
%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 求從一頂點出發,所能遍歷圖中的頂點
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章