guest@cp-base:~/home/graph$
templates/graph-traversal.cpp
compilable
$cat templates/graph-traversal

Graph Traversal

DFS and BFS with bipartite check, cycle detection, topological sort, and path reconstruction.

[Graph]|
Jul 9, 2026
|explanatory_notes.md|
#dfs#bfs#bipartite#topological-sort#cycle-detection
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 5;
vector<int> adj[MAXN];
bool vis[MAXN];
int dep[MAXN], par[MAXN], color[MAXN], deg[MAXN];
int n, m;

void dfs(int u, int p = -1, int d = 0) {
    vis[u] = true; par[u] = p; dep[u] = d;
    for (int v : adj[u])
        if (!vis[v]) dfs(v, u, d + 1);
}

int bfs(int src, int dest) {
    memset(vis, false, sizeof vis);
    memset(dep, 0x3f, sizeof dep);
    queue<int> q;
    q.push(src); vis[src] = true; dep[src] = 0;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int v : adj[u]) {
            if (!vis[v]) {
                vis[v] = true; par[v] = u;
                dep[v] = dep[u] + 1;
                q.push(v);
            }
        }
    }
    return dep[dest] >= 0x3f3f3f3f ? -1 : dep[dest];
}

bool isBipartite() {
    memset(color, 0, sizeof color);
    for (int i = 1; i <= n; i++) {
        if (color[i]) continue;
        queue<int> q;
        q.push(i); color[i] = 1;
        while (!q.empty()) {
            int u = q.front(); q.pop();
            for (int v : adj[u]) {
                if (!color[v]) { color[v] = -color[u]; q.push(v); }
                else if (color[v] == color[u]) return false;
            }
        }
    }
    return true;
}

bool hasCycle(int u, int p = -1) {
    vis[u] = true;
    for (int v : adj[u]) {
        if (!vis[v]) { if (hasCycle(v, u)) return true; }
        else if (v != p) return true;
    }
    return false;
}

vector<int> topoSort() {
    vector<int> order;
    int indeg[MAXN] = {};
    for (int u = 1; u <= n; u++)
        for (int v : adj[u]) indeg[v]++;
    queue<int> q;
    for (int i = 1; i <= n; i++)
        if (!indeg[i]) q.push(i);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        order.push_back(u);
        for (int v : adj[u])
            if (--indeg[v] == 0) q.push(v);
    }
    return order;
}
75 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)

Graph Traversal

Fundamental DFS and BFS with common graph utilities.

Functions

  • dfs(node) — depth-first traversal, records parent and depth

  • bfs(src, dest) — breadth-first, returns shortest distance in unweighted graph (1-1 if unreachable)

  • isBipartite() — BFS 2-coloring check

  • hasCycle(node) — DFS cycle detection for undirected graphs

  • topologicalSort() — Kahn's algorithm using in-degree queue
  • When to Use

  • Basic graph exploration, connectivity, shortest path in unweighted graphs

  • Bipartite checking (2-colorability)

  • Cycle detection in undirected graphs

  • Topological ordering of DAGs
  • Complexity

  • All traversals — O(V+E)O(V + E)

  • Space — O(V+E)O(V + E)