Algorithms
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.
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:
- Start from source vertex and mark as visited
- Add source to queue
- 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:
- Start from source vertex and mark as visited
- 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:
- Initialize distances: source = 0, all others = ∞
- Add all vertices to priority queue
- 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:
- Initialize distances: source = 0, others = ∞
- Repeat V-1 times:
- For each edge (u,v), relax if possible
- 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:
- Sort all edges by weight
- Initialize Union-Find structure
- 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:
- Start with arbitrary vertex
- Add all edges from current tree to priority queue
- 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
| Algorithm | Time Complexity | Space Complexity | Use Case |
|---|---|---|---|
| BFS | O(V + E) | O(V) | Shortest path (unweighted) |
| DFS | O(V + E) | O(V) | Cycle detection, topological sort |
| Dijkstra | O((V + E) log V) | O(V) | Shortest path (non-negative weights) |
| Bellman-Ford | O(VE) | O(V) | Shortest path (negative weights) |
| Kruskal's MST | O(E log E) | O(V) | Minimum spanning tree |
| Prim's MST | O(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
- Early Termination: Stop when target is found
- Bidirectional Search: Search from both ends
- Heuristics: A* algorithm for pathfinding
- Preprocessing: Precompute distances for queries
Common Pitfalls
- Negative Cycles: Use Bellman-Ford for detection
- Disconnected Graphs: Handle unreachable vertices
- Self-loops: Consider in algorithm design
- 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:
- Compare BFS vs DFS: Same graph, different exploration patterns
- Shortest Path: Use Dijkstra with different start/end points
- MST Comparison: Run both Kruskal's and Prim's on same graph
- 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
- Introduction to Algorithms (CLRS)
- Graph Theory and Applications
- Network Analysis in Python
- Competitive Programming Handbook
Continue your algorithm journey with Pathfinding Algorithms Visualization and Sorting Algorithms.