introduction to algorithms. what is computer science? computer science is the study of computers...

31
Introduction to Algorithms

Upload: lillian-neal

Post on 03-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Introduction to Algorithms

Page 2: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

What is Computer Science?Computer Science is the study of computers (??)

• This leaves aside the theoretical work in CS, which does not make use of real computers, but of formal models of computers

• A lot of work in CS is done with pen and paper!

• Actually, the early work in CS took place before the development of the first computer

• Computer Science is no more about computers than astronomy is about telescopes, biology is about microscopes, or chemistry is about test tubes. Science is not about tools. It is about how we use them, and what we find out we can do.

Page 3: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

What is Computer Science?

Computer Science is the study of how to write computer programs (programming) (??)

• Programming is a big part of CS.. ..but it is not the most important part.

Computer Science is the study of the uses and applications of computers and software (??)

• Learning to use software packages is no more a part of CS than driver’s education is part of automotive engineering.

• CS is responsible for building and designing software.

Page 4: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

What is computer science?

• The study of algorithms:– their formal properties• correctness, limits• efficiency/cost

– their hardware realizations• computer design

– their linguistic realizations• programming languages

– their applications• network design, ocean modeling, bioinformatics, ...

Page 5: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

What is an algorithm?

… a well-defined procedure that allows an agent to solve a problem.

Algorithms must:1. Be well-ordered and unambigous2. Be executable (understandable)3. Solve the problem, and4. Terminate.

Note: often the agent is a computer or a robot…

Page 6: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Example Algorithms

• Cooking a dish • Making a peanut-butter jelly sandwich• Shampooing hair• Programming a VCR• Making a pie

Page 7: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Examples

Is this an algorithm?

• Step 1: Wet hair• Step 2: Lather• Step 3: Rinse• Step 4: Repeat

Page 8: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Example

• Problem: Adding two n-digit numbers

7597831 + 1287525-------------------

8885356

How would you write an algorithm to solve this problem? Assume the basic operation is adding one-digit numbers.

Page 9: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Types of Operations

• Basic operations– Wet hair– Rinse– Turn on VCR

• Conditional operations– If batter is too dry add water

• Repeat/looping operations– Repeat step 1 and 2 three times– Repeat steps 2,3,4,…10 until batter becomes soft.

Page 10: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Expressing algorithms

• Is natural language good? – For daily life, yes…but for CS it lacks structure and

would be hard to follow– Too rich, ambiguous, depends on context

• How about a programming language? – Good, but not when we try to solve a problem..we

want to think at an abstract level– It shifts the emphasis from how to solve the problem

to tedious details of syntax and grammar.

Page 11: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Pseudocode

• Pseudocode = English but looks like programming

• Good compromise – Simple, readable, no rules, don’t worry about

punctuation. Lets you think at an abstract level about the problem.

– Contains only instructions that have a well-defined structure and resemble programming languages

Page 12: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Pseudocode elements

• Basic operations– Read the input from user– Print the output to the user– Carry out basic arithmetical computations

• Conditional operations– Execute an operation if a condition is true

• Repeat operations– Execute a block of operation multiple times until a

certain condition is met

Page 13: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Basic operations

– Read the input from user• Get x • Get a, b, c

– Print the output to the user• Print x• Print “Your mileage is ” x

– Cary out basic arithmetical computations• Set x to 10• Set y to x*x/3

Page 14: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Conditional statements

Specify a statement that may or may not be done:if <condition> then

<statement to be done>else <statement to be done otherwise>

Exampleif the value of carry is 0 then set the value of a to 0else set the vale of a to a+1

Page 15: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Loop statements

specify a group of statements that may be done several times (repeated):

repeat until <condition>< statements to be repeated >

• How does this work? – Condition is evaluated– If it is true than the loop terminates and the next instruction to be

executed will be the instruction immediately following the loop– If it is false, then the algorithm executes the <statements to be

repeated> in order, one by one

Page 16: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

ExampleVariables: count, squareStep 1: set count to 1Step 2: repeat step 3 to step 5 until count is > 10

Step 3: set square to count *count Step 4: print value of square and value of countStep 5: add 1 to count

Step 6: end

• What does this algorithm do? • Note: indentation

– Not necessary, but makes reading/understanding algorithms easier

Page 17: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Pseudocode examplesEquivalent:

– Set the value of a to 1– Set a to 1– a=1

Equivalent– Add 1 to count– Set count to count + 1– Increment the value of count by 1– count = count + 1

Writing in pseudocode gives you the freedom to choose any of these!

Page 18: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Pseudocode Examples• Incorrect

– Set 1 to a– Add a + b (what do you do with the result?)– Set a+b +3

• Note: not the same– set a to b – set b to a

• Example: what is the output of the following algorithms?set a to 2 set a to 2set b to 4 set b to 4set a to b set b to aprint a, b print a, b

Page 19: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Problem

• Adding two n-digit numbers

7597831 + 1287525-------------------

8885356

How would you write an algorithm to solve this problem? Assume the basic operation is adding one-digit numbers.

Page 20: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

List variables• How to represent (in pseudocode) inputs of arbitrary size?• Suppose that we need to read 100 numbers from the user, or

1000, or..– we could give each variable a different name…tedious!!

• Use a list variable:– Variable: list a of size n– This means that a is a list of n elements: a1, a2, a3,…,an

– To read the list from the user use• Get n and a1, a2, a3,…,an

– To print the list use• Print a1, a2, a3,…,an

– We can treat each element in the list as a variable• Set a3 to 5• Set a4 to a3 +2

Page 21: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Algorithm for adding two m-digit numbers (Fig 1.2)

Given: m ≥ 1 and two positive numbers a and b, each containing m digits, compute the sum c = a + b.

Variables: m, list a0 ..am-1, list b0 …. bm-1 , list c0 …cm-1 cm, carry, i

0 Get values for m, am-1 … a0 and bm-1 … b0

1 Set the value of carry to 0.2 Set the value of i to 0.3 Repeat steps 4-6 until i > m-1

4 Set the value of ci to ai + bi + carry

5 if ci ≥ 10 then

subtract 10 from ci and set the value of carry to 1

else set the value of carry to 06 Add 1 to i

7 Set the value of cm to carry8 Print value of c = cm cm-1 cm-2 … c0

Page 22: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

So, how does this work?

For example, the input is m = 4, a = 3276, and b = 7345.After step 0, the variables m, a, and b have those values:

m

a3 a2 a1 a0 b3 b2 b1 b0

4 3 2 7 6 7 3 4 5

After steps 1 and 2, the variables i and carry are initialized.

i 0 carry 0

Next, steps 4-6 are repeated until the value of i > 3. Each repetition computes a single digit of c.

c4 c3 c2 c1 c0

Page 23: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

A model for visualizing an algorithm’s behavior

Algorithm

Computer

Input (keyboard)

Output (screen)

Variables

Page 24: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

E.g., Visualizing Fig

m

a3 a2 a1 a0

b3 b2 b1 b0

4

3 2 7 6

7 3 4 5

i 0 carry 0

c4 c3 c2 c1 c0

0 Get values for ……8 Print value of …

Computer

Input (keyboard)

Output (screen)

432767345

Page 25: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Algorithm for computing MPG

Write a pseudocode algorithm to compute the distance traveled and the average miles per gallon on a trip when given as input the number of gallons used and the starting and ending mileage readings on the odometer.

Variables: response, gallons, start, end, distance, mpg0 Set response to “Yes”1 Repeat steps 2-8 until response = “No”

2 Get gallons, start, end3 Set distance to end - start4 Set mpg to distance ÷ gallons5 Print mpg6 if mpg > 25.0 then print “You are getting good gas

mileage” else print “You are NOT getting good gas mileage”7 Print “Do you want to do this again, Yes or No?”8 Get response

9 Stop

Page 26: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

So, how does this work?

For example, suppose we use 25 gallons, beginning at 12000 and ending at 13000 on the odometer. Then, after step 2, some variables have the following values:

response gallons

12000

start

Yes 25

After step 4, the variables distance and mpg are computed.

mpg 40

Steps 5-9 displays these results on the output screen:40You are getting good gas mileageDo you want to do this again, Yes or No?

end

distance

13000

1000

Page 27: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Visualizing Fig

response

end

distance

Yes gallons

start

mpg

0 Set response ……11 Stop

Computer

Input (keyboard)

Output (screen)

251200013000

Page 28: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

Designing Algorithms: A Methodology

1. Read the problem, identifying the input and the output.

2. What variables are needed?3. What computations are required to achieve the

output?4. Usually, the first steps in your algorithm bring

input values to the variables.5. Usually, the last steps display the output6. So, the middle steps will do the computation.7. If the process is to be repeated, add loops.

Page 29: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

How was the MPG program designed?

Problem statement: Write a pseudocode algorithm to compute the distance traveled and the average miles per gallon on a trip when given as input the number of gallons used and the starting and ending mileage readings on the odometer.

Input: number of gallons, starting mileage, ending mileageOutput: distance traveled, average miles per gallonVariables: gallons, start, end, distance, mpgCalculate: distance = end - start

mpg = distance / gallonsPut the steps in order: input, calculate, output (steps 2-8)Determine if a loop is needed (steps 0, 1, 9, 10)

Page 30: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

A Search Algorithm

Problem statement: Write a pseudocode algorithm to find the location of a target value in a list of values.

Input: a list of values and the target value

Output: the location of the target value, or else a message that the value does not appear in the list.

Variables:

Page 31: Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which

The (sequential) search algorithm

Variables: target, n, list of n valuesGet the value of target, n, and the list of n valuesSet index to 1Set found to falseRepeat until found = true or index > n

If the value of listindex = target then

Output the indexSet found to true

elseIncrement the index by 1

If not found thenOutput a message that target was not found

Stop