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
shortestPathTo does not adhere to Traversal restrictions #50
Comments
|
Which other traversal restrictions did you see not being handeld by |
Sorry, the only thing I could come up with was not really a traversal:
|
With my above question I intended to clear why you put "Trversal restrictions" (plural) in the issue title. |
More efficient is the following workaround:
|
Does val g2 = Graph(1~2,2~3,3~4,1~4)
g2.get(1).withKind(DepthFirst).pathTo(g2.get(4))
g2.get(1).withKind(DepthFirst).pathTo(g2.get(2))
// res3: Option[g2.Path] = Some(Path(1, 1~4, 4))
// res4: Option[g2.Path] = Some(Path(1, 1~2, 2)) Both give a shortest path. It is not conclusive, but it seems like BFS is used instead? For a simple graph, I would like to use BFS to find a shortest path of maximum length 6 ( alpha.withMaxDepth(6).withKind(BreadthFirst).pathTo(beta) |
|
You may also want to consider using BfsInformer. |
If you have a very big graph, and you know the destination is a bounded distance, then DFS will be much less efficient. How does one use a BfsInformer? |
Take a look at |
What does |
Informers allow you to inspect implementation-specific internal data structures during the traversal. As implementations vary so do these data structures. BFS works internally with a queue so you can inspect that queue containing known but not yet processed pairs of, yes, Since the BFS implementation does not maintain any path, you need to build it on your own. What about the following: While traversing you build a map with (depth, NodeT). Once you got your target node, you could build the path from there back to your root node by selecting predecessors. |
See also |
While I like the idea of building the stack as we go, it is unclear at call time of the BfsInformer what the predecessor of the current node was. Thus it is not possible to build a path. If instead you build the path as in one of your previous comments, you get something like val x = alpha.innerNodeTraverser.withKind(BreadthFirst).withMaxDepth(6)
val visited = mutable.ListBuffer.empty[g.NodeT]
object AllDone extends Exception
try {
x.foreach {
g.ExtendedNodeVisitor((node, count, depth, informer) => {
assert(depth <= 6, "Depth too high")
informer match {
case BfsInformer(queue) =>
visited += node
if(node == beta) {
throw AllDone
}
case _ => throw new RuntimeException("Unexpected informer")
}
})
}
} catch {
case AllDone =>
val subG = g.filter(node => visited.contains(node))
val subA = subG.get(alpha)
val subB = subG.get(beta)
val subPath = subA.shortestPathTo(subB)
// Convert subG.Path to g.Path
} Which is of course also far from ideal. Any suggestions? Please keep in mind high performance requirements. Currently, I run my own implementation of Dijkstra and this line is 50% of my runtime val newPaths = path.head.neighbors.filterNot(x => visited.contains(x.id)).map(_ :: path) Where the |
Hi, withMaxDepth support has now been added to shortestPathTo. Please check out the preview at http://www.scala-graph.org/temp/. (This link is now obsolate.) |
Included in release 1.11.2. |
I want to try and find a shortest path of a constant maximum length. To do this, I tried using the
withMaxDepth
filter for traversals. This works nicely withpathTo
, butshortestPathTo
ignores the restrictions.Example:
I expect that the
shortestPathTo
returnNone
, but instead it returns a complete path.The text was updated successfully, but these errors were encountered: