• 沒有找到結果。

2Answer 1Question CorrectnessofgetMinIndex

N/A
N/A
Protected

Academic year: 2022

Share "2Answer 1Question CorrectnessofgetMinIndex"

Copied!
1
0
0

加載中.... (立即查看全文)

全文

(1)

Data Structures and Algorithms (NTU, Class 01, Spring 2020) instructor: Hsuan-Tien Lin

Correctness of getMinIndex

1 Question

The following getMinIndex algorithm has been introduced in the class. Prove that the algorithm is correct. That is, the index to the minimum element will be returned.

getMinIndex(integer array arr, integer len) minpos ← 0

for i ← 1 to len − 1 do if arr[i] < arr[minpos] then

minpos ← i end if end for

return minpos

2 Answer

Claim 0: Upon existing the loop at i = k for any k = 1, 2, . . . , len − 1, arr[minpos] ≤ arr[j] for j = 0, 1, 2, . . . , k.

Proof : Let mk denote the value of minpos when existing the loop at i = k.

1. The claim is true when i = 1, because either

• arr[1] < arr[0], which means that the if is true (i is 1 and minpos was assigned to 0 in the first line) and m1 is then assigned to 1, making arr[m1] ≤ arr[j] for j = 0, 1, or

• arr[1] ≥ arr[0], which keeps m1= 0 and thus arr[m1] ≤ arr[j] for j = 0, 1.

2. Assume that when i = t − 1, the claim is true. That is,

arr[mt−1] ≤ arr[j] for j = 0, 1, 2, . . . , t − 1. (1) Then, when i = t, there are two cases

• the if is true, which means arr[t] < arr[mt−1]. Combining the inequality with (1),

arr[t] < arr[j] for j = 0, 1, 2, . . . , t − 1. (2) In this case, mtgets updated to t. Combining the trivial arr[t] ≤ arr[t] with (2), we get

arr[mt] ≤ arr[j] for j = 0, 1, 2, . . . , t. (3)

• the if is false, which means arr[t] ≥ arr[mt−1]. Note that mtkeeps the value of mt−1 here.

We can then combine the inequality with (1), and get

arr[mt] ≤ arr[j] for j = 0, 1, 2, . . . , t. (4) So in both cases, the claim is true when i = t. By mathematical induction, the claim is true for any i = 1, 2, . . . , k.

Claim 1: Upon existing the algorithm, arr[minpos] ≤ arr[j] for j = 0, 1, 2, . . . , len − 1 for any positive len. That is, the algorithm is correct.

Proof : Claim 1 is trivially true for len = 1, where minpos stays at 0 and is surely ≤ arr[j] for j = 0.

For other len, apply Claim 0 with k = len − 1 and we see that Claim 1 is also true.

1 of 1

參考文獻

相關文件

(Class): Apples are somewhat circular, somewhat red, possibly green, and may have stems at the top. Hsuan-Tien

(Class): Apples are somewhat circular, somewhat red, possibly green, and may have stems at the top. Hsuan-Tien

Hsuan-Tien Lin (NTU CSIE) Machine Learning Techniques 5/22.. Decision Tree Decision Tree Hypothesis. Disclaimers about

• check type information very strictly by compiler (as opposed to single-object polymorphism): ensure type safety in JVM. Note: type information erased

—we cannot teach all, but with reading you can learn all 3-6: 3 hour teaching, 6 hour reading/writing after class as important as writing assignments:. some may show up

Hsuan-Tien Lin (NTU CSIE) Machine Learning Basics

Hsuan-Tien Lin (NTU CSIE) Machine Learning Techniques 3/24.:. Deep Learning Deep

instance method binding: dynamic, depending on run-time