Update 0_Silver_Complexity.md

This commit is contained in:
Darren Yao 2020-06-08 16:39:28 -07:00 committed by GitHub
parent 8e8a0662d2
commit 0cac54ec5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,10 +1,10 @@
\chapter{Time/Space Complexity and Algorithm Analysis}
# Time/Space Complexity and Algorithm Analysis
In programming contests, there is a strict limit on program runtime. This means that in order to pass, your program needs to finish running within a certain timeframe. For USACO, this limit is 4 seconds for Java submissions. A conservative estimate for the number of operations the grading server can handle per second is {10}^8 (but is really closer to 5 \cdot 10^8 given good constant factors).
\section{Big O Notation and Complexity Calculations}
# Big O Notation and Complexity Calculations
We want a method of how many operations it takes to run each algorithm, in terms of the input size n. Fortunately, this can be done relatively easily using Big O notation, which expresses worst-case complexity as a function of n, as n gets arbitrarily large. Complexity is an upper bound for the number of steps an algorithm requires, as a function of the input size. In Big O notation, we denote the complexity of a function as O(f(n)), where f(n) is a function without constant factors or lower-order terms. We'll see some examples of how this works, as follows.
The following code is O(1), because it executes a constant number of operations.
@ -91,43 +91,36 @@ for(int i = 1; i <= n; i++){
}
```
\section{Common Complexities and Constraints}
# Common Complexities and Constraints
Complexity factors that come from some common algorithms and data structures are as follows:
- Mathematical formulas that just calculate an answer: O(1)
- Unordered set/map: O(1) per operation
- Binary search: O(log n)
- Ordered set/map or priority queue: O(log n) per operation
- Prime factorization of an integer, or checking primality or compositeness of an integer: O(\sqrt{n})
- Reading in n items of input: O(n)
- Iterating through an array or a list of n elements: O(n)
- Sorting: usually O(n log n) for default sorting algorithms (mergesort, for example Collections.sort or Arrays.sort on objects)
- Java Quicksort Arrays.sort function on primitives on pathological worst-case data sets, don't use this in CodeForces rounds
- Iterating through all subsets of size k of the input elements: O(n^k). For example, iterating through all triplets is O(n^3).
- Iterating through all subsets: O(2^n)
- Iterating through all permutations: O(n!)
- Mathematical formulas that just calculate an answer: O(1)
- Unordered set/map: O(1) per operation
- Binary search: O(log n)
- Ordered set/map or priority queue: O(log n) per operation
- Prime factorization of an integer, or checking primality or compositeness of an integer: O(\sqrt{n})
- Reading in n items of input: O(n)
- Iterating through an array or a list of n elements: O(n)
- Sorting: usually O(n log n) for default sorting algorithms (mergesort, for example Collections.sort or Arrays.sort on objects)
- Java Quicksort Arrays.sort function on primitives on pathological worst-case data sets, don't use this in CodeForces rounds
- Iterating through all subsets of size k of the input elements: O(n^k). For example, iterating through all triplets is O(n^3).
- Iterating through all subsets: O(2^n)
- Iterating through all permutations: O(n!)
Here are conservative upper bounds on the value of n for each time complexity. You can probably get away with more than this, but this should allow you to quickly check whether an algorithm is viable.
\begin{center}
\begin{tabular}{c c}
\toprule
n & Possible complexities \\
\midrule
n \leq 10 & O(n!), O(n^7), O(n^6) \\
n \leq 20 & O(2^n \cdot n), O(n^5) \\
n \leq 80 & O(n^4) \\
n \leq 400 & O(n^3) \\
n \leq 7500 & O(n^2) \\
n \leq 7 \cdot 10^4 & O(n \sqrt n) \\
n \leq 5 \cdot 10^5 & O(n \log n) \\
n \leq 5 \cdot 10^6 & O(n) \\
n \leq 10^{12} & O(\sqrt{n} \log n), O(\sqrt{n}) \\
n \leq 10^{18} & O(\log^2 n), O(\log n), O(1) \\
\bottomrule
\end{tabular}
\end{center}
- n | Possible complexities
- n <= 10 | O(n!), O(n^7), O(n^6)
- n <= 20 | O(2^n \cdot n), O(n^5)
- n <= 80 | O(n^4)
- n <= 400 | O(n^3)
- n <= 7500 | O(n^2)
- n <= 7 * 10^4 | O(n \sqrt n)
- n <= 5 * 10^5 | O(n \log n)
- n <= 5 * 10^6 | O(n)
- n <= 10^18 | O(\log^2 n), O(\log n), O(1)