Presentation is loading. Please wait.

Presentation is loading. Please wait.

12-CRS-0106 REVISED 8 FEB 2013 CSG3F3/ Desain dan Analisis Algoritma Lecture 1 : Introduction to Software Engineering Concepts Intelligence, Computing,

Similar presentations


Presentation on theme: "12-CRS-0106 REVISED 8 FEB 2013 CSG3F3/ Desain dan Analisis Algoritma Lecture 1 : Introduction to Software Engineering Concepts Intelligence, Computing,"— Presentation transcript:

1 12-CRS-0106 REVISED 8 FEB 2013 CSG3F3/ Desain dan Analisis Algoritma Lecture 1 : Introduction to Software Engineering Concepts Intelligence, Computing, Multimedia (ICM)

2 12-CRS-0106 REVISED 8 FEB 2013 Outline Syllabus Top-down & bottom-up programming Correctness proofs by Induction Transforming & optimizing programs

3 12-CRS-0106 REVISED 8 FEB 2013 About DAA This Semester Tiga (3) sks, yaitu 3 jam teori + 1 jam responsi Sistem Penilaian Item-item bobot Quiz10% Tugas besar20% UTS30% UAS40%

4 12-CRS-0106 REVISED 8 FEB 2013 Tujuan Instruksional Umum Setelah mengikuti kuliah ini mahasiswa akan dapat: Menggunakan tools dan teknik-teknik yang lazim digunakan untuk analisis dan desain algoritma, Mendesain, menganalisis, dan menentukan kebenaran suatu algoritma terhadap kasus-kasus tertentu, Membandingkan beberapa algoritma dan menentukan algoritma yang terbaik untuk memecahkan kasus-kasus tertentu.

5 12-CRS-0106 REVISED 8 FEB 2013 Daftar Pustaka Introduction to Algorithms, 2 nd edition –T. H. Cormen, C. E. Leiserson, R. L. Rivest, and Clifford Stein. Published by: MIT Press or McGraw-Hill Introduction to the design and analysis of algorithm –Anany Levitin. Published by: Addison Wesley 2003 Foundations of algorithms –Richard Neapolitan. Kumarss Naimipour. Published by D.C Heath and Company 1996 Referensi Diktat Strategi Algoritmik IF2251 –Ir. Rinaldi Munir, M.T. Departemen Teknik Informatika, Institut Teknologi Bandung

6 12-CRS-0106 REVISED 8 FEB 2013 Top-down Programming Consider the following example of a scratch-off lottery ticket

7 12-CRS-0106 REVISED 8 FEB 2013 Top-down (cont’d) Some possible solutions: 1. Make a table and search it to find the solution

8 12-CRS-0106 REVISED 8 FEB 2013 Top-down (cont’d) 2. Sort the input into descending order and find the first run of three repeated amounts

9 12-CRS-0106 REVISED 8 FEB 2013 Top-down (cont’d) Top level

10 12-CRS-0106 REVISED 8 FEB 2013 Top-down (cont’d) Getting six inputs from the user

11 12-CRS-0106 REVISED 8 FEB 2013 Top-down (cont’d) 1. First alternative is choosing the data structure for the table before designing the strategy for the algorithm 2. Second alternative is postponing the choice of data representation

12 12-CRS-0106 REVISED 8 FEB 2013 Top-down (cont’d) It’s the second idea

13 12-CRS-0106 REVISED 8 FEB 2013 Top-down (cont’d)

14 12-CRS-0106 REVISED 8 FEB 2013 Top-down (cont’d)

15 12-CRS-0106 REVISED 8 FEB 2013 Top-down (cont’d) The activity of creating program at the top level leads to the creation of function calls, whose lower-level function definitions have not yet been written.

16 12-CRS-0106 REVISED 8 FEB 2013 Bottom-up programming Write the definitions of functions first before using calls on them inside other higher-level functions

17 12-CRS-0106 REVISED 8 FEB 2013 Data structure for the table typedef struct { int Amount; int Repetitions; } RowStruct; RowStruct Table[6];

18 12-CRS-0106 REVISED 8 FEB 2013 Final program

19 12-CRS-0106 REVISED 8 FEB 2013

20

21

22 Review questions 1. What is the difference between top-down and bottom-up programming ? 2. What does it mean to postpone the choice of data representations and algorithms ?

23 12-CRS-0106 REVISED 8 FEB 2013 Proving Programs Correct How to prove that a program does what it is intended to do Providing assertions: –Precondition  Initial state –Postcondition  Final state {precondition} P {postcondition}

24 12-CRS-0106 REVISED 8 FEB 2013 Example: SelectionSort(A,m,n) By applying the pattern in the case of SelectionSort: {m  n} SelectionSort(A,m,n) {A[m]A[m+1] … A[n]}

25 12-CRS-0106 REVISED 8 FEB 2013 Proving correctness of SelectionSort 1. Place additional assertions inside the text of the SelectionSort program 2. Construct proofs of correctness of the subroutines used by SelectionSort

26 12-CRS-0106 REVISED 8 FEB 2013 First, Proving FindMax The pre and postcondition are: {mn}/*precondition*/ j=FindMax(A,m,n); {A[j]A[m:n]} /*postcondition*/

27 12-CRS-0106 REVISED 8 FEB 2013 Finding the position of the largest item

28 12-CRS-0106 REVISED 8 FEB 2013 Mathematical Induction i th trip – Statement i++ i=m+1 j=m – Statement if (A[i]>A[j]) j=i A[j]≥A[m:m+1]  A[j] ≥A[m:i] – If ( m<n) and ( i=m+1), then ( i≤n)  Loop invariant A[j]≥A[m:i] Λ (i≤n)  true

29 12-CRS-0106 REVISED 8 FEB 2013 (i+1) st trip – Test the while-condition (i!=n), suppose it is still true, so we have two facts:  (i ≠ n)  A[j]≥A[m:i] Λ (i≤n) → by combining those two facts, the loop invariant assertion will yield: A[j]≥A[m:i] Λ (i<n) – after i++ is executed, another assertion: A[j]≥A[m:i-1] Λ (i≤n) must hold. – The statement if (A[i]>A[j]) j=i; is executed, the loop invariant assertion: A[j]≥A[m:i] Λ (i≤n) still holds true

30 12-CRS-0106 REVISED 8 FEB 2013 The principle of proving correctness program using induction: If it’s true on any given trip, it will remain true on the next trip. Consequently, It’s true on every trip through the loop

31 12-CRS-0106 REVISED 8 FEB 2013 Finishing the proof We are now ready to go back and prove that SelectionSort(A,m,n) sorts A[m:n], using the fact we just establish that j=FindMax(A,m,n) We can add assertions in SelectionSort(A,m,n) as follows :

32 12-CRS-0106 REVISED 8 FEB 2013 Finishing the proof

33 12-CRS-0106 REVISED 8 FEB 2013 Assume condition (m<n) is true –Function FinMax(A,m,n) is called, so we have assertion A[MaxPosition] ≥ A[m:n] (line 14)  Meaning A[MaxPosition] is the largest item in the subarray A[m:n] Statement exchange A[m] – A[maxPosition] –A[m] = subarray’s first item –A[MaxPosition] = subarray’s largest item –After axchange, A[m] is the largest item in A[m:n] → so we have assertion A[m] ≥ A[m:n] (line 19) Outer Subproblem

34 12-CRS-0106 REVISED 8 FEB 2013 Smaller subproblems Recursive call SelectionSort(A,m+1,n) –Effect : rearranging the items in A[m+1:n] into decreasing order so we have assertion A[m+1]≥ A[m+2]≥… ≥A[n] (line 21) – So we have 2 facts :  A[m] ≥ A[m+1]  A[m+1]≥ A[m+2]≥… ≥A[n] → yields : A[m] ≥ A[m+1]≥ A[m+2]≥… ≥A[n] (line 27) – if condition (m,n) on line 10 was false, combining the falsehood (m<n) with precondition (m≤n) on line 4, implies (m=n) must have been true. A[m:m] only have 1 item, so there are no items to sort.

35 12-CRS-0106 REVISED 8 FEB 2013 Recursion Induction We used a second form of induction in constructing the proof of SelectionSort. This sometimes called RECURSION INDUCTION This Method is applied by assuming that recursive calls on smaller-sized subproblem within the text of the main outer recursive function, correctly solve the smaller subproblem

36 12-CRS-0106 REVISED 8 FEB 2013 Summary

37 12-CRS-0106 REVISED 8 FEB 2013 Correctness of Nonrecursive To prove P(n) is true for all n0: 1. First, prove P(0) 2. Then, assuming that P(n) is true for n, prove P(n+1) is true

38 12-CRS-0106 REVISED 8 FEB 2013 Correctness of Nonrecursive

39 12-CRS-0106 REVISED 8 FEB 2013 Correctness of Recursive To prove P(n) is true for all n0: 1. Prove P(0) 2. Assuming P(k) is true for all 0kn, prove P(n) is true

40 12-CRS-0106 REVISED 8 FEB 2013 Correctness of Recursive

41 12-CRS-0106 REVISED 8 FEB 2013 A subtle bug Incomplete assertions !

42 12-CRS-0106 REVISED 8 FEB 2013 Review questions 1. What is an assertion inside a program ? 2. What is a precondition ? 3. What is a postcondition ? 4. What is a correctness proof for a program ?

43 12-CRS-0106 REVISED 8 FEB 2013 Exercise What is the loop invariant inside the while-loop in the following program ? Prove it that it correctly computes the sum of the integers in the array A[1:n] ! int sum(Array A, int n) /* ass n>=0*/ { int i=1, S=0; while (i<=n){ S+=A[i]; i++; } return S; }

44 12-CRS-0106 REVISED 8 FEB 2013 Exercise

45 12-CRS-0106 REVISED 8 FEB 2013 Exercise

46 12-CRS-0106 REVISED 8 FEB 2013 Transforming & Optimizing Programs Recursion elimination is converting a recursive program to an equivalent iterative program. –Transferring the parameters –Making call –Returning from the call

47 12-CRS-0106 REVISED 8 FEB 2013 Program Transformations  exchange the laws that permit us to replace one form of a program with an equivalent form Example: – If condition ( i<n ) is simpler than ( i <= n && i != n ) – Polynomial ((x + 3)*x + 5)*x + 2 is simpler than x*x*x + 3*x*x + 5*x + 2

48 12-CRS-0106 REVISED 8 FEB 2013 SelectionSort Elimination

49 12-CRS-0106 REVISED 8 FEB 2013 Program transformation

50 12-CRS-0106 REVISED 8 FEB 2013

51

52 Iterative vs Recursive running time

53 12-CRS-0106 REVISED 8 FEB 2013 Exercises 1.Eliminate the tail recursion from the following program void RevString(char *S,int m,int n) { char c; if (m<n){ c=S[m];S[m]=S[n];S[n]=c; RevString(S,m+1,n-1); }

54 12-CRS-0106 REVISED 8 FEB 2013 Reference Standish, Thomas A. Data structures, Algorithms, & Software Principles in C. Addison wesley publishing company. 1995

55 12-CRS-0106 REVISED 8 FEB 2013 THANK YOU


Download ppt "12-CRS-0106 REVISED 8 FEB 2013 CSG3F3/ Desain dan Analisis Algoritma Lecture 1 : Introduction to Software Engineering Concepts Intelligence, Computing,"

Similar presentations


Ads by Google