The Algorithms Behind Modern Software
Introduction
- The Shape of Software Problems
- What This Book Is
- What This Book Is Not
- Prerequisites
- How to Use This Book
- A Note on Rigor
- What Lies Ahead
Chapter 1: What Is a Graph?
- A First Intuition: From Networks to Abstractions
- Vertices, Edges and the Mathematical Definition
- Directed and Undirected Graphs
- Weighted Graphs and Edge Labels
- Paths, Cycles, Walks and Trails
- Connectivity and Connected Graphs
- Trees, Forests and Cycles
- Graph Properties: Degree, Density and Diameter
- Summary
Chapter 2: Representing Graphs in Memory
- Adjacency Lists: Structure and Trade-offs
- Adjacency Matrices: When Density Pays Off
- Edge Lists and Adjacency Arrays
- Implicit and Dynamic Graphs
- Multigraphs, Self-Loops and Parallel Edges
- Space and Time Complexity of Representations
- Implementation: The Base Graph Class
- Summary
Chapter 3: Breadth-First Search
- The Layer-by-Layer Idea
- Queue-Based Implementation
- Reachability and Shortest Paths in Unweighted Graphs
- Level Graphs and Distance Labels
- Reconstructing Shortest Paths
- BFS on Directed Graphs
- Complexity and Invariants
- Summary
Chapter 4: Depth-First Search
- The Recursive Dive
- Iterative DFS and the Call Stack
- Discovery and Finish Times
- The DFS Forest and Edge Classification
- Detecting Cycles with DFS
- Topological Sorting via DFS
- Complexity and Stack Overflow Considerations
- Summary
Chapter 5: Connectivity and Components
- Connected Components in Undirected Graphs
- The Connected Components Algorithm
- Counting Components and Component Sizes
- Bipartite Graphs and Two-Coloring
- Testing Bipartiteness
- Applications: Dependency Partitioning
- Summary
Chapter 6: Trees as Graphs
- Tree Definitions and Characterizations
- Rooted Trees and Parent-Child Relations
- Tree Traversals: Preorder, Inorder, Postorder
- Parent Pointers and Tree Distance
- From Graphs to Spanning Trees
- Forests and Multi-Rooted Structures
- Summary
Chapter 7: Directed Acyclic Graphs and Topological Order
- Directed Cycles and Their Meaning
- Detecting Cycles in Directed Graphs
- Kahn’s Algorithm for Topological Sort
- DFS-Based Topological Sort
- Multiple Valid Orders and Ties
- DAG Dynamic Programming Foundations
- Cycle Reporting: Extracting the Cycle
- Summary
Chapter 8: Strong Connectivity
- What Is Strong Connectivity?
- The Condensation Graph
- Kosaraju’s Algorithm: Two-Pass DFS
- Tarjan’s Algorithm: One-Pass Low Links
- Correctness Arguments for Both
- Choosing Between Kosaraju and Tarjan
- Summary
Chapter 9: Critical Structure: Bridges and Articulation Points
- Articulation Points: Vertices That Break Graphs
- Bridges: Edges That Disconnect Components
- The Low-Link Value Technique
- Finding Bridges and Articulation Points in O(V+E)
- Cut Vertices and Biconnected Components
- Applications in Network Resilience
- Summary
Chapter 10: Shortest Paths in Unweighted Graphs
- BFS as Shortest Path Algorithm
- Single-Source Shortest Paths
- Multi-Source Shortest Paths
- All-Pairs in Unweighted Graphs
- Counting Shortest Paths
- Shortest Path Trees
- Summary
Chapter 11: Dijkstra’s Algorithm
- The Greedy Insight
- Priority Queue Implementation
- Proof of Correctness
- Time Complexity and Heap Choices
- Handling Ties and Determinism
- Early Termination and Bidirectional Search
- Summary
Chapter 12: Negative Weights and Bellman-Ford
- Why Dijkstra Breaks with Negative Edges
- Relaxation and Bellman-Ford
- Negative Cycle Detection
- Complexity Analysis
- Applications: Arbitrage and Potential Functions
- SPFA as a Practical Variant
- Summary
Chapter 13: All-Pairs Shortest Paths and Heuristics
- Floyd-Warshall Algorithm
- Dynamic Programming View of Floyd-Warshall
- Johnson’s Algorithm for Sparse Graphs
- A* Search and Admissible Heuristics
- Consistent vs Admissible Heuristics
- Choosing Between All-Pairs Methods
- Summary
Chapter 14: Minimum Spanning Trees
- The MST Problem
- Cut Property and Cycle Property
- Kruskal’s Algorithm
- Prim’s Algorithm
- Correctness Proofs
- Dense vs Sparse Graph Choices
- Summary
Chapter 15: Disjoint-Set Union
- The Union-Find Problem
- Naive Implementation and Its Failure
- Union by Rank
- Path Compression
- Nearly-Linear Time via Amortized Analysis
- Advanced Variants: Persistent and Rollback DSU
- Summary
Chapter 16: Network Flow
- Flow Networks and the Max Flow Problem
- Cuts and the Max-Flow Min-Cut Theorem
- Ford-Fulkerson Method
- Edmonds-Karp: BFS for Strong Polynomial Time
- Dinic’s Algorithm and Level Graphs
- Complexity Comparisons
- Summary
Chapter 17: Bipartite Matching
- Bipartite Matching Formulation
- Maximum Matching via Max Flow
- Hopcroft-Karp Algorithm
- Alternating Paths and Augmenting Paths
- Applications: Task Assignment and Scheduling
- Summary
Chapter 18: Graph Coloring and Hard Problems
- Vertex Coloring and Chromatic Number
- Greedy Coloring and Its Limits
- Brooks’ Theorem and Special Cases
- NP-Completeness of Coloring
- Approximation Algorithms
- Applications: Register Allocation and Scheduling
- Summary
Chapter 19: Eulerian and Hamiltonian Paths
- Eulerian Paths and Circuits
- Hierholzer’s Algorithm
- Hamiltonian Paths and NP-Hardness
- Approximation and Heuristics
- Applications: Route Planning and DNA Sequencing
- Summary
Chapter 20: Tree Algorithms and LCA
- Lowest Common Ancestor
- Binary Lifting for LCA
- Rooted Tree Queries
- Path Aggregation on Trees
- Tree Isomorphism Basics
- Summary
Chapter 21: Heavy-Light and Centroid Decomposition
- Heavy-Light Decomposition
- Path Decomposition and Chains
- Combining HLD with Segment Trees
- Centroid Decomposition
- Applications: Dynamic Tree Problems
- Summary
Chapter 22: Treewidth, Sparsification and Randomized Algorithms
- Treewidth and Tree Decompositions
- Fixed-Parameter Tractability
- Graph Sparsification
- Randomized Algorithms for Connectivity
- Sketches for Massive Graphs
- Summary
Chapter 23: Dependencies and Build Systems
- Package Dependencies as DAGs
- Dependency Resolution and Conflicts
- Build-System Scheduling
- Circular Dependency Detection
- Incremental Recomputation
- Summary
Chapter 24: Networking and Routing
- Shortest Path Routing
- Link-State Routing and Dijkstra
- OSPF and IS-IS in Practice
- Network Flow for Capacity Planning
- Redundancy and Fault Tolerance
- Summary
Chapter 25: Social Networks and Recommendations
- Social Graphs as Data Structures
- Connectivity and Communities
- PageRank and Random Walks
- Collaborative Filtering as Graph Matching
- Scalability Challenges
- Summary
Chapter 26: Workflow Orchestration and Distributed Systems
- Workflow DAGs
- Scheduling with Dependencies
- Distributed Coordination Graphs
- Failure Recovery and Retries
- Cycle Detection in Distributed State
- Summary
Chapter 27: Algorithm Selection Framework
- Graph Modeling as First Step
- Problem Classification by Graph Type
- Choosing Between Algorithm Families
- Scalability Trade-offs
- Library vs Custom Implementation
- Summary
Chapter 28: Production-Grade Graph Code
- Designing the Graph Library API
- Testing Graph Algorithms
- Benchmarking and Profiling
- Memory Optimization for Large Graphs
- Integrating with NetworkX and Other Libraries
- Summary
Chapter 29: Final Synthesis
- The Unifying View of Graph Algorithms
- Common Patterns and Invariants
- When Graphs Are Not the Right Abstraction
- Emerging Areas: Graph Neural Networks and Beyond
- A Practitioner’s Checklist
- Summary
Conclusion
- Delivering on the Promise
- The Lasting Value
- A Final Note