data-structures · intermediate · ~15 min
Unweighted shortest path via BFS.
int bfs_distance(const int *adj,int n,int src,int dst);
Return the fewest edges on any path src→dst (BFS). src==dst → 0. Unreachable → −1.
adj n×n directed 0/1.
Hop count, or −1.
BFS visits vertices in distance order.
#include <stddef.h>
/* Fewest edges on a directed path from src to dst (BFS). Return the distance, or -1 if unreachable. src==dst -> 0. */
int bfs_distance(const int *adj,int n,int src,int dst){ (void)adj;(void)n;(void)src;(void)dst; return -1; }
Using DFS (finds a path, not the shortest); forgetting the −1 case.
src==dst → 0; unreachable → −1.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.