Lecture 4 Examples of Algorithmic Problem Solving SG
Lecture 4 Examples of Algorithmic Problem Solving (S&G, § 2. 3) 3/11/2021 CS 100 - Lecture 4 1
Read S&G ch. 3 for Tuesday First Quiz (ch. 1 -2): Next Thursday 3/11/2021 CS 100 - Lecture 4 2
Algorithm Discovery • Discovering correct & efficient algorithm for a problem requires: – intelligence – hard work – past experience – artistic skill – plain good luck • Practice is essential 3/11/2021 CS 100 - Lecture 4 3
Search Problem • We have a list of names: N 1, N 2, …, N 10000 • We have a corresponding list of telephone numbers: T 1, T 2, …, T 10000 – That is, Ti is the phone number of the person whose name is Ni • We have a Name and want to print this person’s number 3/11/2021 CS 100 - Lecture 4 4
Straight-Line Algorithm 1 2 3 4 Get Name, N 1, …, N 10000, T 1, …, T 10000 If Name = N 1 then print the value of T 1 If Name = N 2 then print the value of T 2 If Name = N 3 then print the value of T 3 … 10000 If Name = N 9999 then print the value of T 9999 10001 If Name = N 10000 then print the value of T 10000 10002 Stop 3/11/2021 CS 100 - Lecture 4 5
Automation Principle Automate mechanical, tedious, or error-prone activities 3/11/2021 CS 100 - Lecture 4 6
Abstraction Principle Avoid requiring something to be stated more than once; factor out the recurring pattern 3/11/2021 CS 100 - Lecture 4 7
Application to Searching Problem • In this case the recurring pattern is: If Name = Ni then print the value of Ti • We want to execute this command for successive values of i = 1, 2, …, 10000 • Until one of them succeeds 3/11/2021 CS 100 - Lecture 4 8
Sequential Search Algorithm 1. Get values for Name, N 1, …, N 10000 and for T 1, …, T 10000 2. Set the value of i to 1 and set the value of Found to no 3. Repeat steps 4 through 8 until Found = yes 4. If Name = Ni then 5. Print the value of Ti 6. Set the value of Found to yes 7. Else (Name is not equal to Ni) 8. Add 1 to i 9. Stop 3/11/2021 CS 100 - Lecture 4 9
Importance of Data Organization • Straight-line algorithm tested all 10000 • Sequential search tests 5000 on average • Could do much better by alphabetizing the list of names • “The selection of an algorithm to solve a problem is greatly influenced by the way the data are organized. ” — S&G • “Pick the right data structure and the algorithm will design itself. ” — Anon. 3/11/2021 CS 100 - Lecture 4 10
Another Example: Find Largest • Purpose: – given a list of numbers – find the largest number & its position in list • Useful in itself • Can also be used in building other algorithms (e. g. , sorting algorithm) 3/11/2021 CS 100 - Lecture 4 11
Programming in the Large • Do not always have to start from the primitive operations – programming in the small • Can make use of already developed algorithms • These can be collected in libraries • Programming in the large • Abstraction Principle 3/11/2021 CS 100 - Lecture 4 12
Find-Largest Problem • Given n 2 • A list of n unique numbers A 1, A 2, …, An • Find and print out: – the largest number in the list – the position in the list of the largest number • For example: – input: n = 5, list 19, 41, 12, 63, 22 – output: 63, 4 3/11/2021 CS 100 - Lecture 4 13
Doing It By Hand 3/11/2021 19 41 12 63 22 A 1 A 2 A 3 A 4 A 5 5342 421 19 41 63 i location largest so far CS 100 - Lecture 4 14
Find Largest Algorithm: Initialization Get a value for n, the size of the list Get values for A 1, A 2, …, An, the list to be searched Set the value of largest so far to A 1 Set the value of location to 1 Set the value of i to 2 3/11/2021 CS 100 - Lecture 4 15
Find Largest Algorithm: Main Loop Repeat until i > n If Ai > largest so far then Set largest so far to Ai Set location to i Add 1 to the value of i End of the loop 3/11/2021 CS 100 - Lecture 4 16
Find Largest Algorithm: Wrap-up Print out the values of largest so far and location Stop 3/11/2021 CS 100 - Lecture 4 17
Pattern Matching Problem • Given a text of n characters T 1 T 2…Tn • Given a pattern of m characters P 1 P 2…Pm • Goal: locate every occurrence of the pattern in the text • Output the location of the beginning of each match 3/11/2021 CS 100 - Lecture 4 18
Doing It By Hand E. g. , search for the pattern ‘the’ in the text: to be or not to be, that is the question the the Match! 3/11/2021 CS 100 - Lecture 4 19
First Draft: Initialization Get values for n and m, the sizes of the text and the pattern, respectively Get values for both the text T 1 T 2…Tn and the pattern P 1 P 2…Pm Set k, the starting location for the attempted match, to 1 3/11/2021 CS 100 - Lecture 4 20
First Draft: Main Loop Repeat until we have fallen off the end of the text Attempt to match every character in the pattern, beginning at position k of the text If there was a match then Print the value of k, the beginning location Add 1 to k, which slides the pattern forward End of the loop 3/11/2021 CS 100 - Lecture 4 21
Top-Down Design • First draft uses high-level statements: – until we have fallen off the end of the text – Attempt to match every character in the pattern, beginning at position k • These are not primitive operations • Must eventually be defined, but definition can be deferred • “Divide and conquer” strategy 3/11/2021 CS 100 - Lecture 4 22
Process of Top-Down Design • Divide problem to be solved into a small number of simpler subproblems • Further divide the subproblems into simpler subproblems • Continue until the simplest subproblems can be easily implemented in terms of the primitive operations 3/11/2021 CS 100 - Lecture 4 23
Attempting the Pattern Match T 1 T 2 … Tk Tk+1 Tk+2 … Tk+(m– 1) … Tn P 1 P 2 P 3 … Pm Tk+(i– 1) Pi 3/11/2021 CS 100 - Lecture 4 24
Attempt to match every character in the pattern, beginning at k Set the value of i to 1 Set the value of Mismatch to no Repeat until either (i > m) or (Mismatch = yes) If Pi ≠ Tk+(i– 1) then Set Mismatch to yes Else Increment i by 1 (to move to next character) End of the loop 3/11/2021 CS 100 - Lecture 4 25
Print Successful Matches If Mismatch = no then Print the message ‘There is a match at position ’ Print the value of k 3/11/2021 CS 100 - Lecture 4 26
Repeat until we have fallen off of end of text T 1 T 2 … Tn–m+1 Tn–m+2 … P 1 P 2 Tn– 1 Tn … Pm– 1 Pm Repeat until k > (n – m + 1) 3/11/2021 CS 100 - Lecture 4 27
Examples and Analysis • We may work out a couple examples to figure out the stopping condition • But we should do the algebra to make sure • There is a danger of “off by one” errors – miss match in last position – try to look beyond end of list • Typical program “bugs” 3/11/2021 CS 100 - Lecture 4 28
What is a “Good” Algorithm • First (and above all), it must be correct • Second, it should be efficient enough – S&G, ch. 3 3/11/2021 CS 100 - Lecture 4 29
- Slides: 29