日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Matlab最短路学习

發布時間:2025/4/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Matlab最短路学习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

      • 1.無向圖最短路引例
      • 2.有向圖最短路引例
      • 3.單源最短路函數graphshortestpath
        • 1)對函數graphshortestpath進行解釋
        • 2)對于find函數解釋
        • 3)對于sparse函數解釋
      • 4.繪制最短路圖形
      • 5.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 =13y = 1 2 5 6 3 7 10 9 11z = 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 =9y = 1 3 6 7z = 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-FordAssumes 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.
BFSBreadth-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.
DijkstraDefault 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 elementsThis MATLAB function returns a vector containing the linear indices of eachnonzero element in array X.

具體而言

[i,j,v]=find(a);

指定三個輸出返回行下標、列下標和元素值

3)對于sparse函數解釋

通過擠出任何零元素將滿矩陣轉換為稀疏格式。如果矩陣包含許多零,將矩陣轉換為稀疏存儲空間可以節省內存

>> help sparse sparse - Create sparse matrixThis MATLAB function converts a full matrix into sparse form by squeezing outany 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是標志圖為有向圖或者無向圖的屬性,該圖是無向圖,對于屬性為falseh = 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求從一頂點出發,所能遍歷圖中的頂點

總結

以上是生活随笔為你收集整理的Matlab最短路学习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。