| Previous Lecture | lect17 | Next Lecture |
lect17,
Complexity Analysis Revisited: Graph Search and std::set
Topics
Complexity of Graph Search
- BFS and DFS both run in O(V + E) ā why?
- Every vertex is visited once (V)
- Every edge is examined once (E)
- Representation matters: adjacency list vs adjacency matrix
- Space complexity: BFS uses a queue (O(V)), DFS uses the call stack (O(V))
Complexity of Mergesort
- Divide: O(log n) levels of recursion
- Conquer: O(n) work at each level
- Total: O(n log n) ā compare to O(n²) for insertion/selection sort
BST Iteration with std::set
std::setis implemented as a balanced BST- Iterating over a
std::setin order is an in-order DFS traversal - In-order traversal runs in O(n) ā visits every node exactly once
- Connecting iterator behavior to the underlying graph structure
Code from lecture
https://github.com/ucsb-cs24-w26/cs24-w26-lectures/tree/main/lect17