This commit is contained in:
krahets
2025-12-31 19:37:45 +08:00
parent 29ec0c699d
commit 3c9d5689c4
279 changed files with 40895 additions and 16087 deletions
@@ -322,12 +322,6 @@ comments: true
```
=== "Zig"
```zig title=""
```
## 2.4.2   計算方法
空間計算量を計算する方法は時間計算量とほぼ同様で、統計対象を「操作数」から「使用空間のサイズ」に変更するだけです。
@@ -474,12 +468,6 @@ comments: true
```
=== "Zig"
```zig title=""
```
**再帰関数では、スタックフレーム空間を考慮に入れる必要があります**。以下のコードを考えてみましょう:
=== "Python"
@@ -718,12 +706,6 @@ comments: true
```
=== "Zig"
```zig title=""
```
`loop()`関数と`recur()`関数の時間計算量は両方とも$O(n)$ですが、それらの空間計算量は異なります。
- `loop()`関数はループ内で`function()`を$n$回呼び出し、各反復の`function()`は返ってそのスタックフレーム空間を解放するため、空間計算量は$O(1)$のままです。
@@ -906,14 +888,6 @@ $$
[class]{}-[func]{constant}
```
=== "Zig"
```zig title="space_complexity.zig"
[class]{}-[func]{function}
[class]{}-[func]{constant}
```
### 2.   線形オーダー $O(n)$ {data-toc-label="2.   線形オーダー"}
線形オーダーは配列、連結リスト、スタック、キューなどで一般的で、要素数は$n$に比例します:
@@ -1033,12 +1007,6 @@ $$
[class]{}-[func]{linear}
```
=== "Zig"
```zig title="space_complexity.zig"
[class]{}-[func]{linear}
```
下図に示されているように、この関数の再帰深度は$n$で、$n$個の未返却の`linear_recur()`関数インスタンスがあり、$O(n)$サイズのスタックフレーム空間を使用します:
=== "Python"
@@ -1136,12 +1104,6 @@ $$
[class]{}-[func]{linear_recur}
```
=== "Zig"
```zig title="space_complexity.zig"
[class]{}-[func]{linearRecur}
```
![Recursive function generating linear order space complexity](space_complexity.assets/space_complexity_recursive_linear.png){ class="animation-figure" }
<p align="center"> 図 2-17 &nbsp; Recursive function generating linear order space complexity </p>
@@ -1255,12 +1217,6 @@ $$
[class]{}-[func]{quadratic}
```
=== "Zig"
```zig title="space_complexity.zig"
[class]{}-[func]{quadratic}
```
下図に示されているように、この関数の再帰深度は$n$で、各再帰呼び出しで長さ$n$、$n-1$、$\dots$、$2$、$1$の配列が初期化され、平均$n/2$となり、全体として$O(n^2)$の空間を占有します:
=== "Python"
@@ -1362,12 +1318,6 @@ $$
[class]{}-[func]{quadratic_recur}
```
=== "Zig"
```zig title="space_complexity.zig"
[class]{}-[func]{quadraticRecur}
```
![Recursive function generating quadratic order space complexity](space_complexity.assets/space_complexity_recursive_quadratic.png){ class="animation-figure" }
<p align="center"> 図 2-18 &nbsp; Recursive function generating quadratic order space complexity </p>
@@ -1477,12 +1427,6 @@ $$
[class]{}-[func]{build_tree}
```
=== "Zig"
```zig title="space_complexity.zig"
[class]{}-[func]{buildTree}
```
![Full binary tree generating exponential order space complexity](space_complexity.assets/space_complexity_exponential.png){ class="animation-figure" }
<p align="center"> 図 2-19 &nbsp; Full binary tree generating exponential order space complexity </p>