-
-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inaccessible Vertices returned when getting distances from DijkstraShortestPathAlgorithm #13
Comments
Hello, I understand your point about inaccessible vertices. QuikGraph is mostly based on the C++ Boost graph library. Concerning the Dijkstra algorithm it seems that it initializes the full set of vertices. Maybe I'm wrong but it seems to be the case. I also looked at BFS implementation. |
But do you think it is strictly necessary to do it? As long as the default distance and color is set when a new vertex is discovered I don't think it is necessary to initialize them at the beginning. |
Well you're right on the fact that not having any distance for the vertex if not reachable seems to be a possible solution as it depicted well the case. BTW not only the Dijktra algorithm can be concerned by such behavior. |
You are right. It seems to be all the shortest path algorithms ( Returning inifinity for an unreachable vertex does make sense too. I have seen that approach in other libraries too. It would be nice if that was a fall back value though. So we don't add all the values when initializing the algorithm but just return that value if it is specifically requested for an unreachable vertex. In That flag can then be set when instantiating To avoid having to change all places where the dictionaries are accessed by indexer we could instantiate the dictionaries as some kind of default value dictionaries where a default value is returned if the key isn't found. Not sure how that would interact with use of |
There are indeed inconsistencies between the shortest path finder algorithms. I think all these should be uniformized or at least the implementation detail requiring some initialization must not be visible in the library API. In some algorithm there is for vertex colors a get like So, in my opinion doing such change require to have a high level vision and not limited to the Dijktra algorithm regading this kind of behavior. The result will be a uniformized API that help user to get in touch with one or another algorithm. Having So to keep an retro compatible API we can think about having a way to specify we want such init and in the same time depreciate the old usage, in order to remove it after. |
I agree. The good thing is that it currently is exposing I think the easiest way to specify the initialization behavior would be through constructors. Either with a flag or some kind of config object (if more flags are needed in the future). Would it make sense to split this ticket up in the following smaller tickets to continue discussion of each part there?:
|
Yeah that's the minimal move I did in order to "hide" implementation but in fact it requires much more harmonization. You're right a flag in constructor would be great indeed in a first approach. That makes sense to split this into several sub tasks. The split you suggested is good enough to work on it. Side note: I was working on a more generic version of graph equate helpers as well as dealing with serialization in other branches. |
I have added #14 and #15 to cover 1 and 3. Would it make sense to join point 2 with 3 since they are dependent on each other? And as a side note: Is it possible for me to either not assign you to a ticket or assign myself to one? Having you assigned to all tickets opened by others is most likely rather confusing for you :-) |
Yes 1 and 3 are quite linked each other. I managed to assign these issues to you, is it ok for you? |
That is completely fine. I should be able to manage that :-) Apparently I need to be designated as a collaborator to be able to assign issues to myself (https://stackoverflow.com/a/26761256) I will close this issue wince the actual implementation will continue in the other tickets. |
Issue
When using
DijkstraShortestPathAlgorithm
to calculate distances to a specific vertex, all vertices in the graph are returned when accessingDijkstraShortestPathAlgorithm.Distances
. Even those vertices that are not accessible from source vertex.I would expect that only the vertices accessible from the source vertex would be included.
Reason
The reason that inaccessible vertices are being returned is that all vertices in the graph are added to
VerticesColors
andDistances
in theInitialize
method inDijkstraShortestPathAlgorithm
:With the current implementation of at least
BreadthFirstSearchAlgorithm.FlushVisitQueue
andShortestPathAlgorithmBase.Relax
it is necessary to add all vertices to the dictionaries since they are expected to exist:Why is this an issue
I see two problems with this:
Distances
dictionary it would be better if no distance was returned for an inaccessible vertex instead of a Double since that a) leads to unnecessary extra logic on the callers side to make sure a vertex is accessible and b) can result in unnecessary looping in case the caller wants to do something with all the key-value pairs in the dictionary.Possible solution
The easiest would be to use
TryGetValue
together with a default value when accessing the dictionaries instead of getting values by key directly:Then there is no need for the looping in
Initialize
.A problem with this approach is that it isn't backwards compatible. Maybe it would be possible to add a flag to the
Initialize
method to pre-populate the dictionaries?:Of course, if this flag only is used in
DijkstraShortestPathAlgorithm
it doesn't make much sense to have it there. Could the flag be added to the constructor directly?The text was updated successfully, but these errors were encountered: