Chapter 2
Download
1 / 52

Chapter 2 - PowerPoint PPT Presentation


  • 248 Views
  • Updated On :

Chapter 2. Algorithm Discovery and Design. Objectives. In this chapter, you will learn about: Representing algorithms Examples of algorithmic problem solving. Introduction. This chapter discusses algorithms and algorithmic problem solving using three problems: Searching lists

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 2' - sherlock_clovis


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
Chapter 2 l.jpg

Chapter 2

Algorithm Discovery

and Design


Objectives l.jpg
Objectives

In this chapter, you will learn about:

  • Representing algorithms

  • Examples of algorithmic problem solving


Introduction l.jpg
Introduction

  • This chapter discusses algorithms and algorithmic problem solving using three problems:

    • Searching lists

    • Finding maxima and minima

    • Matching patterns


Representing algorithms l.jpg

Representing Algorithms

Natural Language - too verbose, ambiguous and lacks structure.

Formal programming languages – forces low level details, punctuation, grammar syntax.

Pseudocode - English like statements, very readable, no grammatical rules, etc., is easily translated into a programming language.


Representing algorithms5 l.jpg
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 algorithms7 l.jpg
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


Slide8 l.jpg

The Beginning of the Addition Algorithm

Expressed in a High-Level Programming Language


Representing algorithms9 l.jpg
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


Representing algorithms10 l.jpg
Representing Algorithms

  • Pseudocode ( continued)

  • Less ambiguous and 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


Types of algorithmic operations l.jpg
Types of algorithmic operations

  • Sequential

  • Conditional

  • Iterative


Sequential operations l.jpg
Sequential Operations

  • Sequential algorithm

    • Also called straight-line algorithm

    • Executes its instructions in a straight line from top to bottom and then stops

  • Computation

    Set the value of “variable” to “arithmetic expression”

    where a Variable is a named storage location that can hold a data value

    For Example:

    Set the value of c to a+b

    or Add a and b to get c


Sequential operations13 l.jpg
Sequential Operations

  • Input operations

    • To receive data values from the outside world

    • Examples:

      Get values for “variable1”, ‘variable2”,...

      Get a value for r, the radius of the circle

  • Output operations

    • To send results to the outside world for display

    • Examples:

      Print values for “variable1”, “variable2”,...

      Print the value of Area



Conditional and iterative operations l.jpg
Conditional and Iterative Operations

  • Control operations

    • Conditional operations

      • “Question asking” operations

      • Often use “if”

    • Iterative operations

      • Looping operations

      • Often use while or repeat


Conditional operations l.jpg
Conditional Operations

  • Conditional operations

    • Ask questions and choose alternative actions based on the answers

    • Also called branch or decision operations

    • Example

      • if x is greater than 25 then

        print x

        else

        print x times 100


Iterative operations l.jpg
Iterative Operations

  • Iterative operations

    • Perform “looping” behavior; repeating actions until a continuation condition becomes false

    • Loop

      • The repetition of a block of instructions


Iterative operations loops l.jpg
Iterative Operations [loops]

Pseudocode ( Counting Loop – For Loop)

Repeat step I to step J until the “True/False” condition becomes true

Step I: operation

……..

Step J :operation


Iterative operations loops19 l.jpg
Iterative Operations [loops]

Pseudocode ( While loop)

Repeat while a “True/False” Condition is false

operation

…..

operation

End of the loop


Iterative operations loops20 l.jpg
Iterative Operations [loops]

Pseudocode (Repeat Until Loop)

Repeat until a “True/False” Condition becomes true

operation

…..

operation

End of the loop


Iterative operations loops21 l.jpg
Iterative Operations (loops)

  • Examples

    • For J = 1 to 10

      print J

    • while j > 0 do

      set s to s + aj

      set j to j - 1

    • repeat

      print ak

      set k to k + 1

      until k > n



Iterative operations loops23 l.jpg
Iterative Operations [ loops]

  • Components of a loop

    • Continuation condition

    • Loop body

  • Infinite loop

    • The continuation condition never becomes false

    • An error


Iterative operations loops24 l.jpg
Iterative Operations [ loops]

  • Pretest loop

    • Continuation condition tested at the beginning of each pass through the loop

    • It is possible for the loop body to never be executed

    • While loop


Iterative operations loops25 l.jpg
Iterative Operations [ loops]

  • Posttest loop

    • Continuation condition tested at the endof loop body

    • Loop body must be executed at least once

    • Do/While loop




Example 1 go forth and multiply l.jpg
Example 1: Go Forth and Multiply

A Simple looping problem

Multiply two numbers using repeated addition

  • Get values for a and b

  • Determine the value of a+a+a+… a+ (b times)

  • Use a loop that executes b times and adds the value of a to a total each time

  • Print the value of the product


Example 2 looking looking looking l.jpg
Example 2: Looking, Looking, Looking

  • Examples of algorithmic problem solving

    • Sequential search: find a particular value in an unordered collection

    • Find maximum: find the largest value in a collection of data

    • Pattern matching: determine if and where a particular pattern occurs in a piece of text


Example 2 looking looking looking continued l.jpg
Example 2: Looking, Looking, Looking (continued)

  • Task

    • Find a particular person’s name from an unordered list of telephone subscribers

  • Algorithm outline

    • Start with the first entry and check its name, then repeat the process for all entries


Example 2 looking looking looking continued31 l.jpg
Example 2: Looking, Looking, Looking (continued)

  • Algorithm discovery

    • Finding a solution to a given problem

  • Naïve sequential search algorithm

    • For each entry, write a separate section of the algorithm that checks for a match

    • Problems

      • Only works for collections of exactly one size

      • Duplicates the same operations over and over


Example 2 looking looking looking continued32 l.jpg
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 2 looking looking looking continued34 l.jpg
Example 2: Looking, Looking, Looking (continued)

  • The selection of an algorithm to solve a problem is greatly influenced by the way the data for that problem are organized


Example 3 big bigger biggest l.jpg
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


Example 3 big bigger biggest continued l.jpg
Example 3: Big, Bigger, Biggest (continued)

  • Once an algorithm has been developed, it may itself be used in the construction of other, more complex algorithms

  • Algorithms can be used as the building blocks of software

  • Library

    • A collection of useful algorithms

    • An important tool in algorithm design and development


Example 3 big bigger biggest continued37 l.jpg
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



Example 4 meeting your match l.jpg
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


Example 4 meeting your match continued l.jpg
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 continued41 l.jpg
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 l.jpg
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



Summary l.jpg
Summary

  • Algorithm design is a first step in developing an algorithm

  • Must also:

    • Ensure the algorithm is correct

    • Ensure the algorithm is sufficiently efficient

  • Pseudocode is used to design and represent algorithms


Pattern matching l.jpg
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


Summary46 l.jpg
Summary

  • Pseudocode is readable, unambiguous, and analyzable

  • Algorithm design is a creative process; uses multiple drafts and top-down design to develop the best solution

  • Abstraction is a key tool for good design


Important algorithms l.jpg
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 algorithms48 l.jpg
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 algorithms49 l.jpg
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 l.jpg
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


Definitions51 l.jpg
Definitions

  • 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 l.jpg
Research

  • Write an algorithm that generates a Caesar cipher ( see p. 76) and Chapter 13

  • Investigate the field of bioinformatics

    • Basic Explanations

      http://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