9.3.4. 최선과 최악의 경우
탐색 대상을 고정하고 길이가 n인 모든 리스트를 생각하자. 최선의 경우(best case)는 비용이 가장 작은 입력을 가리키고, 최악의 경우(worst case)는 비용이 가장 큰 입력을 가리킨다. 선형 탐색에서 리스트가 비어 있으면 두 경우 모두 비교 횟수가 0이다. 원소가 있으면 적어도 첫 원소를 비교한다. 대상이 머리에 있으면 그 1회로 끝나고, 끝까지 일치하는 원소가 없으면 전체 원소를 비교한다. 대상이 마지막에 처음 나타나는 경우에도 n번 비교한다.
그림 9.10에서 길이를 바꿔 두 경계를 비교한다. 탐색 대상은 1이다. 길이가 양수일 때 위쪽 리스트는 머리에만 1이 있고, 아래쪽 리스트는 모두 0이다. 길이가 0이면 두 입력이 모두 []이다.
최솟값을 구할 때는 모든 비어 있지 않은 리스트에서 1 이상이라는 하한과, 비용이 1인 입력이 필요하다. 첫 정리는 머리에서 바로 끝나거나 비용을 한 번 더하는 두 경우에 각각 하한을 증명한다. 둘째 정리는 길이가 n + 1일 때 target :: List.replicate n 0처럼 머리가 target인 리스트를 택하면 하한에 도달함을 보여 준다.
theorem linearSearchCost_ge_one
(target x : Nat) (xs : List Nat) :
1 ≤ linearSearchCost target (x :: xs) := target:Natx:Natxs:List Nat⊢ 1 ≤ linearSearchCost target (x :: xs)
target:Natx:Natxs:List Nath:x = target⊢ 1 ≤ linearSearchCost target (x :: xs)target:Natx:Natxs:List Nath:¬x = target⊢ 1 ≤ linearSearchCost target (x :: xs) target:Natx:Natxs:List Nath:x = target⊢ 1 ≤ linearSearchCost target (x :: xs)target:Natx:Natxs:List Nath:¬x = target⊢ 1 ≤ linearSearchCost target (x :: xs) All goals completed! 🐙
theorem linearSearchCost_best
(target : Nat) (xs : List Nat) :
linearSearchCost target (target :: xs) = 1 := target:Natxs:List Nat⊢ linearSearchCost target (target :: xs) = 1
All goals completed! 🐙
최댓값의 상한은 앞 소절에서 얻었다. 그림 9.11처럼 끝까지 비교한다는 것을 보장하는 길이 n의 입력을 하나 구성하면, 이 상한이 실제 최댓값이 된다. List.replicate n x는 x를 n개 담은 리스트다. x ≠ target이면 머리에서 찾을 수 없다. n에 대한 귀납법으로 길이 n인 반복 리스트에서 n번 비교함을 보일 수 있다.
theorem linearSearchCost_replicate_of_ne
(target x : Nat) (h : x ≠ target) (n : Nat) :
linearSearchCost target (List.replicate n x) = n := target:Natx:Nath:x ≠ targetn:Nat⊢ linearSearchCost target (List.replicate n x) = n
induction n with
target:Natx:Nath:x ≠ target⊢ linearSearchCost target (List.replicate 0 x) = 0 All goals completed! 🐙
target:Natx:Nath:x ≠ targetn:Natih:linearSearchCost target (List.replicate n x) = n⊢ linearSearchCost target (List.replicate (n + 1) x) = n + 1
All goals completed! 🐙
theorem linearSearchCost_worst (n : Nat) :
linearSearchCost 1 (List.replicate n 0) = n :=
linearSearchCost_replicate_of_ne 1 0 (n:Nat⊢ 0 ≠ 1 All goals completed! 🐙) n
#eval linearSearchCost 1 (List.replicate 5 0)
linearSearchCost_worst에는 탐색 대상 1을 고정했다. 임의의 target에 대해서는 그보다 1 큰 수만 반복한 리스트를 증인으로 택할 수 있다. List.length_replicate로 증인의 길이를 확인하고, Nat.succ_ne_self로 반복된 값이 target과 다름을 보인다.
theorem linearSearchCost_attains_worst (target n : Nat) :
∃ xs : List Nat,
xs.length = n ∧ linearSearchCost target xs = n := target:Natn:Nat⊢ ∃ xs, xs.length = n ∧ linearSearchCost target xs = n
target:Natn:Nat⊢ (List.replicate n (target + 1)).length = ntarget:Natn:Nat⊢ linearSearchCost target (List.replicate n (target + 1)) = n
target:Natn:Nat⊢ (List.replicate n (target + 1)).length = n All goals completed! 🐙
target:Natn:Nat⊢ linearSearchCost target (List.replicate n (target + 1)) = n All goals completed! 🐙
따라서 선형 탐색의 최선 비용은 n=0일 때 0, n>0일 때 1이다. 최악 비용은 모든 길이에서 n이다. 평균적인 경우(average case)의 비용을 정의하려면 각 입력이 얼마나 자주 나타나는지를 설명하는 확률분포가 필요하다.
표 9.9에서 각 명제에 사용한 근거를 비교한다. 9.4절에서는 이렇게 얻은 등식과 부등식으로 비용의 점근적 상한을 증명한다.
명제 | 증명에서 사용한 근거 |
|---|---|
길이 계산의 방문 횟수는 원소 수와 같다. | 리스트의 구조적 귀납법 |
뒤집기 비용은 길이의 삼각수다. | 리스트와 자연수에 대한 귀납법 및 분배법칙 |
탐색 비용은 리스트 길이 이하다. | 구조적 귀납법과 머리 비교의 경우 분석 |
탐색의 최선과 최악의 비용은 경계값에 도달한다. | 모든 입력의 부등식과 경계값에 도달하는 구체적인 입력 |
- 연습문제 9.3.3 탐색 대상이 없는 모든 리스트의 비용
h : target ∉ xs를 가정하고linearSearchCost target xs = xs.length를 증명한다.xs에 귀납법을 적용한다. 머리가 대상과 같거나 꼬리에 대상이 있다면 각각 전체 리스트에도 대상이 있다는 사실을List.mem_cons로 확인한다.