Repository navigation
fix: path trees no longer measure from vertices the start cannot reach - #801
Open
OsamaAnsar wants to merge 1 commit into
Open
OsamaAnsar wants to merge 1 commit into
OsamaAnsar wants to merge 1 commit into
Conversation
ShortestPathTree and LongestPathTree read a missing distance as 0, so every vertex acted as a second start. A vertex with no path from the start could shorten or lengthen the distance to another vertex, and hasPathTo and pathTo reported paths over edges that do not exist. LongestPathTree also dropped paths whose length is zero or negative, because it compared them with that same default of 0.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ShortestPathTreeandLongestPathTreesolve the single-source problem, but they read a missing distance as0. Every vertex therefore acts as a second start, and a vertex that the start cannot reach changes the result.LongestPathTreehas the same fault (0 -> 1of weight 1 plus2 -> 1of weight 5 gives a distance of 5 from 0 to 1). Because it also compares every new distance with that default of0, it drops paths whose length is zero or negative, although the class comment says weights can be negative or zero:Change
relaxVertexskips a vertex that has no distance yet. Vertices are relaxed in topological order, so a vertex with no distance at that point has no path from the start.LongestPathTreetakes the first distance it finds for a vertex instead of comparing it with0, andhasPathTochecks that a distance exists instead of checking that it is truthy.getDistTonow givesundefinedfor a vertex with no path, which is what it already gives for a vertex that is not in the graph.hasPathTo(start)staysfalseandpathTo(start)stays[].Tests
spec/shortest_path_tree_spec.tsandspec/longest_path_tree_spec.ts. All 8 fail without the change.npm test: 906 specs, 0 failures.npm run lintis clean.hasPathTo. After it, none do.