Graph algorithms, step by step

    Graph algorithms are easiest to learn by watching the frontier grow. Every topic here animates the visited set, the queue or heap, and the distances as they change.

    Learn BFS and DFS first: Dijkstra is BFS with a priority queue, and A* is Dijkstra with a heuristic.

    Traversal

    Shortest paths

    From unweighted to negative edges.

    1. 0-1 BFS (Deque Optimization)Shortest path when edge weights are only 0 or 1. Uses deque instead of priority queue for O(V+E) instead of O(E log V). Critical for grid problems with obstacles.Advanced25m
    2. Dijkstra's AlgorithmA popular algorithm for finding the shortest path between nodes in a graph. It handles weighted edges and guarantees the shortest path for non-negative weights.Advanced30m
    3. A* SearchAn informed search algorithm that uses heuristics to guide the search. It guarantees the shortest path (if heuristic is admissible) and is usually much faster than Dijkstra.Advanced30m
    4. Bellman-Ford AlgorithmFind shortest paths from a source to all vertices, even with negative edge weights. Can also detect negative weight cycles.Advanced30m
    5. Floyd-Warshall AlgorithmFind shortest paths between ALL pairs of vertices. Works with negative weights (but no negative cycles). Simple triple-nested loop.Advanced30m

    Connectivity and structure

    Learn it as part of a path

    Related topics