Graph Algorithms Visualization: BFS, DFS, Dijkstra & More

Work through the core graph algorithms: breadth-first search, depth-first search, Dijkstra's algorithm, and minimum spanning trees.

·20 min
Graph AlgorithmsBFSDFSDijkstraMSTVisualization

Graph algorithms are essential for solving complex problems in computer science, from social networks to GPS navigation. This comprehensive guide explores the most important graph algorithms, tracing exactly how each one works step by step.

Understanding Graphs

A graph is a collection of vertices (nodes) connected by edges. Graphs can represent:

  • Social Networks: People connected by friendships
  • Transportation: Cities connected by roads
  • Internet: Websites connected by links
  • Dependencies: Tasks connected by prerequisites

Graph Types

  • Directed vs Undirected: Whether edges have direction
  • Weighted vs Unweighted: Whether edges have associated costs
  • Connected vs Disconnected: Whether all nodes are reachable
  • Cyclic vs Acyclic: Whether the graph contains cycles

Graph Traversal Algorithms

Breadth-First Search (BFS)

BFS explores the graph level by level, visiting all neighbors before moving to the next level. It uses a queue data structure.

Graph algorithm visualization

This article originally embedded an interactive graph algorithm visualization. The component has not been ported to this version of the site yet — the explanation below stands on its own in the meantime.

Key Characteristics:

  • Time Complexity: O(V + E) where V = vertices, E = edges
  • Space Complexity: O(V) for the queue
  • Guarantees: Finds shortest path in unweighted graphs
  • Data Structure: Queue (FIFO - First In, First Out)

Algorithm Steps:

  1. Start from source vertex and mark as visited
  2. Add source to queue
  3. While queue is not empty:
    • Dequeue a vertex
    • Visit all unvisited neighbors
    • Mark neighbors as visited and enqueue them

Applications:

  • Shortest Path: In unweighted graphs
  • Level-order Traversal: Tree traversal by levels
  • Connected Components: Finding all connected nodes
  • Bipartite Testing: Checking if graph is bipartite
  • Web Crawling: Systematic exploration of web pages

Depth-First Search (DFS)

DFS explores as far as possible along each branch before backtracking. It uses a stack (or recursion).

Graph algorithm visualization

This article originally embedded an interactive graph algorithm visualization. The component has not been ported to this version of the site yet — the explanation below stands on its own in the meantime.

Key Characteristics:

  • Time Complexity: O(V + E)
  • Space Complexity: O(V) for the stack/recursion
  • Exploration: Goes deep before exploring siblings
  • Data Structure: Stack (LIFO - Last In, First Out) or recursion

Algorithm Steps:

  1. Start from source vertex and mark as visited
  2. For each unvisited neighbor:
    • Recursively apply DFS
    • Mark as visited when processing

Applications:

  • Topological Sorting: Ordering tasks with dependencies
  • Cycle Detection: Finding cycles in directed graphs
  • Path Finding: Finding any path between vertices
  • Maze Solving: Exploring all possible paths
  • Connected Components: In undirected graphs

DFS Variations:

  • Pre-order: Process vertex before children
  • Post-order: Process vertex after children
  • In-order: For binary trees specifically

Shortest Path Algorithms

Dijkstra's Algorithm

Finds the shortest path from a source vertex to all other vertices in a weighted graph with non-negative weights.

Graph algorithm visualization

This article originally embedded an interactive graph algorithm visualization. The component has not been ported to this version of the site yet — the explanation below stands on its own in the meantime.

Key Characteristics:

  • Time Complexity: O((V + E) log V) with binary heap
  • Space Complexity: O(V) for distance array and priority queue
  • Requirement: Non-negative edge weights
  • Guarantee: Finds optimal shortest paths

Algorithm Steps:

  1. Initialize distances: source = 0, all others = ∞
  2. Add all vertices to priority queue
  3. While priority queue is not empty:
    • Extract vertex with minimum distance
    • For each neighbor, try to relax the edge
    • Update distance if shorter path found

Relaxation Process:

if distance[u] + weight(u,v) < distance[v]:
    distance[v] = distance[u] + weight(u,v)
    previous[v] = u

Applications:

  • GPS Navigation: Finding shortest routes
  • Network Routing: Internet packet routing protocols
  • Social Networks: Finding degrees of separation
  • Game AI: Pathfinding for NPCs
  • Flight Planning: Cheapest flight connections

Optimizations:

  • A Algorithm*: Add heuristic for faster pathfinding
  • Bidirectional Search: Search from both ends
  • Goal-directed: Stop when target is reached

Bellman-Ford Algorithm

Handles negative edge weights and detects negative cycles.

Graph algorithm visualization

This article originally embedded an interactive graph algorithm visualization. The component has not been ported to this version of the site yet — the explanation below stands on its own in the meantime.

Key Characteristics:

  • Time Complexity: O(VE)
  • Space Complexity: O(V)
  • Advantage: Handles negative weights
  • Detection: Can detect negative cycles

Algorithm Steps:

  1. Initialize distances: source = 0, others = ∞
  2. Repeat V-1 times:
    • For each edge (u,v), relax if possible
  3. Check for negative cycles:
    • If any edge can still be relaxed, negative cycle exists

Applications:

  • Currency Arbitrage: Detecting profitable exchange cycles
  • Network Protocols: Routing with negative costs
  • Game Theory: Finding optimal strategies
  • Economics: Market analysis with negative returns

Minimum Spanning Tree (MST)

A spanning tree that connects all vertices with minimum total edge weight.

Kruskal's Algorithm

Builds MST by adding edges in order of increasing weight, avoiding cycles.

Graph algorithm visualization

This article originally embedded an interactive graph algorithm visualization. The component has not been ported to this version of the site yet — the explanation below stands on its own in the meantime.

Key Characteristics:

  • Time Complexity: O(E log E) for sorting edges
  • Space Complexity: O(V) for Union-Find structure
  • Approach: Edge-based greedy algorithm
  • Data Structure: Union-Find (Disjoint Set Union)

Algorithm Steps:

  1. Sort all edges by weight
  2. Initialize Union-Find structure
  3. For each edge in sorted order:
    • If edge connects different components, add to MST
    • Union the components

Union-Find Operations:

  • Find: Determine which component a vertex belongs to
  • Union: Merge two components
  • Path Compression: Optimize find operations
  • Union by Rank: Optimize union operations

Prim's Algorithm

Builds MST by growing the tree from a starting vertex.

Graph algorithm visualization

This article originally embedded an interactive graph algorithm visualization. The component has not been ported to this version of the site yet — the explanation below stands on its own in the meantime.

Key Characteristics:

  • Time Complexity: O(E log V) with binary heap
  • Space Complexity: O(V) for priority queue
  • Approach: Vertex-based greedy algorithm
  • Data Structure: Priority queue

Algorithm Steps:

  1. Start with arbitrary vertex
  2. Add all edges from current tree to priority queue
  3. While tree doesn't span all vertices:
    • Extract minimum weight edge that adds new vertex
    • Add vertex and edge to MST

Applications of MST:

  • Network Design: Minimum cost to connect all nodes
  • Circuit Design: Connecting components with minimum wire
  • Clustering: Hierarchical clustering algorithms
  • Image Segmentation: Computer vision applications
  • Approximation Algorithms: For traveling salesman problem

Advanced Graph Algorithms

Topological Sorting

Orders vertices in a directed acyclic graph (DAG) such that for every edge (u,v), u comes before v.

Applications:

  • Task Scheduling: Dependencies between tasks
  • Course Prerequisites: Academic course planning
  • Build Systems: Compilation order
  • Spreadsheet Calculations: Formula dependencies

Strongly Connected Components

Finds maximal sets of vertices where every vertex is reachable from every other vertex.

Applications:

  • Social Network Analysis: Finding tight-knit groups
  • Web Graph Analysis: Identifying web communities
  • Compiler Optimization: Variable dependency analysis

Maximum Flow

Finds the maximum flow from source to sink in a flow network.

Applications:

  • Network Capacity: Maximum data throughput
  • Transportation: Maximum goods flow
  • Matching Problems: Bipartite matching
  • Image Segmentation: Computer vision

Performance Comparison

AlgorithmTime ComplexitySpace ComplexityUse Case
BFSO(V + E)O(V)Shortest path (unweighted)
DFSO(V + E)O(V)Cycle detection, topological sort
DijkstraO((V + E) log V)O(V)Shortest path (non-negative weights)
Bellman-FordO(VE)O(V)Shortest path (negative weights)
Kruskal's MSTO(E log E)O(V)Minimum spanning tree
Prim's MSTO(E log V)O(V)Minimum spanning tree

Implementation Considerations

Graph Representation

Adjacency Matrix:

  • Space: O(V²)
  • Edge lookup: O(1)
  • Good for dense graphs

Adjacency List:

  • Space: O(V + E)
  • Edge lookup: O(degree)
  • Good for sparse graphs

Optimization Techniques

  1. Early Termination: Stop when target is found
  2. Bidirectional Search: Search from both ends
  3. Heuristics: A* algorithm for pathfinding
  4. Preprocessing: Precompute distances for queries

Common Pitfalls

  1. Negative Cycles: Use Bellman-Ford for detection
  2. Disconnected Graphs: Handle unreachable vertices
  3. Self-loops: Consider in algorithm design
  4. Parallel Edges: Multiple edges between same vertices

Real-World Applications

Social Networks

  • Friend Recommendations: Finding mutual connections
  • Influence Propagation: How information spreads
  • Community Detection: Finding groups of users

Transportation

  • GPS Navigation: Shortest route calculation
  • Traffic Optimization: Managing traffic flow
  • Public Transit: Optimal route planning

Computer Networks

  • Routing Protocols: Internet packet routing
  • Network Topology: Designing efficient networks
  • Load Balancing: Distributing network traffic

Game Development

  • AI Pathfinding: NPC movement
  • Game State Search: Finding optimal moves
  • Procedural Generation: Creating game worlds

Bioinformatics

  • Protein Folding: Molecular structure analysis
  • Gene Networks: Understanding genetic relationships
  • Phylogenetic Trees: Evolutionary relationships

Interactive Exercises

Try working these through by hand:

  1. Compare BFS vs DFS: Same graph, different exploration patterns
  2. Shortest Path: Use Dijkstra with different start/end points
  3. MST Comparison: Run both Kruskal's and Prim's on same graph
  4. Negative Weights: See how Bellman-Ford handles them

Advanced Topics

Parallel Graph Algorithms

  • Parallel BFS: Level-synchronous approach
  • Parallel Dijkstra: Delta-stepping algorithm
  • Distributed Graphs: Algorithms for large-scale graphs

Dynamic Graphs

  • Incremental Algorithms: Handle edge additions
  • Decremental Algorithms: Handle edge deletions
  • Fully Dynamic: Handle both additions and deletions

Approximation Algorithms

  • TSP Approximation: Using MST for traveling salesman
  • Vertex Cover: Approximating minimum vertex cover
  • Max Cut: Approximating maximum cut

Conclusion

Graph algorithms are powerful tools for solving complex problems across many domains. Key takeaways:

  • Choose the right algorithm based on your specific requirements
  • Understand time/space tradeoffs for different approaches
  • Consider graph properties (directed, weighted, sparse/dense)
  • Practice with visualizations to build intuition

Tracing how these algorithms explore graphs and make decisions makes it easier to apply them to real-world problems.

Further Reading


Continue your algorithm journey with Pathfinding Algorithms Visualization and Sorting Algorithms.