dag_longest_path

dag_longest_path#

graph_tool.structure.dag_longest_path(g, weight=None)[git][source]#

Return a longest path of a directed acyclic graph.

Parameters:
gGraph

Directed acyclic graph.

weightEdgePropertyMap (optional, default: None)

Edge lengths. If not given, every edge has length one.

Returns:
pathnumpy.ndarray

The vertices of the path.

lengthfloat

Its total length.

See also

topological_sort

A topological ordering

topological_generations

Topological generation of every vertex

shortest_path

Shortest path

Notes

The path returned is a directed path \(P\) that maximizes its length \(\sum_{e\in P} w_e\), where \(w_e\) is the length of edge \(e\) (one, if no lengths are given), among all directed paths of the graph, including those of a single vertex, which have length zero. The longest distances are computed by dynamic programming in topological order, in time \(O(V + E)\). Paths of negative length are never extended (the longest path to a vertex is then the vertex itself), so that the path can start anywhere. The problem is NP-hard for graphs with cycles, for which a ValueError is raised.

Examples

>>> g = gt.Graph([(0, 1), (0, 2), (1, 3), (2, 3), (3, 4), (0, 4)])
>>> print(gt.dag_longest_path(g))
(array([0, 1, 3, 4], dtype=uint64), 3.0)