1 / 54

# Algorithm Discovery and Design - PowerPoint PPT Presentation

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

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

## PowerPoint Slideshow about 'Algorithm Discovery and Design' - levia

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

• The Algorithmic Foundations of Computer Science

1

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

After studying this chapter, students will be able to:

• Illustrate the operation of sample algorithms

• 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

• 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

• 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

• 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

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

• 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

Types of algorithmic operations SJF, Fall2012

• Sequential – step by step

• Conditional – “if”

• Iterative - loop

Representing Algorithms (continued) SJF, Fall2012

• 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

Representing Algorithms (continued) SJF, Fall2012

• 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

Representing Algorithms (continued) SJF, Fall2012

• 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] SJF, Fall2012

• Components of a loop

• Continuation condition

• Loop body

• Infinite loop (avoid)

• The continuation condition never becomes false

• An error

Representing Algorithms (continued) SJF, Fall2012

• 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

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

Examples of Algorithmic Problem Solving SJF, Fall2012Example 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 SJF, Fall2012Example 1: Go Forth and Multiply (continued)

• Get input values

• Get values for a and b

• 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 SJF, Fall2012Example 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 SJF, Fall2012Example 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

Example 2: Looking, Looking, Looking SJF, Fall2012

• 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 SJF, Fall2012Example 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 SJF, Fall2012Example 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

Example 2: Looking, Looking, Looking (continued) SJF, Fall2012

• 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 SJF, Fall2012

• 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 SJF, Fall2012Example 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) SJF, Fall2012

• 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 SJF, Fall2012

• 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 SJF, Fall2012Example 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 SJF, Fall2012Example 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

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

Example 4: Meeting Your Match (continued) SJF, Fall2012

• 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 (continued) SJF, Fall2012

• 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) SJF, Fall2012

• 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

Pattern Matching SJF, Fall2012

• 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 SJF, Fall2012

• 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 SJF, Fall2012

• 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 Algorithms SJF, Fall2012

• 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 Algorithms SJF, Fall2012

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 SJF, Fall2012

• 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 SJF, Fall2012

• 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