data-structures · intermediate · ~15 min

Count vertices at distance d

Level sizes in a BFS tree.

Challenge

int bfs_count_at_distance(const int *adj,int n,int src,int d);

Return how many vertices are at exactly BFS distance d from src (d==0 counts just src).

Input format

adj n×n directed; d>=0.

Output format

Count of vertices at distance d.

Constraints

Starter code

#include <stddef.h>
/* Number of nodes whose shortest-path (BFS) distance from src is exactly d. */
int bfs_count_at_distance(const int *adj,int n,int src,int d){ (void)adj;(void)n;(void)src;(void)d; return 0; }

Common mistakes

Counting distance ≤ d instead of exactly d.

Edge cases to handle

d larger than the graph's radius → 0; d==0 → 1.

Background lessons

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