Update 0_Silver_Complexity.md
This commit is contained in:
parent
8e8a0662d2
commit
0cac54ec5c
1 changed files with 26 additions and 33 deletions
|
@ -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,7 +91,7 @@ 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)
|
||||
|
@ -110,24 +110,17 @@ Complexity factors that come from some common algorithms and data structures are
|
|||
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Reference in a new issue