CS 351: Analysis of Algorithms
What We’ll Discuss This Week
When you hear “graph,” you might think of x-y plots, but in computer science, graphs represent relationships between objects.
Key Insight: Graphs model pairwise relationships between objects
Mathematical Plot
Graph Data Structure
Vertices (or Nodes):
Edges:
Notation: G = (V, E) where V = vertices, E = edges
Undirected Graph
Directed Graph
For any graph G = (V, E):
Example: Social network graph
Implementation:
Key Features:
Use Cases:
Two key metrics:
Fundamental bounds:
Sparse Graph: m ≈ O(n)
Dense Graph: m ≈ O(n²)
Mathematical Definition:
Practical Examples:
Adjacency Lists:
Adjacency Matrix:
Core Components:
Space Complexity: Θ(m + n)
Breakdown:
Total: Θ(n + m + m + m) = Θ(m + n)
Graph:
Adjacency List Representation:
Each vertex stores list of neighbors
Structure: n × n matrix A where:
Undirected graphs: A[i,j] = A[j,i] (symmetric)
Directed graphs: A[i,j] ≠ A[j,i] generally
Graph:
Matrix Representation:
1 2 3
1 [ 0 1 1 ]
2 [ 1 0 1 ]
3 [ 1 1 0 ]
Space Complexity: Θ(n²)
Characteristics:
Operation | Adjacency List | Adjacency Matrix |
---|---|---|
Space | Θ(m + n) | Θ(n²) |
Edge lookup | O(degree(v)) | O(1) |
Add edge | O(1) | O(1) |
Remove edge | O(degree(v)) | O(1) |
Find neighbors | O(degree(v)) | O(n) |
Use Adjacency Lists when:
Use Adjacency Matrix when:
Adjacency Lists:
Adjacency Matrix:
For an undirected graph with n vertices, what is the maximum possible number of edges?
Which representation is better for a sparse graph with 1000 vertices and 1500 edges?
In an adjacency list representation, how many operations are needed to check if edge (u,v) exists?
Graph Search Algorithms:
Shortest Path Algorithms:
Real-world example: Facebook social graph
Clear winner: Adjacency lists!
Common implementations:
Industry standard: Adjacency lists with optimizations
Graph Search Algorithms (Chapter 8):
Shortest Path Algorithms (Chapter 9):
All upcoming algorithms are “for-free primitives”:
Key insight: Linear-time graph algorithms are incredibly powerful!
Big Data Graphs:
Machine Learning on Graphs:
Graph Fundamentals:
Representation Trade-offs:
When designing with graphs:
Build on this foundation:
Social Networks
Applications: