Skip to content

fix: path trees no longer measure from vertices the start cannot reach - #801

Open
OsamaAnsar wants to merge 1 commit into
NaturalNode:masterfrom
OsamaAnsar:fix/path-tree-unreachable-vertices
Open

OsamaAnsar wants to merge 1 commit into
NaturalNode:masterfrom
OsamaAnsar:fix/path-tree-unreachable-vertices

Conversation

@OsamaAnsar

Copy link
Copy Markdown
Contributor

ShortestPathTree and LongestPathTree solve the single-source problem, but they read a missing distance as 0. Every vertex therefore acts as a second start, and a vertex that the start cannot reach changes the result.

const { EdgeWeightedDigraph, ShortestPathTree } = require('natural')

const digraph = new EdgeWeightedDigraph()
digraph.add(0, 1, 5)
digraph.add(2, 1, 1) // no path leads from 0 to 2
digraph.add(2, 3, 1)

const spt = new ShortestPathTree(digraph, 0)
spt.getDistTo(1) // 1, expected 5
spt.hasPathTo(2) // true, expected false
spt.pathTo(3)    // [0, 3], but the graph has no edge 0 -> 3

LongestPathTree has the same fault (0 -> 1 of weight 1 plus 2 -> 1 of weight 5 gives a distance of 5 from 0 to 1). Because it also compares every new distance with that default of 0, it drops paths whose length is zero or negative, although the class comment says weights can be negative or zero:

const digraph = new EdgeWeightedDigraph()
digraph.add(1, 3, -1)

const lpt = new LongestPathTree(digraph, 1)
lpt.hasPathTo(3) // false, expected true
lpt.pathTo(3)    // [], expected [1, 3]

Change

  • relaxVertex skips 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.
  • LongestPathTree takes the first distance it finds for a vertex instead of comparing it with 0, and hasPathTo checks that a distance exists instead of checking that it is truthy.

getDistTo now gives undefined for a vertex with no path, which is what it already gives for a vertex that is not in the graph. hasPathTo(start) stays false and pathTo(start) stays [].

Tests

  • 8 new specs in spec/shortest_path_tree_spec.ts and spec/longest_path_tree_spec.ts. All 8 fail without the change.
  • npm test: 906 specs, 0 failures. npm run lint is clean.
  • I also compared both classes with a brute-force search over every path on 16,724 random DAGs (2 to 7 vertices, integer weights from -5 to 5), which is 33,448 trees. Before the change 29,831 of those trees gave a wrong distance, path or hasPathTo. After it, none do.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant