Algorithms · Visualized

Graph Algorithms

Graph Algorithms

Traverse, order, and find shortest paths and spanning trees on graphs — the machinery behind maps, networks, schedulers and dependency resolvers.

Big-O: Traversals O(V + E); Dijkstra O(E log V) with a heap; Bellman–Ford O(V·E); Floyd–Warshall O(V³); MST O(E log E).

📖 Family Introduction

Graph Algorithms

BFS, DFS and their weighted descendants — the vocabulary of networks and dependencies.

A graph G = (V, E) models pairwise relations; most graph algorithms are a traversal plus a bookkeeping rule. BFS explores by layers through a queue and gives shortest paths when all edges weigh the same; DFS dives depth-first through a stack/recursion and powers component detection, topological ordering and cycle detection. Weighted shortest paths need Dijkstra (greedy with a priority queue, non-negative weights) or Bellman–Ford (relaxation rounds, tolerates negatives and detects negative cycles). Minimum spanning trees come from Prim or Kruskal, and Floyd–Warshall computes all-pairs distances by dynamic programming over intermediate vertices.

The general recipe

  1. Choose a representation: adjacency list O(V + E) memory for sparse graphs, matrix O(V²) for dense.
  2. Pick the traversal that matches the question: layer order (BFS), deep exploration/back-edges (DFS), or relaxation (Dijkstra/Bellman–Ford).
  3. Carry the bookkeeping: visited/colour sets, parent pointers, distance tables — and initialize distances to ∞ with dist[source] = 0.
  4. Handle disconnected graphs by iterating the traversal from every unvisited vertex.
  5. Validate edge cases: directed vs undirected insertion, self-loops, parallel edges, zero-weight edges.

Vocabulary you'll meet everywhere here

BFS
queue-driven layer order; shortest paths in unweighted graphs, O(V + E)
DFS
stack/recursion dive; components, cycles, topological sort
Topological sort (Kahn)
repeatedly remove indegree-0 vertices; only possible on DAGs
Dijkstra
priority-queue greedy on non-negative weights, O(E log V) with a binary heap
Bellman–Ford
relax all edges V − 1 times, O(VE); a further relaxing edge proves a negative cycle
MST (Prim/Kruskal)
greedy minimum spanning tree via growth (Prim) or sorted-edge union-find (Kruskal)

Where this family shows up

  • Maps and routing (Dijkstra/A*), network packet routing, social-network degrees of separation (BFS)
  • Build systems and course prerequisites (topological order), dependency cycles
  • Cheapest cabling/clustering backbones (MST), currency-arbitrage checks (negative cycles)

Common pitfalls

  • Missing visited handling — DFS on a cyclic graph recurses forever
  • Running Dijkstra with negative edges: its "settled" guarantee breaks; use Bellman–Ford
  • Forgetting the graph may be disconnected — one traversal from one source is not the whole story
  • Directed vs undirected edge insertion (adding both directions when you must not, or one when you must)
Python · Graph Algorithms in Code
from collections import deque

def bfs(graph, start):
    visited = [False] * len(graph)
    dist = {start: 0}
    q = deque([start])
    order = []
    while q:
        u = q.popleft()
        order.append(u)
        for v in graph[u]:
            if not visited[v] and v not in dist:
                visited[v] = True
                dist[v] = dist[u] + 1
                q.append(v)
    return order, dist

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.