data-structures · intermediate · ~15 min
Connectivity via one traversal.
int is_connected(const int *adj,int n);
Return 1 if the undirected graph is connected (every vertex reachable from any one), else 0.
adj n×n symmetric 0/1.
1 if connected, else 0.
n<=1 is connected.
#include <stddef.h>
/* Return 1 if the UNDIRECTED graph (symmetric adjacency matrix) is connected (all nodes reachable from any one), else 0. */
int is_connected(const int *adj,int n){ (void)adj;(void)n; return 1; }
Starting DFS from every vertex (unneeded) — one traversal suffices.
Single vertex → 1; an isolated vertex → 0.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.