1 / 29

Algorithmic Problem Solving: Examples and Principles

This lecture discusses examples of algorithmic problem solving and the principles of automation, abstraction, and data organization. It includes examples of straight-line algorithms, sequential search algorithm, finding the largest number in a list, and pattern matching.

christam
Download Presentation

Algorithmic Problem Solving: Examples and Principles

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 4 Examples of Algorithmic Problem Solving(S&G, §2.3) CS 100 - Lecture 4

  2. Read S&G ch. 3 for TuesdayFirst Quiz (ch. 1-2):Next Thursday CS 100 - Lecture 4

  3. Algorithm Discovery • Discovering correct & efficient algorithm for a problem requires: • intelligence • hard work • past experience • artistic skill • plain good luck • Practice is essential CS 100 - Lecture 4

  4. Search Problem • We have a list of names: N1, N2, …, N10000 • We have a corresponding list of telephone numbers: T1, T2, …, T10000 • 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 CS 100 - Lecture 4

  5. Straight-Line Algorithm • Get Name, N1, …, N10000, T1, …, T10000 • If Name = N1 then print the value of T1 • If Name = N2 then print the value of T2 • If Name = N3 then print the value of T3 … 10000 If Name = N9999 then print the value of T9999 10001 If Name = N10000 then print the value of T10000 10002 Stop CS 100 - Lecture 4

  6. Automation Principle Automate mechanical, tedious, or error-prone activities CS 100 - Lecture 4

  7. Abstraction Principle Avoid requiring something to be stated more than once;factor out the recurring pattern CS 100 - Lecture 4

  8. 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 CS 100 - Lecture 4

  9. Sequential Search Algorithm • Get values for Name, N1, …, N10000 and for T1, …, T10000 • Set the value of i to 1 and set the value of Found to no • Repeat steps 4 through 8 until Found = yes • If Name = Ni then • Print the value of Ti • Set the value of Found to yes • Else (Name is not equal to Ni) • Add 1 to i • Stop CS 100 - Lecture 4

  10. 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. CS 100 - Lecture 4

  11. 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) CS 100 - Lecture 4

  12. 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 CS 100 - Lecture 4

  13. Find-Largest Problem • Given n 2 • A list of n unique numbers A1, A2, …, 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 CS 100 - Lecture 4

  14. 19 22 63 12 41 A4 A5 A3 A2 A1 location largestso far i Doing It By Hand 4 3 2 5 2 4 1 19 41 63 CS 100 - Lecture 4

  15. Find Largest Algorithm:Initialization Get a value for n, the size of the list Get values for A1, A2, …, An, the list to be searched Set the value of largest so far to A1 Set the value of location to 1 Set the value of i to 2 CS 100 - Lecture 4

  16. 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 CS 100 - Lecture 4

  17. Find Largest Algorithm:Wrap-up Print out the values of largest so far and location Stop CS 100 - Lecture 4

  18. Pattern Matching Problem • Given a text of n characters T1T2…Tn • Given a pattern of m characters P1P2…Pm • Goal: locate every occurrence of the pattern in the text • Output the location of the beginning of each match CS 100 - Lecture 4

  19. Match! 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 the the the the the CS 100 - Lecture 4

  20. First Draft: Initialization Get values for n and m, the sizes of the text and the pattern, respectively Get values for both the text T1T2…Tn and the pattern P1P2…Pm Set k, the starting location for the attempted match, to 1 CS 100 - Lecture 4

  21. 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 CS 100 - Lecture 4

  22. 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 CS 100 - Lecture 4

  23. 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 CS 100 - Lecture 4

  24. Tk+(i–1) Pi Attempting the Pattern Match T1T2 … Tk Tk+1Tk+2 … Tk+(m–1) … Tn P1P2P3 … Pm CS 100 - Lecture 4

  25. 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 CS 100 - Lecture 4

  26. Print Successful Matches If Mismatch = no then Print the message ‘There is a match at position’ Print the value of k CS 100 - Lecture 4

  27. Repeat until we have fallen off of end of text Repeat until k > (n – m + 1) T1T2 … Tn–m+1Tn–m+2 … Tn–1 Tn P1P2 … Pm–1Pm CS 100 - Lecture 4

  28. 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” CS 100 - Lecture 4

  29. What is a “Good” Algorithm • First (and above all), it must be correct • Second, it should be efficient enough • S&G, ch. 3 CS 100 - Lecture 4

More Related