data-structures · intermediate · ~15 min
Memoized DFS / DP over a DAG.
int longest_path_dag(const int *adj,int n);
Return the number of edges on the longest directed path in a DAG (0 if there are no edges).
adj n×n DAG.
Longest path length in edges.
Graph is acyclic.
#include <stddef.h>
/* Length (in edges) of the longest path in a DAG (directed acyclic graph, adjacency matrix). */
int longest_path_dag(const int *adj,int n){ (void)adj;(void)n; return 0; }
Recomputing subpaths (exponential) instead of memoizing.
No edges → 0; a simple chain of k edges → k.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.