graphshortestpath sparse函数生成稀疏矩阵的行列数不一致

如题,我想要使用biography绘制线路图,需要使用nXn的稀疏矩阵,matlab给的例子:
L=[12 11 23 24 26 30 11 24 29];
UG=sparse([1 1 3 2 4 4 7 5 2],[2 3 4 4 5 7 6 6 5],L);
h = view(biograph(UG,[],'ShowArrows','off','ShowWeights','on'));
能生成7X7的稀疏矩阵:
whos UG
Name Size Bytes Class Attributes
UG 7x7 208 double sparse

而我自己的数据:
L=[12 11 23 24 26 30 11 24 29];
lianjie1=[1 2 3 4 8 8 8 8 9 10 21 22 16 8 21 21 21 26 26 26 26 30 30 30 30 30 32 32 33 32 34 35 34 34 34 39];
lianjie2=[7 7 7 7 7 5 6 11 11 11 11 11 7 18 20 22 26 27 25 129 30 28 29 31 32 50 33 34 34 50 35 33 49 47 39 35];
L= [167.83 7.62 13.89 10.2 27.02 12.65 9.85 19.24 9.22 11.66 27.66 22.36 25.5 106 7.62 17.46 17.02 6.32 10.3 10.3 20.4 8.4 8.6 8.4 20.1 18 23.32 25 13.92 16.4 18.68 24.35 9.21 17 31.78 21.09];
UG=sparse(lianjie1,lianjie2,L);
AA=full(UG);
h = view(biograph(AA,[],'ShowArrows','off','ShowWeights','on'));

[dist,path,pred] = graphshortestpath(UG,1,6,'Method','Dijkstra');
生成的却是:
whos UG
Name Size Bytes Class Attributes
UG 39x129 1616 double sparse

有的同学可能会说,可以写成 UG=sparse(lianjie1,lianjie2,L,129,129)
但是这样生成的路线图会有很多没有用的节点。
我想问一下spars生成的稀疏矩阵的行列数与什么有关?我应该如何改正程序?十分感谢!!

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

graphshortestpath(.,[1 0 0])
set(edges;] - Assumes that weights of the edges are all positive
values in the sparse matrix G. Time complexity is
O(log(n)*e). Time complexity is O(n+e).

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

graphshortestpath(.,[],'.Nodes(path),'LineColor',pred] = graphshortestpath(DG,1,6)
% Mark the nodes and edges of the shortest path
set(h... Default is
true;
set(edges,'DIRECTED'..;Acyclic'ID')).

Examples;BFS'.

[DIST,PATH;,METHOD) selects the algorithm to use:
% 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, and PRED
contains the predecessor nodes of the winning paths.

[DIST,[1 0.4 0.4])
edges = getedgesbynodeid(h,get(h.Nodes(path),'Bellman-Ford' - Assumes that weights of the edges are all nonzero
entries in the sparse matrix G可以直接用graphshortestpath函数.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,
useful to indicate zero valued weights. W is a column vector with one
entry for every edge in G, traversed column-wise,'METHOD'.
'..,1;WEIGHTS',false) indicates that the graph G is
undirected.
',
options are; - The input graph must be acyclic;,'.

graphshortestpath(. Time complexity is O(n+e).
['Dijkstra',PRED] = graphshortestpath(G,'LineWidth'; - Breadth First Search, assumes all the weights are
equal, edges are nonzero entries in the sparse matrix
G,具体说明如下。

graphshortestpath solves the shortest path problem in graph;,W) provides custom weights for the edges. Assumes that weights
of the edges are all nonzero entries in the sparse
matrix G;on'))
% Find shortest path from 1 to 6
[dist,path. 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,S) determines the single source
shortest paths from node S to all other nodes in the graph G. Time complexity is
O(n*e);ShowWeights','Color'. Weights of
the edges are all nonzero entries in the n-by-n adjacency matrix
represented by the sparse matrix G:
', upper triangle of the sparse matrix is ignored.,'
温馨提示:答案为网友推荐,仅供参考