9.6.4. 계산 비용의 상한 증명
길이가 n+1인 리스트를 정렬할 때, 꼬리를 먼저 정렬하는 데 든 비용에는 길이 n의 상한 B(n)을 적용한다. 남은 삽입은 길이가 n인 정렬 결과에 새 원소를 넣으므로 비교 비용이 n 이하이다. 이를 더하면 B(n+1)=B(n)+n이다. 그림 9.19에서 길이가 6이면 각 재귀 단계에 더하는 상한은 0부터 5까지다.
insertCost_le_length를 적용하면 삽입 비용은 (insertionSort xs).length 이하다. 여기서 연습문제 8.6.6에서 보았던 길이 보존이 필요하다. 아래 insertionSort_length는 8장의 순열 정리에서 .length_eq를 바로 얻는 방식이다. 삽입의 상한을 xs.length로 다시 쓴 뒤, Nat.add_le_add로 꼬리 정렬의 귀납 가정과 합친다.
def insertionSortBound : Nat → Nat
| 0 => 0
| n + 1 => insertionSortBound n + n
theorem insertionSort_length (xs : List Nat) :
(insertionSort xs).length = xs.length :=
(insertionSort_perm xs).length_eq
theorem insertionSortCost_le_bound (xs : List Nat) :
insertionSortCost xs ≤
insertionSortBound xs.length := xs:List Nat⊢ insertionSortCost xs ≤ insertionSortBound xs.length
induction xs with
⊢ insertionSortCost [] ≤ insertionSortBound [].length All goals completed! 🐙
x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.length⊢ insertionSortCost (x :: xs) ≤ insertionSortBound (x :: xs).length
x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.lengthh:insertCost x (insertionSort xs) ≤ (insertionSort xs).length⊢ insertionSortCost (x :: xs) ≤ insertionSortBound (x :: xs).length
cons x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.lengthh:insertCost x (insertionSort xs) ≤ xs.length⊢ insertionSortCost (x :: xs) ≤ insertionSortBound (x :: xs).length
rw [insertionSortCost, cons x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.lengthh:insertCost x (insertionSort xs) ≤ xs.length⊢ insertionSortCost xs + insertCost x (insertionSort xs) ≤ insertionSortBound (x :: xs).length List.length_cons, cons x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.lengthh:insertCost x (insertionSort xs) ≤ xs.length⊢ insertionSortCost xs + insertCost x (insertionSort xs) ≤ insertionSortBound (xs.length + 1)
insertionSortBound cons x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.lengthh:insertCost x (insertionSort xs) ≤ xs.length⊢ insertionSortCost xs + insertCost x (insertionSort xs) ≤ insertionSortBound xs.length + xs.length] cons x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.lengthh:insertCost x (insertionSort xs) ≤ xs.length⊢ insertionSortCost xs + insertCost x (insertionSort xs) ≤ insertionSortBound xs.length + xs.length
exact Nat.add_le_add ih h All goals completed! 🐙
이 절의 상한은 n + 1에 도달할 때 n을 더한다. 9.3.1절의 reverseCostBySize는 같은 지점에서 n + 1을 더했다. 따라서 B(n+1)은 뒤집기의 T(n)과 같다. 이를 insertionSortBound_succ로 증명한 뒤 9.3절의 삼각수 공식에 연결한다. 길이가 0일 때는 양변이 0이고, n + 1일 때는 (n + 1) - 1이 n으로 줄어든다.
theorem insertionSortBound_succ (n : Nat) :
insertionSortBound (n + 1) = reverseCostBySize n := by n:Nat⊢ insertionSortBound (n + 1) = reverseCostBySize n
induction n with
| zero => zero ⊢ insertionSortBound (0 + 1) = reverseCostBySize 0 rfl All goals completed! 🐙
| succ n ih => succ n:Natih:insertionSortBound (n + 1) = reverseCostBySize n⊢ insertionSortBound (n + 1 + 1) = reverseCostBySize (n + 1)
change insertionSortBound (n + 1) + (n + 1) =
reverseCostBySize n + (n + 1) succ n:Natih:insertionSortBound (n + 1) = reverseCostBySize n⊢ insertionSortBound (n + 1) + (n + 1) = reverseCostBySize n + (n + 1)
rw [ih succ n:Natih:insertionSortBound (n + 1) = reverseCostBySize n⊢ reverseCostBySize n + (n + 1) = reverseCostBySize n + (n + 1)] All goals completed! 🐙
theorem insertionSortBound_formula (n : Nat) :
2 * insertionSortBound n = n * (n - 1) := by n:Nat⊢ 2 * insertionSortBound n = n * (n - 1)
cases n with
| zero => zero ⊢ 2 * insertionSortBound 0 = 0 * (0 - 1) rfl All goals completed! 🐙
| succ n => succ n:Nat⊢ 2 * insertionSortBound (n + 1) = (n + 1) * (n + 1 - 1)
rw [insertionSortBound_succ succ n:Nat⊢ 2 * reverseCostBySize n = (n + 1) * (n + 1 - 1)] succ n:Nat⊢ 2 * reverseCostBySize n = (n + 1) * (n + 1 - 1)
simpa [Nat.mul_comm] using reverseCostBySize_formula n All goals completed! 🐙
theorem insertionSortBound_eq_div (n : Nat) :
insertionSortBound n = n * (n - 1) / 2 := by n:Nat⊢ insertionSortBound n = n * (n - 1) / 2
rw [← insertionSortBound_formula n n:Nat⊢ insertionSortBound n = 2 * insertionSortBound n / 2] n:Nat⊢ insertionSortBound n = 2 * insertionSortBound n / 2
omega All goals completed! 🐙
theorem insertionSortCost_le_formula (xs : List Nat) :
insertionSortCost xs ≤
xs.length * (xs.length - 1) / 2 := by xs:List Nat⊢ insertionSortCost xs ≤ xs.length * (xs.length - 1) / 2
rw [← insertionSortBound_eq_div xs:List Nat⊢ insertionSortCost xs ≤ insertionSortBound xs.length] xs:List Nat⊢ insertionSortCost xs ≤ insertionSortBound xs.length
exact insertionSortCost_le_bound xs All goals completed! 🐙
따라서 길이가 n인 모든 입력에서 원소 비교 횟수는 n(n-1)/2 이하이다. 이제 각 길이에서 그 횟수를 모두 쓰게 만드는 입력을 찾는다.