This commit is contained in:
krahets
2024-05-06 14:40:36 +08:00
parent 7e7eb6047a
commit 5c7d2c7f17
54 changed files with 3456 additions and 215 deletions
@@ -31,7 +31,15 @@ The following function uses a `for` loop to perform a summation of $1 + 2 + \dot
=== "C++"
```cpp title="iteration.cpp"
[class]{}-[func]{forLoop}
/* for loop */
int forLoop(int n) {
int res = 0;
// Loop sum 1, 2, ..., n-1, n
for (int i = 1; i <= n; ++i) {
res += i;
}
return res;
}
```
=== "Java"
@@ -114,7 +122,7 @@ The following function uses a `for` loop to perform a summation of $1 + 2 + \dot
[class]{}-[func]{forLoop}
```
The flowchart below represents this sum function.
Figure 2-1 represents this sum function.
![Flowchart of the sum function](iteration_and_recursion.assets/iteration.png){ class="animation-figure" }
@@ -145,7 +153,17 @@ Below we use a `while` loop to implement the sum $1 + 2 + \dots + n$.
=== "C++"
```cpp title="iteration.cpp"
[class]{}-[func]{whileLoop}
/* while loop */
int whileLoop(int n) {
int res = 0;
int i = 1; // Initialize condition variable
// Loop sum 1, 2, ..., n-1, n
while (i <= n) {
res += i;
i++; // Update condition variable
}
return res;
}
```
=== "Java"
@@ -253,7 +271,19 @@ For example, in the following code, the condition variable $i$ is updated twice
=== "C++"
```cpp title="iteration.cpp"
[class]{}-[func]{whileLoopII}
/* while loop (two updates) */
int whileLoopII(int n) {
int res = 0;
int i = 1; // Initialize condition variable
// Loop sum 1, 4, 10, ...
while (i <= n) {
res += i;
// Update condition variable
i++;
i *= 2;
}
return res;
}
```
=== "Java"
@@ -363,7 +393,18 @@ We can nest one loop structure within another. Below is an example using `for` l
=== "C++"
```cpp title="iteration.cpp"
[class]{}-[func]{nestedForLoop}
/* Double for loop */
string nestedForLoop(int n) {
ostringstream res;
// Loop i = 1, 2, ..., n-1, n
for (int i = 1; i <= n; ++i) {
// Loop j = 1, 2, ..., n-1, n
for (int j = 1; j <= n; ++j) {
res << "(" << i << ", " << j << "), ";
}
}
return res.str();
}
```
=== "Java"
@@ -449,7 +490,7 @@ We can nest one loop structure within another. Below is an example using `for` l
[class]{}-[func]{nestedForLoop}
```
The flowchart below represents this nested loop.
Figure 2-2 represents this nested loop.
![Flowchart of the nested loop](iteration_and_recursion.assets/nested_iteration.png){ class="animation-figure" }
@@ -491,7 +532,16 @@ Observe the following code, where simply calling the function `recur(n)` can com
=== "C++"
```cpp title="recursion.cpp"
[class]{}-[func]{recur}
/* Recursion */
int recur(int n) {
// Termination condition
if (n == 1)
return 1;
// Recursive: recursive call
int res = recur(n - 1);
// Return: return result
return n + res;
}
```
=== "Java"
@@ -630,7 +680,14 @@ For example, in calculating $1 + 2 + \dots + n$, we can make the result variable
=== "C++"
```cpp title="recursion.cpp"
[class]{}-[func]{tailRecur}
/* Tail recursion */
int tailRecur(int n, int res) {
// Termination condition
if (n == 0)
return res;
// Tail recursive call
return tailRecur(n - 1, res + n);
}
```
=== "Java"
@@ -757,7 +814,16 @@ Using the recursive relation, and considering the first two numbers as terminati
=== "C++"
```cpp title="recursion.cpp"
[class]{}-[func]{fib}
/* Fibonacci sequence: Recursion */
int fib(int n) {
// Termination condition f(1) = 0, f(2) = 1
if (n == 1 || n == 2)
return n - 1;
// Recursive call f(n) = f(n-1) + f(n-2)
int res = fib(n - 1) + fib(n - 2);
// Return result f(n)
return res;
}
```
=== "Java"
@@ -841,7 +907,7 @@ Using the recursive relation, and considering the first two numbers as terminati
[class]{}-[func]{fib}
```
Observing the above code, we see that it recursively calls two functions within itself, **meaning that one call generates two branching calls**. As illustrated below, this continuous recursive calling eventually creates a <u>recursion tree</u> with a depth of $n$.
Observing the above code, we see that it recursively calls two functions within itself, **meaning that one call generates two branching calls**. As illustrated in Figure 2-6, this continuous recursive calling eventually creates a <u>recursion tree</u> with a depth of $n$.
![Fibonacci sequence recursion tree](iteration_and_recursion.assets/recursion_tree.png){ class="animation-figure" }
@@ -905,7 +971,25 @@ Therefore, **we can use an explicit stack to simulate the behavior of the call s
=== "C++"
```cpp title="recursion.cpp"
[class]{}-[func]{forLoopRecur}
/* Simulate recursion with iteration */
int forLoopRecur(int n) {
// Use an explicit stack to simulate the system call stack
stack<int> stack;
int res = 0;
// Recursive: recursive call
for (int i = n; i > 0; i--) {
// Simulate "recursive" by "pushing onto the stack"
stack.push(i);
}
// Return: return result
while (!stack.empty()) {
// Simulate "return" by "popping from the stack"
res += stack.top();
stack.pop();
}
// res = 1+2+3+...+n
return res;
}
```
=== "Java"
@@ -731,7 +731,7 @@ The time complexity of both `loop()` and `recur()` functions is $O(n)$, but thei
## 2.4.3 &nbsp; Common types
Let the size of the input data be $n$, the following chart displays common types of space complexities (arranged from low to high).
Let the size of the input data be $n$, Figure 2-16 displays common types of space complexities (arranged from low to high).
$$
\begin{aligned}
@@ -775,9 +775,28 @@ Note that memory occupied by initializing variables or calling functions in a lo
=== "C++"
```cpp title="space_complexity.cpp"
[class]{}-[func]{func}
/* Function */
int func() {
// Perform some operations
return 0;
}
[class]{}-[func]{constant}
/* Constant complexity */
void constant(int n) {
// Constants, variables, objects occupy O(1) space
const int a = 0;
int b = 0;
vector<int> nums(10000);
ListNode node(0);
// Variables in a loop occupy O(1) space
for (int i = 0; i < n; i++) {
int c = 0;
}
// Functions in a loop occupy O(1) space
for (int i = 0; i < n; i++) {
func();
}
}
```
=== "Java"
@@ -915,7 +934,21 @@ Linear order is common in arrays, linked lists, stacks, queues, etc., where the
=== "C++"
```cpp title="space_complexity.cpp"
[class]{}-[func]{linear}
/* Linear complexity */
void linear(int n) {
// Array of length n occupies O(n) space
vector<int> nums(n);
// A list of length n occupies O(n) space
vector<ListNode> nodes;
for (int i = 0; i < n; i++) {
nodes.push_back(ListNode(i));
}
// A hash table of length n occupies O(n) space
unordered_map<int, string> map;
for (int i = 0; i < n; i++) {
map[i] = to_string(i);
}
}
```
=== "Java"
@@ -1022,7 +1055,13 @@ As shown in Figure 2-17, this function's recursive depth is $n$, meaning there a
=== "C++"
```cpp title="space_complexity.cpp"
[class]{}-[func]{linearRecur}
/* Linear complexity (recursive implementation) */
void linearRecur(int n) {
cout << "Recursion n = " << n << endl;
if (n == 1)
return;
linearRecur(n - 1);
}
```
=== "Java"
@@ -1123,7 +1162,18 @@ Quadratic order is common in matrices and graphs, where the number of elements i
=== "C++"
```cpp title="space_complexity.cpp"
[class]{}-[func]{quadratic}
/* Quadratic complexity */
void quadratic(int n) {
// A two-dimensional list occupies O(n^2) space
vector<vector<int>> numMatrix;
for (int i = 0; i < n; i++) {
vector<int> tmp;
for (int j = 0; j < n; j++) {
tmp.push_back(0);
}
numMatrix.push_back(tmp);
}
}
```
=== "Java"
@@ -1228,7 +1278,14 @@ As shown in Figure 2-18, the recursive depth of this function is $n$, and in eac
=== "C++"
```cpp title="space_complexity.cpp"
[class]{}-[func]{quadraticRecur}
/* Quadratic complexity (recursive implementation) */
int quadraticRecur(int n) {
if (n <= 0)
return 0;
vector<int> nums(n);
cout << "Recursive n = " << n << ", length of nums = " << nums.size() << endl;
return quadraticRecur(n - 1);
}
```
=== "Java"
@@ -1335,7 +1392,15 @@ Exponential order is common in binary trees. Observe Figure 2-19, a "full binary
=== "C++"
```cpp title="space_complexity.cpp"
[class]{}-[func]{buildTree}
/* Exponential complexity (building a full binary tree) */
TreeNode *buildTree(int n) {
if (n == 0)
return nullptr;
TreeNode *root = new TreeNode(0);
root->left = buildTree(n - 1);
root->right = buildTree(n - 1);
return root;
}
```
=== "Java"
@@ -675,7 +675,7 @@ In essence, time complexity analysis is about finding the asymptotic upper bound
If there exist positive real numbers $c$ and $n_0$ such that for all $n > n_0$, $T(n) \leq c \cdot f(n)$, then $f(n)$ is considered an asymptotic upper bound of $T(n)$, denoted as $T(n) = O(f(n))$.
As illustrated below, calculating the asymptotic upper bound involves finding a function $f(n)$ such that, as $n$ approaches infinity, $T(n)$ and $f(n)$ have the same growth order, differing only by a constant factor $c$.
As shown in Figure 2-8, calculating the asymptotic upper bound involves finding a function $f(n)$ such that, as $n$ approaches infinity, $T(n)$ and $f(n)$ have the same growth order, differing only by a constant factor $c$.
![Asymptotic upper bound of a function](time_complexity.assets/asymptotic_upper_bound.png){ class="animation-figure" }
@@ -963,7 +963,7 @@ The following table illustrates examples of different operation counts and their
## 2.3.4 &nbsp; Common types of time complexity
Let's consider the input data size as $n$. The common types of time complexities are illustrated below, arranged from lowest to highest:
Let's consider the input data size as $n$. The common types of time complexities are shown in Figure 2-9, arranged from lowest to highest:
$$
\begin{aligned}
@@ -995,7 +995,14 @@ Constant order means the number of operations is independent of the input data s
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{constant}
/* Constant complexity */
int constant(int n) {
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
count++;
return count;
}
```
=== "Java"
@@ -1095,7 +1102,13 @@ Linear order indicates the number of operations grows linearly with the input da
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{linear}
/* Linear complexity */
int linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
count++;
return count;
}
```
=== "Java"
@@ -1193,7 +1206,15 @@ Operations like array traversal and linked list traversal have a time complexity
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{arrayTraversal}
/* Linear complexity (traversing an array) */
int arrayTraversal(vector<int> &nums) {
int count = 0;
// Loop count is proportional to the length of the array
for (int num : nums) {
count++;
}
return count;
}
```
=== "Java"
@@ -1298,7 +1319,17 @@ Quadratic order means the number of operations grows quadratically with the inpu
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{quadratic}
/* Quadratic complexity */
int quadratic(int n) {
int count = 0;
// Loop count is squared in relation to the data size n
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
}
}
return count;
}
```
=== "Java"
@@ -1413,7 +1444,24 @@ For instance, in bubble sort, the outer loop runs $n - 1$ times, and the inner l
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{bubbleSort}
/* Quadratic complexity (bubble sort) */
int bubbleSort(vector<int> &nums) {
int count = 0; // Counter
// Outer loop: unsorted range is [0, i]
for (int i = nums.size() - 1; i > 0; i--) {
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// Swap nums[j] and nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // Element swap includes 3 individual operations
}
}
}
return count;
}
```
=== "Java"
@@ -1530,7 +1578,19 @@ Figure 2-11 and code simulate the cell division process, with a time complexity
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{exponential}
/* Exponential complexity (loop implementation) */
int exponential(int n) {
int count = 0, base = 1;
// Cells split into two every round, forming the sequence 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
for (int j = 0; j < base; j++) {
count++;
}
base *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
```
=== "Java"
@@ -1636,7 +1696,12 @@ In practice, exponential order often appears in recursive functions. For example
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{expRecur}
/* Exponential complexity (recursive implementation) */
int expRecur(int n) {
if (n == 1)
return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
```
=== "Java"
@@ -1739,7 +1804,15 @@ Figure 2-12 and code simulate the "halving each round" process, with a time comp
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{logarithmic}
/* Logarithmic complexity (loop implementation) */
int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
count++;
}
return count;
}
```
=== "Java"
@@ -1841,7 +1914,12 @@ Like exponential order, logarithmic order also frequently appears in recursive f
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{logRecur}
/* Logarithmic complexity (recursive implementation) */
int logRecur(int n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}
```
=== "Java"
@@ -1953,7 +2031,16 @@ Linear-logarithmic order often appears in nested loops, with the complexities of
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{linearLogRecur}
/* Linear logarithmic complexity */
int linearLogRecur(int n) {
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
count++;
}
return count;
}
```
=== "Java"
@@ -2072,7 +2159,17 @@ Factorials are typically implemented using recursion. As shown in the code and F
=== "C++"
```cpp title="time_complexity.cpp"
[class]{}-[func]{factorialRecur}
/* Factorial complexity (recursive implementation) */
int factorialRecur(int n) {
if (n == 0)
return 1;
int count = 0;
// From 1 split into n
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
}
```
=== "Java"
@@ -2196,9 +2293,30 @@ The "worst-case time complexity" corresponds to the asymptotic upper bound, deno
=== "C++"
```cpp title="worst_best_time_complexity.cpp"
[class]{}-[func]{randomNumbers}
/* Generate an array with elements {1, 2, ..., n} in a randomly shuffled order */
vector<int> randomNumbers(int n) {
vector<int> nums(n);
// Generate array nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
// Generate a random seed using system time
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
// Randomly shuffle array elements
shuffle(nums.begin(), nums.end(), default_random_engine(seed));
return nums;
}
[class]{}-[func]{findOne}
/* Find the index of number 1 in array nums */
int findOne(vector<int> &nums) {
for (int i = 0; i < nums.size(); i++) {
// When element 1 is at the start of the array, achieve best time complexity O(1)
// When element 1 is at the end of the array, achieve worst time complexity O(n)
if (nums[i] == 1)
return i;
}
return -1;
}
```
=== "Java"