问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

用MATLAB软件如何找最短路径?

发布网友 发布时间:2023-02-15 01:17

我来回答

1个回答

热心网友 时间:2023-09-13 16:33

可以直接用graphshortestpath函数,具体说明如下。

graphshortestpath solves the shortest path problem in graph.

[DIST,PATH,PRED] = graphshortestpath(G,S) determines the single source
shortest paths from node S to all other nodes in the graph G. Weights of
the edges are all nonzero entries in the n-by-n adjacency matrix
represented by the sparse matrix G. DIST are the n distances from source
to every node (using Inf for non-reachable nodes and zero for the source
node). The PATH contains the winning paths to every node, and PRED
contains the predecessor nodes of the winning paths.

[DIST,PATH,PRED] = graphshortestpath(G,S,D) determines the single
source-single destination shortest path from node S to node D.

graphshortestpath(...,'METHOD',METHOD) selects the algorithm to use,
options are:
'BFS' - Breadth First Search, assumes all the weights are
equal, edges are nonzero entries in the sparse matrix
G. Time complexity is O(n+e).
['Dijkstra'] - Assumes that weights of the edges are all positive
values in the sparse matrix G. Time complexity is
O(log(n)*e).
'Bellman-Ford' - Assumes that weights of the edges are all nonzero
entries in the sparse matrix G. Time complexity is
O(n*e).
'Acyclic' - The input graph must be acyclic. Assumes that weights
of the edges are all nonzero entries in the sparse
matrix G. Time complexity is O(n+e).

Note: n and e are number of nodes and edges respectively.

graphshortestpath(...,'DIRECTED',false) indicates that the graph G is
undirected, upper triangle of the sparse matrix is ignored. Default is
true.

graphshortestpath(...,'WEIGHTS',W) provides custom weights for the edges,
useful to indicate zero valued weights. W is a column vector with one
entry for every edge in G, traversed column-wise.

Examples:
% Create a directed graph with 6 nodes and 11 edges
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'))
% Find shortest path from 1 to 6
[dist,path,pred] = graphshortestpath(DG,1,6)
% Mark the nodes and edges of the shortest path
set(h.Nodes(path),'Color',[1 0.4 0.4])
edges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
set(edges,'LineColor',[1 0 0])
set(edges,'LineWidth',1.5)

% Solving the previous problem for an undirected graph
UG = tril(DG + DG')
h = view(biograph(UG,[],'ShowArrows','off','ShowWeights','on'))
% Find the shortest path between node 1 and 6
[dist,path,pred] = graphshortestpath(UG,1,6,'directed',false)
% Mark the nodes and edges of the shortest path
set(h.Nodes(path),'Color',[1 0.4 0.4])
fowEdges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(path)),'ID'));
edges = [fowEdges;revEdges];
set(edges,'LineColor',[1 0 0])
set(edges,'LineWidth',1.5)

See also: graphallshortestpaths, graphconncomp, graphisdag,
graphisomorphism, graphisspantree, graphmaxflow, graphminspantree,
graphpred2path, graphtheorydemo, graphtopoorder, graphtraverse.

References:
[1] E.W. Dijkstra "A note on two problems in connexion with graphs"
Numerische Mathematik, 1:269-271, 1959.
[2] R. Bellman "On a Routing Problem" Quarterly of Applied Mathematics,
16(1):87-90, 1958.
Reference page in Help browser
doc graphshortestpath
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
步步高学习机@iboxH2 H2学习机的屏幕大小? 6-7万左右纯电 女生想买台便宜的小车在城市代步,有什么好推荐?电动车最好? 河南德盛智能环保科技有限公司怎么样? 深圳市德盛铭电科技有限公司怎么样? pvc防水门生产厂家哪个好呢? 晋江市德顺陶瓷建材有限公司简介 酒店家具厂家 梨子酿酒最简单的方法 5ml注射器针头正常是几号的? 注射器分多少号? 发面芝麻烧饼的做法 手机玩游戏和cpu有关系吗还是Gpu 好听的情侣游戏网名精选,前世为你驻足 | 今生因你回眸 excl如何把第1节换成第一节 小米10怎么刷新手机 在计算机知道中,我们通常所说的“祼机”指的是:A:只装有操作系统的计算机, CCIE对于企业有什么作用 笔记本可以一边充电一边用么? 网络游戏经典宠物名字,敢不敢喜欢我 2017年厦门暑假旅游攻略 0.2纳米的光刻机意味着什么 厦门四日游详细攻略 菜鸟裹裹上门拿了两件衣服商家只收到一件怎么办在哪里投诉 菜鸟裹裹邀请有礼成功会显示什么? safety check:please move clamp !翻译成中文是什么意思? 两个字文雅的游戏网名 两字游戏网名高贵 天龙八部网名起名 厦门社保局周末上班吗 开头难的励志句子(精选49句) 咪咕盒子mg100替换百视通桌面 oa计算机应用属于什么 春节期间北京好玩的地方 鸟倦飞而知还的上句及下句 通信行程卡怎么用? 湛江旅游景点大全介绍免费景点 VⅰⅤ0Z5i手机的开发者选项怎么找? 办理信用卡的条件是什么,在京东或者天猫可以分期的那种 乔安的故事是什么演唱方式 什么音色适合唱音乐剧 唱音乐剧,是唱美声还是通俗 像《音乐之声》这样的音乐剧用的是什么唱法啊 我是学美声的想学他这种感觉的 电脑长鸣开不了机怎么回事? 湛江自驾游去哪最好 今天在智行火车票这个软件上面订了两张高铁票,诱导我重复付款了两次,被骗了666元,软件上支付宝和银 女角色名字 撒贝宁的开场白可以剪辑吗 求广州到厦门3天的旅游攻略 最后一代不支持指纹识别的ipad是几