1 / 54

Chapter 2 The Algorithmic Foundations of Computer Science

Chapter 2 The Algorithmic Foundations of Computer Science. 1. Objectives. After studying this chapter, students will be able to: Explain the benefits of pseudocode over natural language or a programming language Represent algorithms using pseudocode

levia
Download Presentation

Chapter 2 The Algorithmic Foundations of Computer Science

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. Chapter 2 • The Algorithmic Foundations of Computer Science 1

  2. Objectives After studying this chapter, students will be able to: • Explain the benefits of pseudocode over natural language or a programming language • Represent algorithms using pseudocode • Identify algorithm statements as sequential, conditional, or iterative • Define abstraction and top-down design, and explain their use in breaking down complex problems Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  3. Objectives (continued) After studying this chapter, students will be able to: • Illustrate the operation of sample algorithms • multiplication by repeated addition • sequential search of a collection of values • finding the maximum element in a collection • finding a pattern string in a larger piece of text Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  4. Introduction • Algorithms for everyday may not be suitable for computers to perform (as in Chapter 1) • Algorithmic problem solving focuses on algorithms suitable for computers • Pseudocode is a tool for designing algorithms • This chapter will use a set of problems to illustrate algorithmic problem solving Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  5. Representing Algorithms • Natural language • Language spoken and written in everyday life • Examples: English, Spanish, Arabic, etc. • Problems with using natural language for algorithms • Verbose • Imprecise • Relies on context and experiences to give precise meaning to a word or phrase

  6. Representing Algorithms • High-level programming language • Examples: C++, Java • Problem with using a high-level programming language for algorithms • During the initial phases of design, we are forced to deal with detailed language issues

  7. Representing Algorithms Pseudocode • English language constructs modeled to look like statements available in most programming languages • Steps presented in a structured manner (numbered, indented, etc.) • No fixed syntax for most operations is required • Less ambiguous, more readable than natural language • Emphasis is on process, not notation • Well-understood forms allow logical reasoning about algorithm behavior • Can be easily translated into a programming language

  8. Representing Algorithms • Pseudocodeis used to design algorithms • Natural language is: • expressive, easy to use • verbose, unstructured, and ambiguous • Programming languages are: • structured, designed for computers • Formal syntax, grammar • grammatically fussy, cryptic • Pseudocode lies somewhere between these two Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  9. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  10. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  11. Types of algorithmic operations • Sequential – step by step • Conditional – “if” • Iterative - loop

  12. Representing Algorithms (continued) • Sequential operations perform a single task • Input: gets data values from outside the algorithm • Computation: a single numeric calculation • Output: sends data values to the outside world • A variable is a named location to hold a value • A sequential algorithm is made up only of sequential operations • Example: computing average miles per gallon Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  13. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  14. Representing Algorithms (continued) • Control operation: changes the normal flow of control • Conditional statement: asks a question and selects among alternative options • Evaluate the true/false condition • If the condition is true, then do the first set of operations and skip the second set • If the condition is false, skip the first set of operations and do the second set • Example: check for good or bad gas mileage Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  15. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  16. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  17. Representing Algorithms (continued) • Iteration: an operation that causes looping, repeating a block of instructions • While statement repeats while a condition remains true • continuation condition: a test to see if while loop should continue • loop body: instructions to perform repeatedly • Example: repeated mileage calculations Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  18. Iterative Operations [ loops] • Components of a loop • Continuation condition • Loop body • Infinite loop (avoid) • The continuation condition never becomes false • An error

  19. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  20. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  21. Representing Algorithms (continued) • Do/while, repeat/until alternate iterative operation • continuation condition appears at the end • loop body always performed at least once • post-test loop • Primitive operations: sequential, conditional, and iterative are all that is needed Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  22. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  23. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  24. Examples of Algorithmic Problem SolvingExample 1: Go Forth and Multiply “Given two nonnegative integer values, a ≥ 0, b ≥ 0, compute and output the product (a × b) using the technique of repeated addition. That is, determine the value of the sum a + a + a + . . . + a (b times).” Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  25. Examples of Algorithmic Problem SolvingExample 1: Go Forth and Multiply (continued) • Get input values • Get values for a and b • Compute the answer • Loop b times, adding each time* • Output the result • Print the final value* * steps need elaboration Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  26. Examples of Algorithmic Problem SolvingExample 1: Go Forth and Multiply (continued) • Loop b times, adding each time • Set the value of count to 0 • While (count < b) do • … the rest of the loop* • Set the value of count to count + 1 • End of loop * steps need elaboration Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  27. Examples of Algorithmic Problem SolvingExample 1: Go Forth and Multiply (continued) • Loop b times, adding each time • Set the value of count to 0 • Set the value of product to 0 • While (count < b) do • Set the value of product to (product + a) • Set the value of count to count + 1 • End of loop • Output the result • Print the value of product Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  28. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  29. Example 2: Looking, Looking, Looking • Examples of algorithmic problem solving: Searching • 2. Sequential search: find a particular value in an unordered collection • 3. Find maximum: find the largest value in a collection of data • 4. Pattern matching: determine if and where a particular pattern occurs in a piece of text

  30. Examples of Algorithmic Problem SolvingExample 2: Looking, Looking, Looking “Assume that we have a list of 10,000 names that we define as N1, N2, N3, . . . , N10,000, along with the 10,000 telephone numbers of those individuals, denoted as T1, T2, T3, . . . , T10,000. To simplify the problem, we initially assume that all names in the book are unique and that the names need not be in alphabetical order.” Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  31. Examples of Algorithmic Problem SolvingExample 2: Looking, Looking, Looking (continued) • Finding the correct solution to a problem is called algorithm discovery and is the most challenging and creative part of the problem-solving process. • Three versions here illustrate algorithm discovery, working toward a correct, efficient solution • A sequential algorithm (no loops or conditionals) • An incomplete iterative algorithm • A correct algorithm Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  32. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  33. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  34. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  35. Example 2: Looking, Looking, Looking (continued) • Correct sequential search algorithm • Uses iteration to simplify the task • Refers to a value in the list using an index (or pointer) • Handles special cases (like a name not found in the collection) • Uses the variable Found to exit the iteration as soon as a match is found

  36. Example 3: Big, Bigger, Biggest • Task • Find the largest value from a list of values • Algorithm outline • Keep track of the largest value seen so far (initialized to be the first in the list) • Compare each value to the largest seen so far, and keep the larger as the new largest

  37. Examples of Algorithmic Problem SolvingExample 3: Big, Bigger, Biggest • A “building-block” algorithm used in many libraries • Library: A collection of pre-defined useful algorithms “Given a value n ≥ 1 and a list containing exactly n unique numbers called A1, A2, . . . , An, find and print out both the largest value in the list and the position in the list where that largest value occurred.” Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  38. Example 3: Big, Bigger, Biggest (continued) • Find Largest algorithm • Uses iteration and indices like previous example • Updates location and largest so far when needed in the loop

  39. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  40. Example 4: Meeting Your Match • Task • Find if and where a pattern string occurs within a longer piece of text • Algorithm outline • Try each possible location of pattern string in turn • At each location, compare pattern characters against string characters

  41. Examples of Algorithmic Problem SolvingExample 4: Meeting Your Match • Pattern-matching: common across many applications • word processor search, web search, image analysis, human genome project “You will be given some text composed of n characters that will be referred to as T1T2 . . . Tn. You will also be given a pattern of m characters, m ≤ n, that will be represented as P1 P2 . . . Pm. The algorithm must locate every occurrence of the given pattern within the text. The output of the algorithm is the location in the text where each match occurred.” Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  42. Examples of Algorithmic Problem SolvingExample 4: Meeting Your Match (continued) • Algorithm has two parts: • Sliding the pattern along the text, aligning it with each position in turn • Given a particular alignment, determine if there is a match at that location • Solve parts separately and use • Abstraction, focus on high level, not details • Top-down design, start with big picture, gradually elaborate parts Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  43. Example 4: Meeting Your Match (continued) • Abstraction • Separating high-level view from low-level details • Key concept in computer science • Makes difficult problems intellectually manageable • Allows piece-by-piece development of algorithms

  44. Example 4: Meeting Your Match (continued) • Top-down design • When solving a complex problem: • Create high-level operations in first draft of an algorithm • After drafting the outline of the algorithm, return to the high-level operations and elaborate each one • Repeat until all operations are primitives

  45. Example 3: Meeting Your Match (continued) • Pattern-matching algorithm • Contains a loop within a loop • External loop iterates through possible locations of matches to pattern • Internal loop iterates through corresponding characters of pattern and string to evaluate match

  46. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  47. Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  48. Pattern Matching • Some applications • Matching phrases in Literature or text • Find a pattern and replace it with another • Web search ( “Google”)- matching strings • Patterns in X-rays, CAT scans, etc. • Patterns in the human genome – whole new field of bioinformatics

  49. Summary • Pseudocode is used for algorithm design: structured like code, but allows English and math phrasing and notation • Pseudocode is made up of: sequential, conditional, and iterative operations • Algorithmic problem solving involves: • Step-by-step development of algorithm pieces • Use of abstraction, and top-down design Invitation to Computer Science, 6th Editionm, modified by SJF, Fall2012

  50. Important Algorithms • Sequential search • ( Get values) and Start at the beginning of list • Set found = false • Repeat until found or end of list • Look at each element • If element = target • set found = true and • print desired element else move pointer to next element • End loop • If found = false then print message “not on list” • Stop

More Related