dag_longest_path#
- graph_tool.structure.dag_longest_path(g, weight=None)[git][source]#
Return a longest path of a directed acyclic graph.
- Parameters:
- g
Graph Directed acyclic graph.
- weight
EdgePropertyMap(optional, default:None) Edge lengths. If not given, every edge has length one.
- g
- Returns:
- path
numpy.ndarray The vertices of the path.
- length
float Its total length.
- path
See also
topological_sortA topological ordering
topological_generationsTopological generation of every vertex
shortest_pathShortest 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
ValueErroris 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)