Running Times for CS140 / CS302 Algorithms
Unless otherwise specified, the data structure will hold n elements.
- Inserting to the beginning of a vector: O(n).
- Appending to the end of a vector: O(1).
- Erasing from the beginning of a vector: O(n).
- Erasing from the end of a vector: O(1).
- Inserting to the beginning of a deque: O(1).
- Appending to the end of a deque: O(1).
- Erasing from the beginning of a deque: O(1).
- Erasing from the end of a deque: O(1).
- Inserting anywhere in a list: O(1).
- Erasing anywhere from a list: O(1).
- Inserting anything in a set or map: O(log n).
- Inserting anywhere in a set or map: O(log n).
- Erasing anywhere from a set or map: O(log n).
- Traversing a set or map: O(n).
- Creating a set or map: O(n log(n)).
- Enumerating permutations: O(n!).
- Enumerating pairs: O(n2).
- Enumerating bit strings: O(2n).
- Find in a disjoint set O(α(n)).
- Union in a disjoint set O(1).
- Push in a heap: O(log(n))
- Pop in a heap: O(log(n))
- Creating a heap O(n).
- Bubble sort, selection sort, worst-case insertion sort, worst-case quicksort: O(n2))
- Heap sort, merge sort, average-case quicksort: O(n log(n))
- Bucket sort, "nearly"-sorted insertion sort: O(n)
- Edges in a dense graph with n nodes: O(n2)
- DFS, BFS, Topological sort in general: O(|V|+|E|)
- DFS, BFS, Topological sort on a connected graph: O(|E|)
- Dijkstra's shortest paths, Prim's minimum spanning tree: O(|E| log |V|)
- Kruskal's minimum spanning tree: O(|E|log |E|).
- Of course, log|E| is O(log|V|), so you don't have to be too
anal about it.
- The Ford-Fulkerson algorithm in general when the max flow is f:
O(f|E|)
- The Edmonds-Karp algorithm for network flow: O(|V||E|2).
- Dynamic programming: You have to analyze each program individually. In
general, if you've done step 2, then the running time is roughly the size of
the cache.