csci140 algorithms

43
Chapter 6 Problem Solving and Algorithm Design (adapted by SLU faculty)

Upload: shruti-shah

Post on 07-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 1/43

Page 2: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 2/43

2

Chapter Goals

• Describe the computer problem-solving process

and relate it to Polya’s How to Solve It list

• Distinguish between following an algorithm anddeveloping one

• Describe the pseudocode constructs used inexpressing an algorithm

• Use pseudocode to express an algorithm• Apply top-down design methodology to develop

an algorithm to solve a problem

Page 3: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 3/43

3

Problem Solving

Problem solving

The act of finding a solution to a perplexing,distressing, vexing, or unsettled question

How do you define problem solving? 

Page 4: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 4/43

4

Problem Solving

How to Solve It: A New Aspect of 

Mathematical Method by George Polya

"How to solve it list" written within the

context of mathematical problems

But the list is quite general

We can use it to solve computerrelated problems!

Page 5: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 5/43

5

Problem Solving

How do you solve problems? 

Understand the problemDevise a plan

Carry out the plan

Look back

Page 6: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 6/43

6

Strategies

Ask questions!

 – What do I know about the problem? 

 – What is the information that I have to process 

in order the find the solution?  – What does the solution look like? 

 – What sort of special cases exist? 

 – How will I recognize that I have found the solution? 

Page 7: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 7/43

7

Strategies

Never reinvent the wheel!

Similar problems come up again and againin different guises

A good programmer recognizes a task orsubtask that has been solved before and

plugs in the solution

Can you think of two similar problems? 

Page 8: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 8/43

8

Strategies

Divide and Conquer!

Break up a large problem into smaller unitsand solve each smaller problem

 – Applies the concept of abstraction

 – The divide-and-conquer approach can be

applied over and over again until eachsubtask is manageable

Page 9: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 9/43

9

Algorithms

Algorithm

A set of unambiguous instructions for solving aproblem or subproblem in a finite amount of time

using a finite amount of data 

Why must instructions be unambiguous? 

Why must time and data be finite? 

Page 10: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 10/43

10

Computer Problem-SolvingAnalysis and Specification Phase

AnalyzeSpecification

Algorithm Development Phase

Develop algorithm

Test algorithmImplementation Phase

Code algorithm

Test algorithm

Maintenance PhaseUse

Maintain

Page 11: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 11/43

11

Following an Algorithm

Figure 6.4 A recipe for Hollandaise sauce

Page 12: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 12/43

12

Following an Algorithm

Algorithm for preparing a Hollandaise sauce

If concerned about cholesterol 

Put butter substitute in a pot Else 

Put butter in a pot 

Turn on burner Put pot on the burner 

While (NOT bubbling)Leave pot on the burner 

Put other ingredients in the blender 

Turn on blender While (more in pot)

Pour contents into lender in slow steam Turn off blender 

Page 13: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 13/43

13

Developing an Algorithm

Two methodologies used to develop

computer solutions to a problem – Top-down design focuses on the tasks to be

done

 – Object-oriented design focuses on the datainvolved in the solution

But first, let's look at a way to expressalgorithms: pseudocode

Page 14: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 14/43

14

PseudocodePseudocode

A mixture of English and formatting to makethe steps in an algorithm explicit

There are no syntax rules in pseudocode

Pseudocode is not case sensitive

Algorithm to Convert base-10 number to other bases

While ( the quotient is not zero )Divide the decimal number by the new base Make the remainder the next digit to the left in the answer Replace the original decimal number with the quotient 

Page 15: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 15/43

15

Following Pseudocode

What is 93 in base 8?

93/8 gives 11 remainder 5

11/6 gives 1 remainder 31/ 8 gives 0 remainder 1

answer 1 3 5

While ( the quotient is not zero )Divide the decimal number by the new base 

Make the remainder the next digit to the left in the answer Replace the original decimal number with 

Page 16: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 16/43

16

Following Pseudocode

Easier way to organize solution

Page 17: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 17/43

17

Pseudocode for Complete

Computer SolutionWrite "Enter the new base" 

Read newBase 

Write "Enter the number to be converted" 

Read decimalNumber 

Set quotient to 1

While (quotient is not zero)

Set quotient to decimalNumber DIV newBase 

Set remainder to decimalNumber REM newBase 

Make the remainder the next digit to the left in the answer 

Set decimalNumber to quotient 

Write "The answer is " 

Write answer 

Page 18: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 18/43

18

Pseudocode Functionality

Variables

Names of places to store values

quotient, decimalNumber, newBase 

Assignment

Storing the value of an expression into a

variableSet quotient to 64 

quotient <-- 64 

quotient <-- 6 * 10 + 4 

Page 19: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 19/43

19

Pseudocode Functionality

Output

Printing a value on an output deviceWrite, Print 

Input

Getting values from the outside word and

storing them into variablesGet, Read 

Page 20: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 20/43

20

Pseudocode Functionality

Repetition

Repeating a series of statementsSet count to 1

While ( count < 10)

Write "Enter an integer number" 

Read aNumber 

Write "You entered " + aNumber 

Set count to count + 1

How many values were read? 

Page 21: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 21/43

21

Pseudocode Functionality

Selection

Making a choice to execute or skip a statement (orgroup of statements)

Read number 

If (number < 0)

Write number + " is less than zero." 

or

Write "Enter a positive number." 

Read number 

If (number < 0)

Write number + " is less than zero." 

Write "You didn't follow instructions." 

Page 22: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 22/43

22

Pseudocode Functionality

Selection

Choose to execute one statement (or group ofstatements) or another statement (or group ofstatements)

If ( age < 12 )

Write "Pay children's rate" 

Write "You get a free box of popcorn" 

else If ( age < 65 )

Write "Pay regular rate" 

else 

Write "Pay senior citizens rate" 

Page 23: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 23/43

23

Pseudocode Example

Write "How many pairs of values are to be entered?" 

Read numberOfPairs 

Set numberRead to 0 

While (numberRead < numberOfPairs)

Write "Enter two values separated by a blank; press return" 

Read number1

Read number2 

If (number1 < number2)

Print number1 + " " + number2 

Else 

Print number2 + " " number1

Increment numberRead 

Page 24: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 24/43

24

Walk Through

Data Fill in values during each iteration3 numberRead number1 number2  

55 70

2 1

33 33

numberOfPairs 

What is the output? 

Page 25: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 25/43

25

Top-Down Design

Top-Down Design

Problem-solving technique in which the problem is dividedinto subproblems; the process is applied to each subproblem

Modules

Self-contained collection of steps, that solve a problem orsubproblem

Abstract Step

An algorithmic step containing unspecified details

Concrete Step

An algorithm step in which all details are specified

Page 26: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 26/43

26

Top-Down Design

Process continues for as many levels as it takes to make every stepconcrete

Name of (sub)problem at one level becomes a module at next lowerlevel

Figure 6.5An exampleof top-downdesign

Page 27: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 27/43

27

A General Example

Planning a large party

Figure 6.6 Subdividing the party planning

Page 28: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 28/43

28

A Computer Example

Problem

Create a list that includes each person’sname, telephone number, and e-mail

address – This list should then be printed in alphabetical

order

 – The names to be included in the list are on

scraps of paper and business cards

Page 29: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 29/43

29

A Computer Example

Main Level 0

Enter names and numbers into list Put list into alphabetical order Print list 

Enter names and numbers into list Level 1

While ( more names)Enter name 

Enter telephone number Enter email address 

Insert information into list 

Which steps are abstract? Which steps are concrete? What is missing? 

Page 30: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 30/43

30

A Computer Example

Enter names and numbers into list (revised) Level 1

Set moreNames to true While (moreNames)

Prompt for and enter name Prompt for and enter telephone number Prompt for and enter email address 

Insert information into list Write "Enter a 1 to continue or a 0 to stop." Read response 

If (response = 0)Set moreNames to false 

Which steps are concrete? Which steps are abstract? 

Page 31: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 31/43

31

A Computer Example

Prompt for and enter name Level 2

Write "Enter last name; press return." 

Read lastName Write "Enter first name; press return." Read firstName 

Prompt for and enter telephone number Level 2

Write "Enter area code and 7-digit number; press return." 

Read telephoneNumber 

Prompt for and enter email address Level 2

Write "Enter email address; press return." 

Read emailAddress 

Page 32: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 32/43

32

A Computer Example

Put list into alphabetical order

Concrete or abstract? 

Print the list Level 1

Write "The list of names, telephone numbers, and email 

addresses follows:" Get first item from the list While (more items)

Write item's firstName + " " + lastName Write item's telephoneNumber 

Write item's emailAddress Write a blank line 

Get next item from the list 

Page 33: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 33/43

33

A Computer Example

Note: Insert information is within the loop

Page 34: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 34/43

34

Testing the Algorithm

Important distinction

Mathematics

We test the answer 

Programs

We test the process 

Page 35: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 35/43

35

Testing the Algorithm

Desk checking

Working through a design at a desk with a pencil and paper

Walk-through

Manual simulation of the design by team members, taking

sample data values and simulating the design using thesample data

Inspection

One person (not the designer) reads the design (handedout in advance) line by line while the others point out errors

Page 36: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 36/43

36

Important Threads

Programming language

A set of grammar rules, symbols, and special words used

to construct a program

Program

A sequence of instructions written to perform a specified

task

Syntax

The formal grammar rules governing the construction of

valid instructions

Semantics

The rules that give meaning to the instructions

Page 37: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 37/43

37

Important Threads

Information Hiding

The practice of hiding the details of a module withthe goal of controlling access to it

AbstractionA model of a complex system that includes onlythe details essential to the viewer

Information Hiding and Abstraction are twosides of the same coin

Page 38: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 38/43

38

Important Threads

Data abstraction

Separation of the logical view of data from theirimplementation

Procedural abstraction

Separation of the logical view of actions from theirimplementation

Control abstraction

Separation of the logical view of a control structure from itsimplementation

Page 39: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 39/43

39

Important Threads

Abstraction is the most powerful tool people have for managing complexity! 

Page 40: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 40/43

40

Important Threads

Identifiers

Names given to data and actions, by which – we access the data and

Read firstName, Set count to count + 1

 – execute the actions

name.initialize(), name.print()

Giving names to data and actions is a formof abstraction

Page 41: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 41/43

41

Ethical Issues

Licensing Computer Professionals

Are computer professionals licensed? 

What is the ACM and why is it opposed 

to licensing? What is the IEEE and what is its 

position on licensing? Should computer professionals be licensed? 

Page 42: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 42/43

42

Who am I?

I am a 

mathematician.Why is my picture in a book about 

computer science? 

Page 43: Csci140 Algorithms

8/4/2019 Csci140 Algorithms

http://slidepdf.com/reader/full/csci140-algorithms 43/43

43

Do you know?

What does TNDM stand for and what is it? 

How is a computer data base being used 

to help endangered species? 

What is forensic computing? What techniques does it use? 

How is physical evidence protected?