Chapter 2: Design of Algorithms Representing algorithms How to express algorithms the best? Possible solutions: Natural language Natural language Programming language Pseudo-Code + Known by everyone (that speaks it) -- Hides the structure of the algorithm -- Allows ambiguity in expressions Programming language + Language that computer understands (e.g. Fortran, C, Pascal) + Very exact and well structured -- Very restrictive particularly in early design phases 1 Representing Algorithms Pseudo-Code: golden medium Bases on a natural language (e.g. English) Uses “programming-language”-like notation (e.g. statements, conditions etc.) Examples Set x to the value 45 Return Go to step 100 if condition is satisfied Types of pseudo-code instructions Sequential operations Conditional operations Iterative operations 2 Representing Algorithms Sequential operations Input/Output: Communication with the outside Input: Output Get the value for “variable1” “variable2” … Example: Get the value of x y z Print the value for “variable1” “variable2” … Example: Print the value of x y z Print the message ‘message content’ Example: Print the message ‘An error has been detected’ Computation: Set the value of “variable” to “arithmetic operation” Example: Set the value of Area to p*r2 3 Representing Algorithms Conditional operations: If “condition” is true then Set of algorithmic operations Else Set of algorithmic operations Example: If denominator = 0 then Print the message ‘Operation cannot be done’ Else Set fraction to numerator/denominator 4 Representing Algorithms Iterative operations Repeat Step i to j until “condition” becomes true Step i: … Step j: Repeat until “condition” becomes true Set of operations End of loop While “condition” remains true do Set of operations End of the loop 5 Representing Algorithms Examples of iterative operations Printing squares of 1 to n: Get the value of n While n > 0 do The same algorithm with a repeat loop: Get the value of n If n > 0 then Print n2 Set n to n-1 Repeat until n < 1 becomes true 2 Print n Set n to n-1 Difference between while and repeat: While: pre-check (continuation) condition then perform work (# of times 0, 1, 2, …) Repeat: perform work then post-check (termination) condition (# of times 1, 2, …) 6 Representing Algorithms Are the three types of operations (sequential, conditional, iterative) sufficient to represent ANY possible valid algorithm? YES! Even the most complex algorithms used in e.g. international switching systems can be represented using said operations Compare: We only need few letters (26 in English) to write the most marvelous novels. 7 Algorithmic Problem Solving Sequential Search Algorithm Problem Statement: Given a name N and a telephone book including names N1 to N1000 and phone numbers T1 to T1000 Task: Find the phone number T of the name N First solution: 1. 2. … 1001. 1002. Get N, N1,…,N1000, T1, …T1000 If N = N1 then Print the value T1 If N = N1000 then Print the value T1000 Stop 8 Sequential Search Algorithm Assessment of the first solution +: Algorithm prints all phone numbers of the given name N --: Too long listing (1001 lines) --: Slow: it checks always all entries even after the phone number has been already found --: No error message if the name is not in the list 9 Sequential Search Algorithm Second solution: 1. Get N, N1, …, N1000, T1, …, T1000 2. Set i to 1 and mark the N as not yet found 3. Repeat 4 to 7 until N is found 4. If N = Ni then 5. Print the Phone number is Ti 6. Mark N as found Else 7. Add 1 to i 8. Stop 10 Sequential Search Algorithm Assessment of the second solution +: Very short listing (only 8 lines) +: Quick if N is in the list, it does not check all entries in all cases --: Big problem: Endless loop if N is not in the list Final solution: 1. Get N, N1, …, N1000, T1, …, T1000 2. Set i to 1 and mark the N as not yet found 3. Repeat 4 to 7 until either N is found or i > 1000 4. If N = Ni then 5. Print the Phone number is Ti 6. Mark N as found Else 7. Add 1 to i 8. If N is not marked as found then Print message ‘Name is not in the directory’ 9. Stop 11 Finding the Maximum Given a list of n numbers Task: find the largest number in the list Solution: 1. Get the value n 2. Get the values A1, A2, …, An 3. Set the value of maximum to A1 4. Set the value of location to 1 5. Set the value of i to 2 6. While i <= n do 7. If Ai > maximum then 8. Set maximum to Ai 9. Set location to I 10. Add 1 to I End of the loop 11. Print maximum and location 12. Stop 12 Pattern Matching Given a text T (sequence of letters) and a pattern (e.g. word) p Find the occurrence of p in T Example: Text: Pattern: Output: Text: Pattern: Output: let me know whether or not to go no Match starting at position 9 let me know whether or not to go no Match starting at position 24 Example: Genome research Genome: … T C G A T T G T C C C A G T G C A A A C T G C A T … Probe: AAA (a match) 13 Pattern Matching Solution: 1. Get values n and m the size of the text and the pattern 2. Get T1,…,Tn and P1,…,Pm 3. Set k to 1 4. Repeat until k > n-m+1 (until we treated the entire text) 5. Try to match the pattern P1…Pm at position k 6. If there was a match then 7. Print the value of k the starting location of the match 8. Add 1 to k (slides to the right) End of loop 9. Stop 14 Pattern Matching Step 5 ??? 5. Try to match the pattern P1…Pm at position k Situation: Text: T1 T2 … Tk Tk+1…Tk+m-1 Tk+m …Tn Pattern: P 1 P2 … Pm Algorithm for Step 5 Set i to 1 and mismatch to no Repeat until (i > m) or mismatch = yes If Pi not equal Tk+i-1 then Else Set mismatch to yes Set i to i+1 End of loop 15