Task: I want to compute a shortest path between a source and target node in a DAG (directed acyclic graph) using Python's graph-tool
efficiently. My DAG has negative weights.
In theory, this is a computationally "easy" problem (i.e., O(V + E)) by first computing a topological sorting of the graph and then visiting and updating parent nodes and distances (e.g. as discussed here).
How can I implement this efficiently using graph-tool
?
My failed attempts so far:
- manually implementing the theoretically efficient algorithm in Python. Since I have to loop over each vertex in the graph, however, this becomes unacceptably slow
- using
shortest_path
function fromgraph-tool
to call the Dijkstra routine fromBoost Graph Library
would have an acceptable running time, but doesn't fully exploit the DAG structure and doesn't work for negative weights anyways - using
shortest_path
to callBellman-Ford
returns a correct shortest path, but doesn't exploit the DAG structure and is too slow (O(VE)).
The efficient DAG shortest path algorithm is implemented as dag_shortest_paths
in the underlying Boost Graph Library. Is there any way of accessing this function through graph-tool
or any other way of computing this efficiently with graph-tool
?