[PDF] Recursive Algorithm Correctness (Continued)



Previous PDF Next PDF







Algorithmics Correction Final Exam  (P2)

Algorithmics Correction Final Exam #2 (P2) – Undergraduate 1st year S2 Epita Solution 3 (List→ AVL– 5 points) Specifications: The function list2avl(L) returns an A -V L (class AVL) built from the list L sorted in stricly



Algorithmics Correction Midterm Exam  (Version profs)

Algorithmics Correction Midterm Exam #1 23 avril 2019 - 13:30 Undergraduate 1 st year S1# Epita (a) There is no precondition It is an internal operation de ned on the bounds of the vector



Recursive Algorithm Correctness (Continued)

CSC236H1F Lecture Summary for Week 7 Fall 2015 Recursive Algorithm Correctness (Continued) Example 1 (Binary search algorithm) Consider the following recursive implementation of binary search algo-



An Atmospheric Correction Algorithm for Remote Sensing of

improved atmospheric correction algorithms must be developed for the remote sensing of Case 2 waters Previously, we developed an atmospheric correction algo-rithm for hyperspectral remote sensing of ocean color [9] In this paper, we describe modifications to this algorithm for multichannel remote sensing of coastal waters using MODIS



CAAS: an atmospheric correction algorithm for the remote

infrared (NIR) correction scheme (originally with assump-tions of zero water-leaving radiance for the NIR bands) and several ancillary parameters to remove atmospheric effects in remote sensing of ocean colour The failure of this algo-rithm over complex waters has been reported by many recent investigations, and can be attributed to the



An algebraic algorithm for nonuniformity correction in focal

effective bias correction with relatively few frames This will be demonstrated with both simulated and real infra-red data Because of its highly localized nature in time and space, the algorithm is easily implemented and com-putationally efficient, permitting quick computation of the correction matrix that is needed to compensate con-



Digital algorithm for dispersion correction in optical

Digital algorithm for dispersion correction in optical coherence tomography for homogeneous and stratified media Daniel L Marks, Amy L Oldenburg, J Joshua Reynolds, and Stephen A Boppart The resolution of optical coherence tomography OCT often suffers from blurring caused by material dispersion



See Something, Say Something: Correction of Global Health

Our previous work considering algo-rithmic correction speculated that part of its effectiveness in reducing misinformation resulted from trust in the unbiased nature of algorithms People tend to



Brahim BESSAA

Les Structures de Contrôle (Conditionnelles – Itératives) Exercices Corrigés d’Algorithmique – 1ére Année MI 5 EXERCICE 1 Ecrire un algorithme qui demande un nombre à l’utilisateur, puis calcule et affiche le carré de ce nombre

[PDF] Exemples de fonctions en Python - Lirmm

[PDF] Récursivité (1/3)

[PDF] Corrigé Série d 'exercices n°4 : Les fonctions et procédures

[PDF] Bases d 'algorithmique

[PDF] COURS ALGORITHMIQUE ET PROGRAMMATION INFORMATIQUE

[PDF] FICHE n°6 : PROGRAMMER DES BOUCLES - Maths-et-tiques

[PDF] Correction TD1 algorithme

[PDF] Correction TD1 algorithme

[PDF] Algorithmique au lycée

[PDF] fiche maternelle algorithme imprimer- pdf documents

[PDF] Fiche enseignant ALGORITHMES NIVEAU : GRANDE SECTION

[PDF] Algorithme et numération - Académie de Nancy-Metz

[PDF] L 'atelier des petites chenilles en PS Etape 1 - académie de Caen

[PDF] reproduire une suite algorithmique - Accueil DSDEN 22

[PDF] Rappels : Tableaux et Matrices

CSC236H1F Lecture Summary for Week 7 Fall 2015

Recursive Algorithm Correctness (Continued)

Example 1(Binary search algorithm).Consider the following recursive implementation of binary search algo-

rithm:

1:functionRecBSearch(x, A, s, f)

2:ifs==fthen

3:ifx==A[s]then

4:returns

5:else

6:return1

7:end if

8:else

9:m= (s+f)=2.Integer Division

10:ifxA[m]then

11:returnRecBSearch(x, A, s, m)

12:else

13:returnRecBSearch(x, A, m + 1, f)

14:end if

15:end if

16:end function

Precondition:

1.

Elemen tsof Acomparable with each other and withx

2. Assume arra yindices start at 0 and h ence0 sf < length(A) 3. Arra yAissorted in nondecreasing order (A[s] A[f]) Postcondition:RecBSearch(x,A,s,f) terminates and returns indexpsuch that:

1.spforp=1

2.

If s < p, thenA[p1]< x

3.

If spf, thenx=A[p]

Now, Let's prove the correctness of this algorithm. Proof.By induction on sizen=f+ 1s, we prove (precondition and execution) implies (termination and postcondition). Inductive structure of proof will follow recursive structure of algorithm.

Base case:n= 1, i.e.,s=f. Then, algorithm terminates (lines 2-7 contain no loop or call), and returnssif

x=A[s],1 ifx6=A[s], which satises postcondition.

Induction Step:Letn >1 and suppose postcondition holds after execution for all inputs of sizekthat satisfy

precondition, for 1k < n(IH). Consider call RecBSearch(x,A,s,f) whenf+1s=n2. Test on line 2 fails,

sos < f(sincesfby precondition ands6=fby negation of test) and algorithm executes line 9. Next, test on

line 10 executes. Case 1 (xA[m]):Becausem < fthenm+ 1s < f+ 1sand hence by IH, RecBSearch(x,A,s,m) returns indexpsuch that:

1.spmorp=1

2. if s < p, thenA[p1]< x Dept. of Computer Science, University of Toronto, St. George Campus Page 1 of 4

CSC236H1F Lecture Summary for Week 7 Fall 2015

3. if spm, thenx=A[p]

Hence,

1.spf(sincem < f) orp=1

2. if s < p, thenA[p1]< xsincepmand because of IH 3. b ecuasew erecursed on the rst half then if spfthenpm, and by IH forspmthenx=A[p]

Therefore, current call satises postcondition.

Case 2 (A[m]< x):Becausesmthens < m+ 1 sof+ 1(m+ 1)< f+ 1sand hence by IH,

RecBSearch(x,A,m+ 1,f) returns indexpsuch that:

1.m+ 1pforp=1

2. if m+ 1< p, thenA[p1]< x 3. if m+ 1pfthenx=A[p]

Hence,

1.spf(sinces < m+ 1) orp=1

2. if s < pthen we know thatm+1psince we recursed on the second half. By IHA[p1]< xform+1< p and by the test of line 10,A[m]< xin this case. Therefore, ifs < pthenA[p1]< x 3. if spfthenm+ 1pfsince we recursed on the second half and by IHx=A[p]

Therefore, current call satises postcondition. In all cases, current call satises postcondition. Therefore, by

induction, RecBSearch is correct.Example 2.In this example we prove the correctness of MergeSort algorithm.

1:functionMergeSort(A,s,f)

2:ifs==fthen

3:return

4:else

5:m= (s+f)=2.Integer Division

6:MergeSort(A,s,m)

7:MergeSort(A,m+1,f)

8:# merge sorted A[s::m] and A[m+ 1::f] back into A[s::f]

9:fori=s;;fdo

10:B[i] = A[i]

11:end for

12:c=s

13:d=m+ 1

14:fori=s;;fdo

15:ifd > for (cmand B[c]

16:A[i] = B[c]

17:c=c+ 1

18:else. dfand (c > mor B[c]B[d])

19:A[i] = B[d]

20:d=d+ 1

21:end if

22:end for

Dept. of Computer Science, University of Toronto, St. George Campus Page 2 of 4

CSC236H1F Lecture Summary for Week 7 Fall 2015

23:end if

24:end function

Precondition:

1.s;f2N, 0sf < length(A)

2. elemen tsof A[s::f] comparable with each other

Postcondition:A[s::f] contains same elements as before, but sorted in non-decreasing order (A[s] A[f])

Proof.By induction on sizen=f+ 1s, we prove precondition and execution implies termination and post-

condition, for all inputs of sizen. Once again, the inductive structure of proof will follow recursive structure of

algorithm. Base case:Suppose (A,s,f) is input of sizen=fs+1 = 1 that satises precondition. Then,f=sso algorithm terminates and returnsAunchanged (on line 2), which satises postcondition.

Induction Step:Supposen >1 and, for 1k < n, for all inputs of sizekthat satisfy precondition, algorithm

terminates and postcondition holds after execution (IH). Suppose (A,s,f) is input of sizen=fs+ 1>1 that

satises precondition, and consider call MergeSort(A,s,f). Test on line 2 fails becausefs+ 1>1 if > s and hence the algorithm executes line 5. Sinces bs+f2 c< f, IH implies that MergeSort(A,s,m) terminates and

the outputA[s::m] contains same elements as inputA[s::m] but sorted in non-decreasing order. For the same

reason, MergeSort(A,m+ 1,f) terminates and outputA[m+ 1::f] contains same elements as inputA[m+ 1::f]

but sorted in non-decreasing order. Lines 9-11 copiesA[s::f] intoB[s::f] (exercise: prove this). Lines 12-22 merge

B[s::m] andB[m+1::f] intoA[s::f], which satises postcondition. This last statement requires formal proof, but

we haven't learned how to prove the correctness of iterative algorithms yet! Hence, we skip this part of the proof

for now and we will come back to it later.From these two example, you can see that sometimes it becomese tedious to use a proof by induction as it

requires you to write down a lot of details. There is another way of proving the correctness which requires less

elaboration and minimizes the writing eort. In this technique we have the following steps: 1. W ritedo wnthe correct sp ecication(pre/p ost-conditions) 2. Sp ecifywhat is the size of an instance for the purp oseof induction 3.

List all p rogrampaths to a return p oint.F oreac hpath, argue wh ythat path terminates and the p ostcondition

is true at the return point I. if the path includes no recursiv ecalls, this is usually straigh tforwardreasoning. Don't forget the termination part. II. if the path inclu desrecursiv ecalls then: Show that the precondition for each recursive call holds just before the call is made Argue (assuming the postconditions of the recursive calls hold) why the postcondition of the func- tion holds Show that each recursive call is made on a smaller-sized instance. Based on this, argue why the function terminates (assuming that the recursive calls terminate) This technique is called proving that the specication is inductive.

Example 3.Let's prove the correctness of RecBSearch by proving that the specication is inductive.Just to recap:

Precondition:

1.

Elemen tsof Acomparable with each other and withx

Dept. of Computer Science, University of Toronto, St. George Campus Page 3 of 4

CSC236H1F Lecture Summary for Week 7 Fall 2015

2. Assume arra yindices start at 0 and h ence0 sf < length(A) 3. Arra yAissorted in nondecreasing order (A[s] A[f]) Postcondition:RecBSearch(x,A,s,f) terminates and returns indexpsuch that:

1.spforp=1

2.

If spf, thenx=A[p]

Note that this postcodition is slightly weaker than the one provided in the previous example. But it will still

serve the purpose of this example.Path 1 (2,3,4):Sincef=s, andp=s, thenspf(rst item in the

postcondition is true). Since line 3 is true,x=A[p] (second item in the postcondition is true). Therefore, the

postcondition is satised. It is obvious that the algorithm terminates in this case.

Path 2 (2,5,6):In this casep=1 and hence the rst postcondition is true because one element of disjunction

is true. Since the if condition doesn't hold the the second postcondition is also true. Path 3 (8,9-11):In this cases6=f. Hence,sm < fand by preconditionf < length(A). SinceA[s::f] is

sorted thenA[m::f] should also be sorted and by preconditions the elements inA[m::f] are comparable toA.

From our arguments so far, we can see that the preconditions of input to the recursive call in line 11 holds. Since

ms+1< fs+1, then we are recursing on an instance of smaller size. Hence if the recursive call terminates,

by postconditions, it should either return1 or an indexpfor which we havespmandx=A[p]. If the

former holds then the postcondition of our bigger instance also holds. If the latter holds, then the postcondition

of our bigger instance also holds becausespm < fandx=A[p].

Path 4 (8,9,12-13):This case is similar to the previous case. We only need to consider the fact that sinces6=f

ands0 then 0sm < m+ 1f < length(A). We can then argue similar to Path 3 that preconditions

of the recursive call holds and then argue that postconditions should hold which means that either an index of

1 is returned or an indexm+ 1pfis returned andx=A[p]. It is obvious that if former holds then the

postcondition of our bigger instance holds and if latter holds, thensm < m+ 1pfandx=A[p] which again means that the postcondition of our bigger instance holds.

Exercise.Try to prove the correctness of MergeSort by proving that the specication is inductive (at one point,

you need to prove that the for loops are correct, for now assume that they are correct and they terminate).

Dept. of Computer Science, University of Toronto, St. George Campus Page 4 of 4quotesdbs_dbs5.pdfusesText_9