data-structures · intermediate · ~15 min
Directed cycle detection with DFS colors.
int has_cycle_directed(const int *adj,int n);
Return 1 if the directed graph contains a cycle, else 0.
adj n×n directed 0/1.
1 if cyclic, else 0.
Self-loops count as cycles if present (tests use none).
#include <stddef.h>
/* Return 1 if the DIRECTED graph (adjacency matrix) contains a cycle, else 0. */
int has_cycle_directed(const int *adj,int n){ (void)adj;(void)n; return 0; }
Using undirected logic; a cross-edge to a finished vertex is not a cycle.
DAG → 0; a→b→a → 1.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.