data-structures · intermediate · ~15 min

Can src reach every vertex?

Reachability closure from one source.

Challenge

int can_reach_all(const int *adj,int n,int src);

Return 1 if every vertex is reachable from src (directed), else 0.

Input format

adj n×n directed 0/1.

Output format

1 if src reaches all, else 0.

Constraints

src reaches itself.

Starter code

#include <stddef.h>
/* Return 1 if every node is reachable from src (directed graph), else 0. */
int can_reach_all(const int *adj,int n,int src){ (void)adj;(void)n;(void)src; return 1; }

Common mistakes

Treating edges as undirected.

Edge cases to handle

Single vertex → 1; any unreachable vertex → 0.

Background lessons

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