Chapter 2
This presentation is the property of its rightful owner.
Sponsored Links
1 / 54

Chapter 2 The Algorithmic Foundations of Computer Science PowerPoint PPT Presentation


  • 99 Views
  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Algorithm discovery and design

  • Chapter 2

  • The Algorithmic Foundations of Computer Science

1


Objectives

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


Objectives continued

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


Introduction

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


Representing algorithms

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


Representing algorithms1

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


Representing algorithms2

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


Representing algorithms3

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


Algorithm discovery and design

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


Algorithm discovery and design

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


Types of algorithmic operations

Types of algorithmic operations

  • Sequential – step by step

  • Conditional – “if”

  • Iterative - loop


Representing algorithms continued

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


Algorithm discovery and design

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


Representing algorithms continued1

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


    Algorithm discovery and design

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


    Algorithm discovery and design

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


    Representing algorithms continued2

    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


    Iterative operations loops

    Iterative Operations [ loops]

    • Components of a loop

      • Continuation condition

      • Loop body

    • Infinite loop (avoid)

      • The continuation condition never becomes false

      • An error


    Algorithm discovery and design

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


    Algorithm discovery and design

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


    Representing algorithms continued3

    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


    Algorithm discovery and design

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


    Algorithm discovery and design

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


    Examples of algorithmic problem solving example 1 go forth and multiply

    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


    Examples of algorithmic problem solving example 1 go forth and multiply continued

    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


    Examples of algorithmic problem solving example 1 go forth and multiply continued1

    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


    Examples of algorithmic problem solving example 1 go forth and multiply continued2

    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


    Algorithm discovery and design

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


    Example 2 looking looking looking

    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


    Examples of algorithmic problem solving example 2 looking looking looking

    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


    Examples of algorithmic problem solving example 2 looking looking looking continued

    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


    Algorithm discovery and design

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


    Algorithm discovery and design

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


    Algorithm discovery and design

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


    Example 2 looking looking looking continued

    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


    Example 3 big bigger biggest

    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


    Examples of algorithmic problem solving example 3 big bigger biggest

    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


    Example 3 big bigger biggest continued

    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


    Algorithm discovery and design

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


    Example 4 meeting your match

    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


    Examples of algorithmic problem solving example 4 meeting your match

    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


    Examples of algorithmic problem solving example 4 meeting your match continued

    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


    Example 4 meeting your match continued

    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


    Example 4 meeting your match continued1

    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


    Example 3 meeting your match continued

    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


    Algorithm discovery and design

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


    Algorithm discovery and design

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


    Pattern matching

    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


    Summary

    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


    Important algorithms

    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


    Important algorithms1

    Important Algorithms

    • Find Largest

      • ( Get values) and Start at the beginning of list

      • Set found = false and Largest to first element

      • Repeat until end of list

        • Look at each element

        • If element > largest

        • set (found = true) and

        • (largest = element) and (location = i)

        • move pointer to next element

      • End loop

      • Print largest and location

      • Stop


    Important algorithms2

    Important Algorithms

    Pattern matching in text

    • Location 123456789……………….

    • Text A manand a woman

    • Pattern an

    • Output There is a match at position 4, etc.

      Patterns in genes

    • Gene: TCAGGCTAATCGGAAGT

    • Probe: TAATC

    • Match: Yes!


    Definitions

    Definitions

    • Algorithm Discovery – finding a correct and efficient solution to a problem

    • Library – collection of useful algorithms

    • Iteration – repeated operations

    • Index – the position of an element in a list

    • Abstraction- ability to separate the high level view of an object from the low level details

    • Top-Down Design – viewing an operation at a high level of abstraction and later adding the details in steps

      • ( stepwise refinement)


    Research

    Research

    • Write an algorithm that generates a Caesar cipher (See p. 86, #21) and Chapter 13

    • Investigate the field of bioinformatics

      • Basic Explanations

        htthttp://en.wikipedia.org/wiki/Bioinformatics

        http://biotech.icmb.utexas.edu/pages/bioinfo.html

      • BLAST – tool used to search protein databases

        http://www.ncbi.nlm.nih.gov/Education/BLASTinfo/information3.html


  • Login