早教吧 育儿知识 作业答案 考试题库 百科 知识分享

跪求解释Matlab中graphshortestpath的具体用法需要什么参数,矩阵,得到什么结果????是一个最短路径问题,据说是封装好的floyd,Dijkstra算法最短路径问题是有权还是没权问题呢?

题目详情
跪求解释 Matlab中graphshortestpath的具体用法 需要什么参数,矩阵,得到什么结果????是一个最短路径问题,据说是封装好的floyd,Dijkstra 算法 最短路径问题是有权还是没权问题呢?
▼优质解答
答案和解析
我看了一下这个函数的例子 默认是Dijkstra 算法 是有权的, 我想如果把权都赋1的话, 就相当于没权的了 参数是带权的稀疏矩阵及结点 看看这两个例子(一个有向一个无向), 或许你能找到你想知道的 % 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) %找顶点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) % 对matlab我只知皮毛, 只是对你这个问题感兴趣而已(以前学过), 可别怪我没答到点子上哈