Algorithms and Software Chapter 7 Some of Chapter 8 Sections 17.2, 17.3 What If There Isn’t an “Obvious” Way.

Download Report

Transcript Algorithms and Software Chapter 7 Some of Chapter 8 Sections 17.2, 17.3 What If There Isn’t an “Obvious” Way.

Algorithms and Software
Chapter 7
Some of Chapter 8
Sections 17.2, 17.3
What If There Isn’t an “Obvious” Way
Square Root
9
=
16
=
Square Root
9
=
12
=
16
=
1
6
11
16
21
26
31
36
41
46
51
56
61
66
71
76
81
86
91
96
101
106
111
116
121
126
131
136
Square Root
14
12
10
8
6
Series1
4
2
0
Hot Water
Iteration
Guacamole
Iteration
Iteration
The key idea:
1. Make a first guess.
2. Until answer is good enough:
1. See how close guess is to being right.
2. If not good enough, see how to move and choose a next
guess.
Square Root
1643 – 1727
Idea here: Newton’s Method:
Approximate with straight lines.
Riding the Line


f(x) = x - 1
Riding the Line


f(x) = x - 1

Square Root
Example:
f(x) = x2 - 90


Make a first guess, let’s say: 1. We want to ride the curve up to
where it crosses the x-axis. But we don’t know how to.
So pretend it were a straight line and ride it up.
Square Root
Example:
f(x) = x2 - 90



Start at 5.
Pretend the curve were a straight line and ride it up to 12.
Square Root
Example:
f(x) = x2 - 90


Start at 12.
Pretend the curve were a straight line and ride it down to 9.
Square Root
Doing this more generally to
find sqrt(n):
f(x) = x2 – n
f(x)= 2x



xi
The line we have drawn:
To find where it crosses:
y = f(xi) + (xi+1 – xi)*2xi
0 = f(xi) + (xi+1 – xi)*2xi
Square Root
The line we have drawn:
To find where it crosses:
f(x) = x2 – n
y = f(xi) + (xi+1 – xi)*2xi
0 = f(xi) + (xi+1 – xi)*2xi
0 = xi2 – n + (xi+1 – xi)*2xi
0 = xi2 – n + 2xi+1xi – 2xi2
xi2 + n = 2xi+1xi
xi2 + n = xi+1
2xi
xi + n
xi = xi+1
2
Square Root


def newton(n):
guess = 1
while guess not good enough:
guess = (guess + n/guess)/2
return(guess)

xi
Square Root



xi
def newton(n):
guess = 1
while abs(guess**2 - n) > .00000001*n:
guess = (guess + n/guess)/2
return(guess)
http://balance3e.com/Ch8/Newton.html
Correctness
How do we know that our loops will halt?
def newton(n):
guess = 1
while abs(guess**2 - n) > .00000001*n:
guess = (guess + n/guess)/2
return(guess)
Harder here. It is possible to prove that it will converge and in
fact that it will do so very quickly.
The Beauty of an Algorithm
def newton(n):
guess = 1
while abs(guess**2 - n) > .00000001*n:
guess = (guess + n/guess)/2
return(guess)
It’s not necessary to undertstand why it is correct in
order to execute it. You can execute it without
understanding. So can a computer.
Of course, someone has to prove once that it is
correct or we wouldn’t trust our key systems to it.
Invariants
Invariants
Invariants
Until no further beans can be removed:
Randomly choose two beans.
If the two beans are the same color:
Throw both away and add a new black bean.
If the two beans are different colors:
Throw away the black one and return the white one.
What color will the remaining bean be?
The Coffee Can Problem
Invariants
We have run a distributed (parallel) Monte Carlo simulation.
What did we observe?
Preserve parity of whites. So:
• Odd whites: white
• Even whites: black
The Coffee Can Problem
The Trivia Challenge Project
http://www.cs.utexas.edu/~ear/cs302/trivia.txt
An Episode You Can't Refuse
On the Run With a Mammal
Let's say you turn state's evidence and need to "get on the
lamb." If you wait /too long, what will happen?
You'll end up on the sheep
You'll end up on the cow
You'll end up on the goat
You'll end up on the emu
1
Lambs are baby sheep.
http://www.cs.utexas.edu/~ear/cs302/Homeworks/Trivia.html
Key Ideas
• Divide and conquer
• Read input from a file
• Handling exceptions
Key Ideas
• Divide and conquer
Divide and Conquer
def chess(board):
while game_on:
internal_board = scan(board)
move = choose(internal_board)
play(move, board)
Recall the idea: decompose into
pieces that make sense.
Divide and Conquer
Guacamole
Salsa
Joe
Chips
Smoked brisket
Cole slaw
Potato salad
Bill
Sarah
Jim
Chocolate cake
Apple pie
Casey
Moolenium crunch
Allison
Key Ideas
• Divide and conquer
• Read input from a file
• Handling exceptions
Let’s Look at the Code
http://www.cs.utexas.edu/~ear/cs302/triviagameprogram.txt
Searching
8
76 45 3
12 98 52 87 66 4
9
55 21 34 63 93 96 55 1
• How many steps to look for 55?
• How many steps to look for 88?
• How many steps to look for 77?
How many steps on average for a list of length n?
88
Searching
8
76 45 3
12 98 52 87 66 4
9
55 21 34 63 93 96 55 1
• How many steps to look for 55?
• How many steps to look for 88?
• How many steps to look for 77?
How many steps on average for a list of length n?
O(n/2) or just O(n)
88
Searching a Sorted List
Binary Search
2
9
13 17 19 22 28 30 35 40 43 55 61 69 73 83 86 91 93 99
mid
1st compare
Look for 93.
Binary Search
2
9
13 17 19 22 28 30 35 40 43 55 61 69 73 83 86 91 93 99
mid
2nd compare
Look for 93.
Binary Search
2
9
13 17 19 22 28 30 35 40 43 55 61 69 73 83 86 91 93 99
mid
3rd compare
Look for 93.
Binary Search
2
9
13 17 19 22 28 30 35 40 43 55 61 69 73 83 86 91 93 99
mid
4th compare
Look for 93.
Binary Search
def binary_search(list,object):
first = 0
last = len(list) - 1
# one is numbered 0.
mid = first + (last - first)//2
found = False
while(mid >= first and mid <= last):
if object == list[mid]:
print("Found at position ", mid + 1)
found = True
break
elif object > list[mid]:
first = mid + 1
How many steps on
else:
average does it take?
last = mid - 1
mid = first + (last - first)//2
if not found:
print("Not found")
Binary Search
def binary_search(list,object):
first = 0
last = len(list) - 1
# one is numbered 0.
mid = first + (last - first)//2
found = False
while(mid >= first and mid <= last):
if object == list[mid]:
print("Found at position ", mid + 1)
found = True
break
elif object > list[mid]:
first = mid + 1
How many steps on
else:
average does it take?
last = mid - 1
mid = first + (last - first)//2
if not found:
print("Not found")
O(log2 n)
Searching Huge Files
Comparing:
O(n)
to
O(log2 n)
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
11000
12000
13000
14000
15000
16000
17000
18000
19000
9.965784
10.96578
11.55075
11.96578
12.28771
12.55075
12.77314
12.96578
13.13571
13.28771
13.42522
13.55075
13.66622
13.77314
13.87267
13.96578
14.05325
14.13571
14.21371
100,000
16.60964
1,000,000
19.93157
But Is It Correct?
2
9
13 17 19 22 28 30 35 40 43 55 61 69 73 83 86 91 93 99
2
9
13 17 19 22 28 30 35 40 43 55 61 69 73 83 86 91 93 99
2
9
13 17 19 22 28 30 35 40 43 55 61 69 73 83 86 91 93 99
2
9
13 17 19 22 28 30 35 40 43 55 61 69 73 83 86 91 93 99
Search for 93.
Invariant:
Binary Search Trees
Binary Search Trees
Binary Search Trees
Binary Search Trees
How many leaves of the tree with 20 questions?
Binary Search Trees
How many leaves of the tree with 20 questions?
220 = 1,048,576
Information Theory – or
Making Every Bit Count
In English, they don’t:
F**r sc*r* *nd s*v*n ***rs *g* **r f*th*rs br**ght
f*rth *n th*s c*nt*n*nt, * n*w n*t**n, c*nc**v*d *n
L*b*rt*, *nd d*d*c*t*d t* th* pr*p*s*t**n th*t *ll m*n
*r* cr**t*d *q**l.
Making Every Bit Count
Based on the rates of correct guesses—and rigorous mathematical
analysis—Shannon determined that the information content of typical
written English was around 1.0 to 1.2 bits per letter. This means that a
good compression algorithm should be able to compress ASCII English
text—which is eight bits per letter—to about 1/8th of its original size.
Indeed, if you use a good file compressor on a .txt ebook, that’s about
what you’ll find.
Information Theory – or
Making Every Bit Count
Machine to machine communication that is hidden from you:
• Compressing text, images and video
• Sending messages along cables
• Cryptography
Information Theory – or
Making Every Bit Count
Machine to machine communication that is hidden from you:
• Compressing text, images and video
• Sending messages along cables
• Cryptography
But when people are involved, we have to think about it:
Information Theory – or
Making Every Bit Count
Machine to machine communication that is hidden from you:
• Compressing images and video
• Sending messages along cables
• Cryptography
But when people are involved, we have to think about it:
• Doctor asking patient about symptoms
• Telephone help desk
Ternary Search Trees
http://www.20q.net/
Heuristic Search
What is a
Heuristic?
What is a
Heuristic?
The word heuristic comes from
the Greek word 
(heuriskein), meaning “to
discover”, which is also the origin
of eureka, derived from
Archimedes’ reputed
exclamation, heurika (“I have
found”), uttered when he had
discovered that the volume of
water displaced in the bath equals
the volume of whatever (him) got
put in the water. This could be
used as a method for determining
the purity of gold.
What is a
Heuristic?
The word heuristic comes from
the Greek word 
(heuriskein), meaning “to
discover”, which is also the origin
of eureka, derived from
Archimedes’ reputed
exclamation, heurika (“I have
found”), uttered when he had
discovered that the volume of
water displaced in the bath equals
the volume of whatever (him) got
put in the water. This could be
used as a method for determining
the purity of gold.
A heuristic is a rule that helps
us find something.
Heuristic Search on the Web
Searches you use everyday:
The 15-Puzzle
http://www.javaonthebrain.com/java/puzz15/
Hill Climbing
Problem: You have just arrived in Washington, D.C. You’re in
your car, trying to get downtown to the Washington
Monument.
Objective (Heuristic) Functions
To guide our search, we need something to measure. Then we
can seek to maximize (or minimize).
An Objective (Heuristic) Function for Chess
c1 * material +
c2 * mobility +
c3 * king safety +
c4 * center control + ...
Computing material:
Pawn
Knight
Bishop
Rook
Queen
King
100
320
325
500
975
32767
An Objective (Heuristic )Function for Driving
An Objective (Heuristic) Function
for the 15-Puzzle
Hill Climbing – Some Problems
Let’s Try This One
From the initial state, move A to the table. Three choices for what
to do next.
A local heuristic function: Add one point for every block that is
resting on the thing it is supposed to be resting on. Subtract one
point for every block that is sitting on the wrong thing.
“Randomness”
• Computers behave in a predicatble way.
• But some processes are naturally random.
Musikalisches Würfelspiel
A musical dice game
Musikalisches Würfelspiel
A musical dice game
Musikalisches Würfelspiel
An algorithm to generate a piece of music:
def musical_dice(table):
for i in range(len(table)):
table[i] = roll_dice()
Musikalisches Würfelspiel
An algorithm to generate a piece of music:
import random
def musical_dice(table):
for i in range(len(table)):
table[i] = roll_dice()
def roll_dice():
x = random.randint(1,6)
y = random.randint(1,6)
return(x + y)
Recall Monte Carlo Methods
Applications of Monte Carlo Methods
Very useful for complex problems that we don’t know
how to model exactly:
•
•
•
•
Weather
Oil exploration
Engineering design, e.g., a new network
Financial systems
SIMD Parallelism
•
•
•
•
Weather
Oil exploration
Engineering design, e.g., a new network
Financial systems
A Monte Carlo Example
http://www.youtube.com/watch?v=-fCVxTTAtFQ Intro, then skip to 6:36
Real Programs
Lines of Code
OpenOffice
9 million
Android OS
http://www.gubatron.com/blog/2010/05/23/how-manylines-of-code-does-it-take-to-create-the-android-os/
GNU/Linux
30 million
Windows Vista
50 million
Mac OS X 10.4
86 million
Lucent 5ESS Switch 100 million
Developers
2000
5000
Coding and Debugging
“Debugging is twice as hard as writing the code in the first
place. Therefore, if you write the code as cleverly as possible,
you are, by definition, not smart enough to debug it.”
Brian Kernighan
20 Famous Software Disasters
http://www.devtopics.com/20-famous-software-disasters/
3n + 1
until n is 1:
if n is even: n = n/2
if n is odd: n = 3n+1
What happens on:
n=7
3n + 1
until n is 1:
if n is even: n = n/2
if n is odd: n = 3n+1
def threen(value):
# compute 3n+1
while value != 1:
if value % 2 == 0:
value = value//2
else:
value = 3 * value + 1
print(int(value))
http://math.carleton.ca/%7Eamingare/mathzone/3n+1.html
3n + 1
until n is 1:
if n is even: n = n/2
if n is odd: n = 3n+1
def threen(value):
# compute 3n+1
while value != 1:
if value % 2 == 0:
value = value//2
else:
value = 3 * value + 1
print(int(value))
Collatz Conjecture: This program always halts.
3n + 1
until n is 1:
if n is even: n = n/2
if n is odd: n = 3n+1
def threen(value):
# compute 3n+1
while value != 1:
if value % 2 == 0:
value = value//2
else:
value = 3 * value + 1
print(int(value))
But what happens on:
threen(-7)?