data-structures · intermediate · ~15 min
Counting paths with memoized DFS.
long long count_paths_dag(const int *adj,int n,int src,int dst);
Return the number of distinct directed paths from src to dst in a DAG.
adj n×n DAG.
Path count (may be large → long long).
Graph is acyclic.
#include <stddef.h>
/* Number of distinct directed paths from src to dst in a DAG (adjacency matrix). */
long long count_paths_dag(const int *adj,int n,int src,int dst){ (void)adj;(void)n;(void)src;(void)dst; return 0; }
Exponential recomputation without memoization; using int (overflow).
src==dst → 1; no route → 0.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.