Revizia anterioară Revizia următoare
Numbers everyone should know
Jeff Dean , a famous Google engineer, popularized a list of latency numbers everyone should know. The list is a great resource for designing infrastructure system.
Algorithms and their complexity occur often in critical parts of computer systems, but I find that a lot of even experienced engineers don't have a good understanding about how a O(n!) algorithm compares to a O(n5) one.
In the coding contest world competitors think of these tradeoffs all the time. No wonder, there's a set of numbers every algorithm designer should know.
The table below shows the limits that can be reached in a few seconds by algorithms of different complexities. I've also added a few examples of algorithms or problems and data structures that may occur in those complexity classes.
size of n | complexity | algorithms | data structures |
---|---|---|---|
8 | nn | brute force, cartesian product | |
9-10 | n! | brute force, backtracking, next_permutation | |
13-17 | 3n | dynamic programming with exponential states | hash_map (to store the states) |
15 - 20 | n2 2n | dynamic programming with exponential states | bitsets, hash_map |
15 - 25 | 2n | subset enumeration, brute force, dynamic programming with exponential states | |
25 - 40 | 3n/2, 2n/2 | meet in the middle | hash tables (for set intersection) |
30-50 | n4, n5, n6 | ||
300-500 | n3 | all pairs shortest paths, largest sum submatrix, matrix multiplication, matrix chain multiplication, network flow | |
1000 - 10000 | n2 | largest empty rectangle, Dijkstra, Prim (on dense graphs) | |
30000 | n1.585, n sqrt n | Karatsuba, square root trick | two level tree |
60000 | n log2 n | divide and conquer | 2d range trees |
100000 | n log n | sorting, divide and conquer, sweep line, Kruskal, Dijkstra | segment trees, range trees, heaps, treaps, binary indexed trees, suffix arrays |
1000000 | n, n log log n, n log* n | set intersection, Eratosthenes sieve, radix sort, KMP, topological sort, Euler tour, strongly connected components, 2sat | disjoint sets, tries, hash_map, rolling hash |
1000000000 | log n, sqrt n | binary search, ternary search, fast exponentiation, euclid algorithm |