THE GRAPH FUNCTION DOESN'T CONSIDER WEIGHTS

조회 수: 11 (최근 30일)
Gianluca Angelini
Gianluca Angelini 2022년 1월 30일
댓글: Gianluca Angelini 2022년 2월 4일
Hi to everybody, in my code I am trying to calculate the shortest path from a graph, whose I have explicited all parameters.
But from some checks I have done, it seems like the shortestpath function doesn't consider the weights I have putted in the graph. In fact I have manually specified some extremely high values for some of the edges of my graph so that the shortestpath function should avoid them in its calculation, but it doen't happen. I specify that no negative values are present in the Graph.Edges.Weight array.
Are there some tricks to consider, or some advice you can give me to solve my trouble or to understand why shortestpath function doesn't work properly?
Thanks in advance!

채택된 답변

Christine Tobler
Christine Tobler 2022년 1월 31일
Hard to say what's going wrong without seeing your code. Here's an example where you can see the weights being used to decide on the path:
g = graph([1 2 3], [2 3 1], [1 2 100]);
plot(g, 'EdgeLabel', g.Edges.Weight)
[path, dist] = shortestpath(g, 1, 3)
path = 1×3
1 2 3
dist = 3
[path, dist] = shortestpath(g, 1, 3, 'Method', 'unweighted')
path = 1×2
1 3
dist = 1
  댓글 수: 5
Christine Tobler
Christine Tobler 2022년 1월 31일
All right, so in that case shortestpath was using these edges with extremely high values because there is no path connecting the two input nodes that doesn't pass through these edges.
Gianluca Angelini
Gianluca Angelini 2022년 2월 4일
Thanks for your support.
I will maintain the line with the rmnedge function in my code in order to avoid the edges with high weights.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 1월 30일
Are you representing your graph as a graph or digraph object or as a biograph object?
How have you stored / represented your weights? If a graph or digraph does your object's edges table G.Edges contain a variable named Weight? [Weights won't do, it must be named exactly Weight.]
What algorithm did you tell shortestpath to use in determining the shortest path? If you're using a biograph did you specify BFS as the method?
  댓글 수: 1
Gianluca Angelini
Gianluca Angelini 2022년 1월 30일
I confirm I have used the graph object and its weights are stored in a G.Edges.Weight array.
Concerning the shortestpath, I have used this:
shortestpath(G,start_id,finish_id,'Method','positive');

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by