invitation to computer science 6 th edition chapter 2 the algorithmic foundations of computer...

60
Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Upload: audra-gaines

Post on 12-Jan-2016

237 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science 6th Edition

Chapter 2

The Algorithmic Foundations of Computer Science

Page 2: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Objectives

In this chapter, you will learn about:

• Representing algorithms

• Examples of algorithmic problem solving

2

Page 3: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Introduction

• Chapter 1 – Introduced algorithms and algorithmic problem

solving

• This chapter– Develops more fully the notions of algorithm and

algorithmic problem solving

3

Page 4: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Representing Algorithms

• Pseudocode– Natural language: used to express algorithms– Problems using natural language to represent

algorithms• Natural language can be extremely verbose

• Lack of structure makes it difficult to locate specific sections of the algorithm

• Natural language is too “rich” in interpretation and meaning

4

Page 5: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.1 The Addition Algorithm of Figure 1.2 Expressed in Natural Language

5

Page 6: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Representing Algorithms(continued)

• 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

6

Page 7: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.2 The Beginning of the Addition Algorithm of Figure 1.2 Expressed in aHigh-Level Programming Language

7

Page 8: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Pseudocode (continued)

• Natural languages– Not sufficiently precise to represent algorithms

• High-level programming language– During the initial phases of design, we are forced to

deal with detailed language issues

8

Page 9: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Pseudocode

• Pseudocode– Used to design and represent algorithms– A compromise between the two extremes of natural

and formal languages

9

Page 10: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 1.2 Algorithm for Adding Two m-digit Numbers

10

Page 11: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Pseudocode(continued)

• English language constructs modeled to look like statements available in most programming languages

• Steps presented in a structured manner (numbered, indented, and so on)

• No fixed syntax for most operations is required

11

Page 12: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

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

12

Page 13: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Sequential Operations

• Basic sequential operations – Computation, input, and output

• Instruction for performing a computation and saving the result– Set the value of “variable” to “arithmetic expression”

• Variable – Storage location that can hold a data value

13

Page 14: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Sequential Operations (continued)

• Pseudocode – Not a precise set of notational rules to be memorized

and rigidly followed

• Input operations – Submit to the computing agent data values from the

outside world that it may then use in later instructions

• Output operations – Send results from the computing agent to the

outside world

14

Page 15: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.3 Algorithm for Computing Average Miles per Gallon

15

Page 16: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition 16

Page 17: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Conditional and Iterative Operations

• Sequential algorithm– Sometimes called a straight-line algorithm– Executes its instructions in a straight line from top to

bottom and then stops

• Control operations– Conditional and iterative– Allow us to alter the normal sequential flow of control

in an algorithm

17

Page 18: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Conditional and Iterative Operations (continued)

• Conditional statements– Ask questions and choose alternative actions based

on the answers– If then else– Example

• if x is greater than 25 then

print x

else

print x times 100

18

Page 19: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.4 The If/Then/Else Pseudocode Statement

19

Page 20: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.5 Second Version of the Average Miles per Gallon Algorithm

20

Page 21: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Conditional and Iterative Operations (continued)

• Loop– The repetition of a block of instructions– While statement

• Continuation condition– Determines if statement is true or false

• Infinite loop– Continuation condition never becomes false

21

Page 22: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Conditional and Iterative Operations (continued)

• Examples

– while j > 0 do

set s to s + aj

set j to j – 1

– do

print ak

set k to k + 1

while k < n22

Page 23: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.6 Execution of the While Loop23

Page 24: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Conditional and Iterative Operations (continued)

• Loop example

Step Operation

1 Set the value of count to 1

2 While (count ≤ 100) do step 3 to step 5

3 Set square to (count x count)

4 Print the values of count and square

5 Add 1 to count

24

Page 25: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Conditional and Iterative Operations (continued)

• Pretest loop – Continuation condition is tested at the beginning of

each pass through the loop

• Posttest loop– Continuation condition is tested at the end of the

loop body, not the beginning

• Primitives– Instructions that computing agent understands and is

capable of executing without further explanation

25

Page 26: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.7 Third Version of the Average Miles per Gallon Algorithm

26

Page 27: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.8 Execution of the Do/While Posttest Loop

27

Page 28: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition 28

Page 29: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.9 Summary of Pseudocode Language Instructions

29

Page 30: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Examples of Algorithmic Problem Solving

• Go Forth and Multiply: Multiply two numbers using repeated addition

• 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

Invitation to Computer Science, 6th Edition 30

Page 31: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Examples of Algorithmic Problem Solving

• Example 1: Go Forth and MultiplyGiven 2 nonnegative integer values, a ≥ 0, b ≥ 0, compute and output the product (a 3 b) using the technique of repeated addition. That is, determine the value of the sum a + a + a + . . . + a (b times)

31

Page 32: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Examples of Algorithmic Problem Solving((continued))

• Algorithm outline– Create a loop that executes exactly b times, with

each execution of the loop adding the value of a to a running total

32

Page 33: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

• Get values for a and b

• Set the values 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

• Print the value of product

Invitation to Computer Science, 6th Edition 33

Page 34: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.10 Algorithm for Multiplication of Nonnegative Values via Repeated Addition

34

Page 35: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition 35

Page 36: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition 36

Page 37: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Modified version of Multiply

• Devise an algorithm that uses a loop to take in a positive integer n and a real number x as the input and computes and print xn . Assume that the computing agent knows how to multiply but do not know how to calculate exponential. Hint: x0 = 1; x1 = x; x2 = x*x; x3= x*x*x; etc. Make sure you stop the algorithm at some point.

Invitation to Computer Science, 6th Edition 37

Page 38: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Example 2: Looking, Looking, Looking

• Algorithm discovery– Finding a solution to a given problem

• Sequential search– Standard algorithm for searching an unordered list of

values

• Binary Search (extended example)– In computer science, a binary search or half-interval

search algorithm finds the position of a specified value (the input “key”) within a sorted array.

38

Page 39: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

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

Invitation to Computer Science, 6th Edition 39

Page 40: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.11 First Attempt at Designing a Sequential Search Algorithm

40

Page 41: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

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

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

• Problems of naïve sequential search algorithm– Only works for collections of exactly one size– Duplicates the same operations over and over

41

Page 42: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

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 (such as a name not found in

the collection)– Uses the variable Found to exit the iteration as soon

as a match is found

Invitation to Computer Science, 6th Edition 42

Page 43: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.12 Second Attempt at Designing a Sequential Search Algorithm

43

Page 44: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.13 The Sequential Search Algorithm

44

Page 45: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition 45

Page 46: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

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 is organized

Invitation to Computer Science, 6th Edition 46

Page 47: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Example 3: Big, Bigger, Biggest

• Library– Collection of useful algorithms

• ProblemGiven 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

47

Page 48: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.14 Algorithm to Find the Largest Value in a List

48

Page 49: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition 49

Page 50: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition 50

Page 51: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Example 4: Meeting Your Match

• Pattern matching– Process of searching for a special pattern of symbols

within a larger collection of information– Is assisting microbiologists and geneticists studying

and mapping the human genome

51

Page 52: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Example 4: Meeting Your Match (continued)

• Pattern-matching problemYou will be given some text composed of n characters that will be referred to as T1 T2 . . . 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 pattern within the text. The output of the algorithm is the location in the text where each match occurred. For this problem, the location of a match is defined to be the index position in the text where the match begins

52

Page 53: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Example 4: Meeting Your Match (continued)

• STEP 1– The matching process: T1 T2 T3 T4 T5 … P1 P2 P3

• STEP 2– The slide forward: T1 T2 T3 T4 T5 …– 1-character slide -> P1 P2 P3

53

Page 54: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.15 First Draft of the Pattern-Matching Algorithm

54

Page 55: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Figure 2.16 Final Draft of the Pattern-Matching Algorithm

55

Page 56: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition 56

Page 57: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Example 4: Meeting Your Match (continued)

• Examples of higher-level constructs– Sort the entire list into ascending order– Attempt to match the entire pattern against the text– Find a root of the equation

• Abstraction– Use of high-level instructions during the design

process

• Top-down design– Viewing an operation at a high level of abstraction

57

Page 58: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Summary

• Algorithm design – A first step in developing an algorithm

• Algorithm design must: – Ensure the algorithm is correct– Ensure the algorithm is sufficiently efficient

• Pseudocode – Used to design and represent algorithms

58

Page 59: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Invitation to Computer Science, 6th Edition

Summary (continued)

• Pseudocode – Readable, unambiguous, and able to be analyzed

• Algorithm design – Uses multiple drafts and top-down design to develop

the best solution

• Abstraction – A key tool for good design

59

Page 60: Invitation to Computer Science 6 th Edition Chapter 2 The Algorithmic Foundations of Computer Science

Test 1

• 4 types questions (11)– Concept: computer science, algorithm, Von

Neumann architecture– Design: using Pseudocode code– Correction: verify the given algorithms– Discussion: be creative & comprehensive

• Close notes & Close books

• Panther Card

• Feb 4th: 3:00-4:15pm

• Assignment 1 due on Feb 4th before 4:15pmInvitation to Computer Science, 6th Edition 60