Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Back of the Envelope Estimates: A Problem-Solving Technique in Computer Science, Summaries of Piano

An understanding of 'back of the envelope' estimates, a problem-solving technique used in Computer Science to make rapid estimates, eliminate candidate solutions, establish feasibility, and sketch out potential trade-offs. The technique involves finding the connection between givens and results, making plans, carrying out the plan, and examining the result.

What you will learn

  • How does one check the plausibility of 'Back of the Envelope' estimates?
  • What are the general principles of 'Back of the Envelope' estimates?
  • What is the role of geometry and extrapolation in 'Back of the Envelope' estimates?
  • How does one make a plan using 'Back of the Envelope' estimates?
  • What is the purpose of 'Back of the Envelope' estimates in Computer Science?

Typology: Summaries

2021/2022

Uploaded on 09/27/2022

amlay
amlay 🇺🇸

4.1

(19)

384 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CompSci 001 5.1
Today’s topics
Designing and Implementing Algorithms
Problem solving
Pseudocode
Java
Syntax and Grammars
Upcoming
More Java
Acknowledgement
Marti Hearst, UC Berkeley
David Smith, Georgia tech
Reading
Computer Science, Chapter 5
Great Ideas, Chapter 2
CompSci 001 5.2
Problem Solving
Programming is a strenuous exercise in problem solving
G. Polya. How to Solve It, 2nd ed., Princeton University Press, 1957.
Understand the problem
What are its parts? unknown, data, condition
Does the problem make sense? Is it feasible?
Think about the problem, get a sense of what it needs
Make a plan
Find the connection between givens and result
What kind of problem is it? Is it familiar?
Think about generalizations, specializations, variants
Carry out the plan
Check each step
Examine the result
Does it make sense?
CompSci 001 5.3
Back of the envelope calculations
Jon Bentley. Programming Pearls. 2nd edition. A-W, 2000.
http://www.vendian.org/envelope/
Engineering technique to approximate and check answers
Two answers are better than one
Quick checks
Rules of thumb
Practice
Ad claims that salesperson drove 100,000 miles in a year.
True?
Newspaper article states that a United States quarter dollar
coin has “an average life of 30 years.” How can you check that
claim?
CompSci 001 5.4
Why “back of the envelope” estimates?
Often need to make rapid estimates
to eliminate candidate solutions
establish feasibility
sketch out potential trade-offs
Most remember key numbers related to their field, not every detail
Hence we need to estimate
which numbers are important
values of numbers needed
how to perform the calculation
Emphasis is on “order of magnitude” estimates
to nearest factor of 10 (or 2)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Back of the Envelope Estimates: A Problem-Solving Technique in Computer Science and more Summaries Piano in PDF only on Docsity!

CompSci 001

5.

Today’s topics

Designing and Implementing Algorithms

Syntax and GrammarsJavaPseudocodeProblem solving

Upcoming

More Java

Acknowledgement

David Smith, Georgia techMarti Hearst, UC Berkeley

Reading

Computer Science

, Chapter 5

Great Ideas

, Chapter 2

CompSci 001

Problem Solving

G. Polya. Programming is a strenuous exercise in problem solving

(^) How to Solve It,

(^) nd

ed., Princeton University Press, 1957.^



Understand the problem 

What are its parts? unknown, data, condition



Does the problem make sense? Is it feasible?



Think about the problem, get a sense of what it needs



Make a plan 

Find the connection between givens and result



What kind of problem is it? Is it familiar?



Think about generalizations, specializations, variants



Carry out the plan 

Check each step



Examine the result 

Does it make sense?

CompSci 001

5.

Back of the envelope calculations

Jon Bentley.

Programming Pearls.

2 nd

edition. A-W, 2000.^

 http://www.vendian.org/envelope/

Engineering technique to approximate and check answers 

Two answers are better than one

Quick checks

Rules of thumb

Practice

True?Ad claims that salesperson drove 100,000 miles in a year.

claim?coin has “an average life of 30 years.” How can you check thatNewspaper article states that a United States quarter dollar

CompSci 001

 Why “back of the envelope” estimates?

Often need to make rapid estimates 

to eliminate candidate solutions

establish feasibility

sketch out potential trade-offs

Most remember key numbers related to their field, not every detail

Hence we need to estimate 

which

numbers are important

values

(^) of numbers needed

how

to perform the calculation

Emphasis is on “order of magnitude” estimates 

to nearest factor of 10 (or 2)

CompSci 001

5.

 Orders of Magnitude

How far away is home? Is it more like 1, or 10, or 100 miles? 

Probably do not know exactly

Is it approximately "a couple", or "a few", or "a lot”

Estimate based on powers rather than multiples of 10

How tall is your dorm? More like 1, 10, 100, 1000 feet? 

1 foot tall is like a doll house, so that’s out

What do we know that is about 10 feet big? Hmm... People

If building is a couple of people high, 10 sounds good.

But that means 1000, would be 100 people high, so that’s out

So 10 or 100 depending on how many people tall the building is

Use orders of magnitude as brackets to find reasonable range

CompSci 001

 Example: How many piano tuners in NYC

Approximately how many people are in New York City? 

10,000,

Does every individual own a piano? 

No

Reasonable to assert “individuals do not own pianos; families do”? 

Yes

About how many families are there in a city of 10 million people? 

Perhaps there are 2,000,000 families

Does every family own a piano? 

No

Perhaps one out of every five does 

That would mean there are about 400,000 pianos in NYC

CompSci 001

5.

Example: Piano Tuners continued

How many piano tuners are needed for 400,000 pianos? 

Some people never get around to tuning their piano

Some people tune their piano every month

then there are 400,000 every yearAssume "on the average" every piano gets tuned once a year,

How many piano tunings can one piano tuner do? 

Assume that average piano tuner can tune four pianos a day

Assume that there are 200 working days per year

That means every tuner can tune about 800 pianos per year

How many piano tuners are needed in NYC? 

Number of tuners is approximately 400,000/800 or 500

CompSci 001

 Example: Piano Tuners summary

“Back of the Envelope” estimates have 

Formulas: provide roadmap to upcoming calculations

Estimates: brief justification of approximations in formula

Calculations: estimates and known facts are use in formula

Piano Tuner example 

# tuners = # pianos x # repairs / # repairs per day x # daysFormula:

#working days ~= 200 (5 x 50 – vacation, sickness)# repairs per day ~= 4# repairs ~= 1 per piano (some many, some none)# pianos ~= 400,000 (20% of 2,000,000 families own pianos)Estimates

# tuners ~= (400,000 x 1) / (4 x 200) = 500Calculation

CompSci 001

5.

Algorithms



Hand-waving not allowed!



making it work.Specifying algorithms requires you to say what is really involved in



Example:



How does a computer work?



Hand-wave: zeros & ones



Real answer: see later part of class.



You learn to know when you don’t know



“I know nothing except the fact of my ignorance.”



Socrates, from Diogenes Laertius, Lives of Eminent Philosophers

CompSci 001

Describing Algorithms

programming languageSpecific high-levelPseudo-codeNatural language (English) Pictures

expressed More easily

preciseMore

CompSci 001

5.

Pseudocode

A shorthand for specifying algorithms

Leaves out the implementation details

Leaves in the essence of the algorithm

What does this algorithm do?

How many times does it print Hello?

CompSci 001

Sequential search

CompSci 001

5.

Picking courses

Make a list of courses you want to register for, in order of priority

Start with empty schedule. Number of courses = 0.

Choose highest priority class on list.

steps):with classes already scheduled, then register for the class (2If the chosen class is not full and its class time does not conflict

Add the class to the schedule

Increment the number of classes scheduled

Cross that class off of your list.

>= 4, or until all classes have been crossed out.Repeat steps 3 through 5 until the number of classes scheduled is

Stop.

CompSci 001

Flowcharts

End

Begin

Make list of classes you want to take

Num Classes = 0

Choose highest priority class on list

Add the class to your schedule. Increment Num Classes.

Cross the class off your list.

yes yes yes

no

no

no

Is this class full?

Is there a time conflict?

Num Classes >= 4?

More classes on list?

yes

no

CompSci 001

5.

Programming Primitive Operations

Assign a value to a variable

Call a method

Arithmetic operation

Comparing two numbers

Indexing into an array

Following an object reference

Returning from a method

CompSci 001

Components of Computing Algorithms

Any computing algorithm will have AT MOST five

kinds of components:

  • Modules to make the algorithm manageable by• Control structures to act on decisions• Conditional expressions to make decisions• Instructions change data values• Data structures to hold data

abstraction, i.e., grouping related components

CompSci 001

5.

systems are involvedA distributed system is one where multiple separate computer 

Electronic card catalogs

The web

Java was designed for the web

lives?Question: What are examples of a distributed task in your

dynamic language.”portable, high performance, multi-threaded, andinterpreted, robust, secure, architecture-neutral, “Java is a simple, object-oriented, distributed,

CompSci 001 

Java a

high-level language

native tongue, machine languageHigh-level languages must be translated to a computer’s

and runintermediate form and then converted to machine languageInterpreted high-level languages are translated to an

Why?

We’ll learn more about this later

dynamic language.”portable, high performance, multi-threaded, andinterpreted, robust, secure, architecture-neutral, “Java is a simple, object-oriented, distributed,

CompSci 001

5.

reasonablyPrograms will have errors, but a good program degrades

down with itdo, but it should not bring down other unrelated programsA robust program may not do exactly what it is supposed to

have seen?Question: Give me an example of a non-robust program you

dynamic language.”portable, high performance, multi-threaded, andinterpreted, robust, secure, architecture-neutral, “Java is a simple, object-oriented, distributed,

CompSci 001 

computer cannot be read or compromisedSecurity: techniques that ensure that data stored on a

from erasing all of your data, accidentally or otherwise?A program is running on your computer. What is to stop it

Question: Is Java really all that secure?

dynamic language.”portable, high performance, multi-threaded, andinterpreted, robust, secure, architecture-neutral, “Java is a simple, object-oriented, distributed,

CompSci 001

5.

particular type of computer architecturesA language is architecture-neutral if it does not prefer a

run faster on one than an other.weaknesses. It is not too hard to construct a program that will(x86-Pentium) family have their own respective strengths andE.g. The Macintosh processor family (PowerPC) and the PC

thoughA particular program is never entirely architecture neutral

Question: When is being architecturally neutral a bad thing?

dynamic language.”portable, high performance, multi-threaded, andinterpreted, robust, secure, architecture-neutral, “Java is a simple, object-oriented, distributed,

CompSci 001 

many different computer systemsA program is portable if it will work the same (roughly) on

HTML is also platform-independent or portable

A whole lot of effort is currently spent

porting

(^) non-portable

code

dynamic language.”portable, high performance, multi-threaded, andinterpreted, robust, secure, architecture-neutral, “Java is a simple, object-oriented, distributed,

CompSci 001

5.

Performance: speed in completing some task

manufacturers.Performance is everything to most computer and software

Story: 

could drive across country in 5 minutes for $35.industry, one would be able to now buy a Roll Royce thatIf the transportation industry kept up with the computer

Rebuttal: 

It would crash once a week, killing everyone on board.

dynamic language.”portable, high performance, multi-threaded, andinterpreted, robust, secure, architecture-neutral, “Java is a simple, object-oriented, distributed,

CompSci 001 

independently of its other partsA thread is a part of the program that can operate

Multi-threaded programs can do multiple things at once 

other web pagese.g. download a file from the web while still looking at

at the same time?Question: What is the problem with multiple agents working 

Synchronization

dynamic language.”portable, high performance, multi-threaded, andinterpreted, robust, secure, architecture-neutral, “Java is a simple, object-oriented, distributed,

CompSci 001

5.

Formal specifications

Need a precise notation of syntax of a language

Grammars can be used for generation and also can be used

Context-free grammars

=>

sequence of letters and/or digits that begins with a letter

 => msg42 => guessB

can be generated this waySubstitute as many times as necessary. All legal statements 

Want

person

=

firstn

+

"

"

+

lastn;

How

do

we

get

this

from

our

grammar?

CompSci 001

A Grammar for Java

Need a set of rules

Our first one was a good start: 

=>

any string of alphanumeric symbols that

begins with a letter

Let’s add something to define a simple statement: 

=>

= (^)

;

And then work on the details: 

<oth-expression> => <string-expression> | <int-expression> |

<string-expression>

=>

=>

=>

any sequence of charcters

CompSci 001

5.

A Simple Statement



Now have enough to generate a statement like:

(^) msg =

“hello”;



Start with:

=>

(^)



Then using: =>

(^) any string of alphanumeric symbols that begins

with a letter

msg



<oth-expression>Then, using: => <string-expression> | <int-expression> |

msg

(^) <string-expression>



Using: <string-expression> =>

msg =

(^)



Using:

any sequence of charcters

msg = ”hello” ;

CompSci 001

A Grammar for Java



Including more rules to describe programs we have: 1.

=>

(^) any string of alphanumeric symbols that begins with a letter

2.

=>

(^)

3.

=>

(^) new

(^)

4.

5.

=>

(^) possibly empty list of

(^)

s separated

(^) by

commas

6.

expression> => <string-expresseion> | <int-expression> | <oth-

7.

<string-expression> => <string-expression>

<string-expression>

8.

<string-expression> =>

9.

=

any sequence of characters

10.

=

CompSci 001

5.

Using our Grammar

Use this to generate:

person

=

firstn

lastn;

Rule

Statement being Generated

2: => #

1: =>

person

6: =>

person

<str-expression>

7: =>

person

<str-expression>

<str-expression>

8: =>

person

<str-expression>

10: =>

person

+ <str-expression>

1: =>

person

firstn

<str-expression>

7: => person = firstn + <str-expression> + <str-expression>

8: =>

person

firstn

+ <str-expression>

9: =>

person

firstn

8: =>

person

firstn

10: =>

person

firstn

1: => =>

person

firstn

lastn;

CompSci 001

5.

Proving Grammatical Correctness

Why go through the process we went through? 

grammarShows that desired statement can be generated from this

Actually

proves

(^) that the statement is grammatically correct!

Same rigor as a mathematical proof

(Doesn’t prove that logic is correct, though)

covered so farActually need more rules to handle the level of Java we’ve 

Summary of rules shown on pages 78-79 of

(^) Great Ideas

Also give an example

for a complete applet

Too long to go through in class – Please Read!