THE GRAPH FUNCTION DOESN'T CONSIDER WEIGHTS

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!

 Accepted Answer

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 Comments

The code is as simple as that in your example... The only difference is in the number of elements conisdered (but i think it should not create any problem), since s-, t- and weights-arrays from the expression g = graph(s, t, weights) have more or less 400000x1 elements.Indeed I have tried to plot the graph taking into account only part of the arrays, and I have something like this:
The length of the inputs really shouldn't matter.
The first thing you might consider is whether, when those edges you gave extremely high values to are removed, there is still any other path that avoids those edges. You could check this by using rmedge to remove all those edges from the graph, and then call shortestpath again and see if any path is found.
Also, what result are you getting for the length of the path (second output of shortestpath). Is it Inf by any chance?
Hi Christine, I have just tried to follow your suggestions.
By using rmedge, shortestpath function returns an empty value of path and Inf for the lenght of the path consequently.
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.
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.

Sign in to comment.

More Answers (1)

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 Comment

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');

Sign in to comment.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!