data-structures · intermediate · ~15 min

Does a path exist? (DFS)

Reachability via DFS.

Challenge

int has_path(const int *adj,int n,int src,int dst);

Return 1 if dst is reachable from src via directed edges (a vertex reaches itself), else 0.

Input format

adj n×n directed; 0<=src,dst<n.

Output format

1 if reachable, else 0.

Constraints

Starter code

#include <stddef.h>
/* Return 1 if there is a directed path from src to dst (src==dst counts as reachable), else 0. */
int has_path(const int *adj,int n,int src,int dst){ (void)adj;(void)n;(void)src;(void)dst; return 0; }

Common mistakes

Not handling src==dst; treating edges as undirected.

Edge cases to handle

src==dst → 1; disconnected → 0.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.