Project 10: Graph Algorithms Suite

Build a toolkit of core graph algorithms with visualizations and tests.

Quick Reference

Attribute Value
Difficulty Level 3: Advanced
Time Estimate 3-4 weeks
Language Python (Alternatives: C++, Java, Go)
Prerequisites Projects 6 and 9, recursion, priority queue
Key Topics Graphs, BFS/DFS, shortest paths, MST

1. Learning Objectives

By completing this project, you will:

  1. Implement graph representations (adjacency list/matrix).
  2. Implement BFS, DFS, Dijkstra, Bellman-Ford, and MST algorithms.
  3. Visualize graph traversal and shortest paths.
  4. Analyze complexity and choose appropriate algorithms.

2. Theoretical Foundation

2.1 Core Concepts

  • Graph representation: Lists are efficient for sparse graphs; matrices for dense.
  • Traversal: BFS explores levels, DFS explores depth.
  • Shortest paths: Dijkstra for non-negative weights, Bellman-Ford for negatives.
  • MST: Kruskal and Prim minimize total edge weight.

2.2 Why This Matters

Graphs model real-world relationships: networks, maps, dependencies, social graphs. Algorithms here are foundational for routing and optimization.

2.3 Historical Context / Background

Dijkstra (1950s) introduced shortest paths; Kruskal and Prim developed MST algorithms soon after.

2.4 Common Misconceptions

  • Misconception: BFS works for weighted graphs. Reality: Only for unweighted graphs.
  • Misconception: Dijkstra handles negative edges. Reality: Use Bellman-Ford for negatives.

3. Project Specification

3.1 What You Will Build

A CLI and library that can load graphs, run traversals, compute shortest paths, and output paths and costs. Optional graphviz output for visualization.

3.2 Functional Requirements

  1. Representations: Adjacency list and matrix.
  2. Traversals: BFS and DFS with visitation order.
  3. Shortest paths: Dijkstra and Bellman-Ford.
  4. MST: Prim or Kruskal.
  5. Visualization: Export to DOT format.

3.3 Non-Functional Requirements

  • Performance: Use heap-based priority queue for Dijkstra.
  • Reliability: Detect negative cycles in Bellman-Ford.
  • Usability: CLI flags for algorithm selection.

3.4 Example Usage / Output

$ ./graph-tool --algo dijkstra --start A
Path A->D: A B D (cost 7)
$ ./graph-tool --algo mst
MST cost: 12

3.5 Real World Outcome

You will load a graph from a text file, run algorithms, and see traversal order or shortest path results. DOT exports let you view the graph with Graphviz.


4. Solution Architecture

4.1 High-Level Design

┌───────────┐     ┌──────────────┐     ┌──────────────┐
│ File/CLI  │────▶│ Graph Core   │────▶│ Algorithms   │
└───────────┘     └──────┬───────┘     └──────┬───────┘
                            │                │
                      ┌─────▼─────┐    ┌──────▼──────┐
                      │ Visualizer│    │ Reporter    │
                      └───────────┘    └─────────────┘

4.2 Key Components

Component Responsibility Key Decisions
graph.py Data structure List vs matrix
algorithms.py BFS/DFS/Dijkstra/MST Shared adjacency API
io.py Load graph from file Simple text format
viz.py DOT export Optional dependency

4.3 Data Structures

# adjacency list
adj = {
    "A": [("B", 3), ("C", 5)],
    "B": [("D", 4)],
}

4.4 Algorithm Overview

Key Algorithm: Dijkstra (non-negative weights)

  1. Initialize distances to infinity.
  2. Use min-heap to pick closest node.
  3. Relax edges.

Complexity Analysis:

  • Time: O((V + E) log V)
  • Space: O(V)

5. Implementation Guide

5.1 Development Environment Setup

python3 -m venv .venv
. .venv/bin/activate
pip install networkx graphviz

5.2 Project Structure

graph-tool/
├── src/
│   ├── graph.py
│   ├── algorithms.py
│   ├── io.py
│   ├── viz.py
│   └── cli.py
├── tests/
└── README.md

5.3 The Core Question You’re Answering

“Which graph algorithm fits which problem, and why?”

5.4 Concepts You Must Understand First

Stop and research these before coding:

  1. Graph representations
  2. Edge relaxation
  3. Union-find for Kruskal

5.5 Questions to Guide Your Design

  1. How will you represent directed vs undirected graphs?
  2. How will you parse weighted edges from input?
  3. How will you export paths for visualization?

5.6 Thinking Exercise

Given a weighted graph with 5 nodes, run Dijkstra by hand and record the priority queue at each step.

5.7 The Interview Questions They’ll Ask

  1. Why does BFS find shortest path in unweighted graphs?
  2. When should you use Bellman-Ford?
  3. Explain MST vs shortest path tree.

5.8 Hints in Layers

Hint 1: Start with adjacency list It is simplest and handles sparse graphs well.

Hint 2: Use heapq Python’s heapq makes Dijkstra easy.

Hint 3: Separate graph core from algorithms Keeps code reusable.

5.9 Books That Will Help

Topic Book Chapter
Graphs “Algorithms, Fourth Edition” Ch. 4.1
BFS/DFS “Introduction to Algorithms” Ch. 22
Shortest paths “Introduction to Algorithms” Ch. 24
MST “Introduction to Algorithms” Ch. 23

5.10 Implementation Phases

Phase 1: Foundation (5-7 days)

Goals:

  • Build graph representations
  • Implement BFS and DFS

Tasks:

  1. Implement adjacency list and matrix.
  2. Add BFS/DFS traversal output.

Checkpoint: Traversal order matches expected.

Phase 2: Shortest Paths (7-10 days)

Goals:

  • Implement Dijkstra and Bellman-Ford
  • Add path reconstruction

Tasks:

  1. Implement priority queue-based Dijkstra.
  2. Add negative cycle detection.

Checkpoint: Shortest paths match known examples.

Phase 3: MST and Visualization (5-7 days)

Goals:

  • Implement MST
  • Add DOT export

Tasks:

  1. Implement Prim or Kruskal.
  2. Export graph and MST to DOT.

Checkpoint: MST cost matches expected.

5.11 Key Implementation Decisions

Decision Options Recommendation Rationale
Graph format list, matrix list Sparse-friendly
MST Prim, Kruskal Kruskal Clean with union-find
Visualization optional yes Helps intuition

6. Testing Strategy

6.1 Test Categories

Category Purpose Examples
Unit Tests Core algorithms Known graphs
Integration Tests CLI + IO Load from file
Edge Case Tests Disconnected graphs Multiple components

6.2 Critical Test Cases

  1. Graph with negative edges but no negative cycles.
  2. Disconnected graph for BFS/DFS.
  3. Graph with multiple equal shortest paths.

6.3 Test Data

A B 3
A C 5
B D 4

7. Common Pitfalls and Debugging

7.1 Frequent Mistakes

Pitfall Symptom Solution
Using BFS on weighted graph Incorrect distances Use Dijkstra
Forgetting to relax edges Wrong path cost Check relaxation loop
Missing negative cycle detection Infinite updates Add final pass

7.2 Debugging Strategies

  • Print distances after each relaxation.
  • Compare with known examples from textbooks.

7.3 Performance Traps

Using a matrix for sparse graphs leads to wasted memory and slow scans.


8. Extensions and Challenges

8.1 Beginner Extensions

  • Add topological sort for DAGs.
  • Add path reconstruction for BFS.

8.2 Intermediate Extensions

  • Implement A* search.
  • Add dynamic graph updates.

8.3 Advanced Extensions

  • Implement Johnson’s algorithm for all-pairs shortest paths.
  • Add visualization of algorithm steps.

9. Real-World Connections

9.1 Industry Applications

  • Routing and navigation systems
  • Dependency resolution in build systems
  • networkx: Python graph algorithms.
  • Boost Graph Library: C++ graph toolkit.

9.3 Interview Relevance

Graph problems are common in interviews, especially BFS/DFS and shortest path variants.


10. Resources

10.1 Essential Reading

  • “Algorithms, Fourth Edition” by Sedgewick - Ch. 4.1
  • “Introduction to Algorithms” - Ch. 22-24

10.2 Video Resources

  • Graph algorithm visualizations on YouTube

10.3 Tools and Documentation

  • Graphviz: https://graphviz.org
  • networkx: https://networkx.org

11. Self-Assessment Checklist

11.1 Understanding

  • I can explain BFS vs DFS use cases.
  • I can explain why Dijkstra needs non-negative edges.
  • I can explain MST vs shortest path.

11.2 Implementation

  • Algorithms pass tests on known graphs.
  • CLI outputs are correct.

11.3 Growth

  • I can choose the right graph algorithm for a problem.

12. Submission / Completion Criteria

Minimum Viable Completion:

  • BFS, DFS, Dijkstra
  • Graph loader

Full Completion:

  • Bellman-Ford and MST
  • DOT export

Excellence (Going Above and Beyond):

  • A* and all-pairs shortest paths
  • Step-by-step visual playback

This guide was generated from LEARN_ALGORITHMS_DEEP_DIVE.md. For the complete learning path, see the parent directory.