C Mastery / Graph Algorithms: BFS, DFS, Dijkstra, A*, Topological Sort
Part 8 — Data Structures and Algorithms in C

Graph Algorithms: BFS, DFS, Dijkstra, A*, Topological Sort

This chapter implements the fundamental graph algorithms: BFS, DFS, Dijkstra, A*, and topological sort.

Why This Matters

Graphs model networks, dependencies, maps, and state spaces. These algorithms are the building blocks of routing, scheduling, pathfinding, and compiler analysis.

Prerequisites

Core Concept

unweighted graphs.

cycles, topological sort.

queue.

Examples

BFS (unweighted shortest path)

void bfs(Graph *g, int start, int *dist)
{
    for (int i = 0; i < g->V; i++) dist[i] = -1;
    int q[MAXV]; int head = 0, tail = 0;
    q[tail++] = start; dist[start] = 0;
    while (head < tail) {
        int u = q[head++];
        for (Edge *e = g->adj[u]; e; e = e->next) {
            if (dist[e->to] == -1) {
                dist[e->to] = dist[u] + 1;
                q[tail++] = e->to;
            }
        }
    }
}

DFS

void dfs(Graph *g, int u, int *visited)
{
    visited[u] = 1;
    for (Edge *e = g->adj[u]; e; e = e->next)
        if (!visited[e->to])
            dfs(g, e->to, visited);
}

Dijkstra

/* dist[] initialized to INF; dist[src]=0 */
/* repeatedly extract min-dist unvisited vertex, relax edges */

Uses a priority queue for O((V+E) log V).

Topological sort (DFS-based)

void topo_dfs(Graph *g, int u, int *visited, int *order, int *idx)
{
    visited[u] = 1;
    for (Edge *e = g->adj[u]; e; e = e->next)
        if (!visited[e->to])
            topo_dfs(g, e->to, visited, order, idx);
    order[--*idx] = u;
}

How It Works

BFS uses FIFO order to guarantee shortest unweighted paths. DFS uses recursion/ stack to go deep first. Dijkstra greedily fixes the shortest distance to the closest unfinished vertex. A* adds a heuristic to guide the search. Topological sort uses DFS finishing order (or Kahn's algorithm with indegrees).

Variations

Kahn's algorithm (topological sort)

Repeatedly remove vertices with indegree 0, using a queue. Detects cycles if not all vertices are output.

Run BFS from both ends for faster unweighted shortest path.

Common Mistakes

Undefined Behavior

Portability

implemented in earlier chapters.

Under the Hood

BFS/DFS are O(V+E). Dijkstra is O((V+E) log V) with a binary heap. A*'s performance depends on the heuristic. Topological sort is O(V+E).

Practical Usage

Exercises

1. Implement BFS and DFS on an adjacency-list graph. 2. Implement Dijkstra with a priority queue. 3. Implement A* on a grid with a Manhattan heuristic. 4. Implement topological sort with both DFS and Kahn's algorithm.

Deep Challenge

Implement A* on a weighted grid, and compare it to Dijkstra. Explain how an admissible heuristic guarantees optimality and how an inconsistent heuristic affects correctness/performance.

References

Verification

verified.`

Progress

Concept checkboxes

Mastery levels

ConceptCurrent level (0–8)Target level
c.alg.bfs05
c.alg.dfs05
c.alg.dijkstra06
c.alg.astar06
c.alg.toposort05