Professional Documents
Culture Documents
Lecture 1
The Goal is to
UNDERSTAND
Algorithms
Fundamentally to their core
Representation:
Understand the relationship between
different representations of the same
information or idea
1
2
3
Rudich www.discretemath.com
Different Representations
of Algorithms
Code
Running example
DFA and Turing Machines
Higher level abstract view
Code
Representation of an Algorithm
class InsertionSortAlgorithm extends SortAlgorithm {
void sort(int a[]) throws Exception {
for (int i = 1; i < a.length; i++) {
int j = i;
int B = a[i];
while ((j > 0) && (a[j-1] > B)) {
a[j] = a[j-1];
j--; }
a[j] = B;
}}
Code
Representation of an Algorithm
Pros:
Cons:
Runs on computers
Precise and succinct
Perception that being
able to code is the
only thing needed to
get a job. (false)
I am not a computer
I need a higher level of
intuition.
Prone to bugs
Language dependent
Running Example
Representation of an Algorithm
Try out a problem or
solution on small examples.
Running Example
Representation of an Algorithm
52
88
14
31
25
98
30
88
23
62
14
98
79
Running Example
Representation of an Algorithm
14,23,25,30,31,52,62,79,88,98
Running Example
Representation of an Algorithm
Pros:
Concrete
Dynamic
Visual
Cons:
Relies on you to find the
pattern.
Does not explain why it
works.
Demonstrates for only
one of many inputs.
10
Cons:
Not good for designing
algorithms
12
i-1
13
Levels of Abstraction
It is hard to think of love in terms of the firing of
neurons.
vs
vs
14
Cons:
Mathematical mumbo
jumbo
Too abstract
Students resist it
Value Simplicity
Abstract away the inessential features of a problem
=
Goal: Understand and think about complex
algorithm in simple ways.
Dont tune out. There are deep ideas within
the simplicity.
16
Levels of Abstraction
Given a concrete algorithm, eg. MergeSort
and a concrete input,
eg. <4,2,6,1,7>
trace it and prove that it works.
Given a concrete algorithm, eg. MergeSort
and an abstract input,
eg. <x1, xn>
trace it and prove that it works.
Given a Meta Algorithm, eg. Iterative Algs
and an abstract input,
eg. <x1, xn>
trace it and prove that it works.
17
Levels of Abstraction
Given a new computational problem on exam,
design a new concrete algorithm that solves it
following the Meta Algorithm steps.
If you do not get all the details of the new
algorithm correct, it wont be a disaster.
If you do not get the steps of the Meta
Algorithm correct, it will be a disaster.
There are only a few Meta Algorithms
Learn them!
18
Levels of Abstraction
Meta Algorithms
Iterative Algorithms
Recursive Algorithms
Greedy Algorithms
Recursive Back Tracking
Dynamic Programming.
NP-Completeness
19
Levels of Abstraction
Meta Math
Existential and Universal Quantifiers
Asymptotic Growth
Adding Made Easy Approximations
Recurrence Relations
20
Value Correctness
Dont tack on a formal proof of correctness
after coding to make the professor happy.
It need not be mathematical mumbo jumbo.
Goal: To think about algorithms in such way
that their correctness is transparent.
21
Explaining an Algorithm
Goal: To provide a number of
different notations, analogies, and
abstractions within which to
develop, think about, and
describe algorithms.
22
23
Iterative Algorithms
loop (until done)
take step
end loop
25
26
Problem Specification
Pre condition: location of home and school
Post condition: traveled from home to school
27
Algorithm
What is an Algorithm?
28
Algorithm
The algorithm defines the computation route.
29
Algorithm
Is this reasonable?
30
Complexity
There are an infinite number of input instances
Algorithm must work for each.
31
Complexity
Difficult to predict where computation might be
in the middle of the computation.
32
Location of Computation
The current State of computation is
determined by values of all variables.
33
Location of Computation
Suppose the computation ends up here?
34
Dont Panic
Where ever the computation might be,
take best step towards school.
35
General Principle
Do not worry about the entire computation.
Take one step at a time!
36
Defining Algorithm
Wherever the computation might be,
take best step towards school.
37
Take a step
What is required of this step?
38
A Measure of Progress
75 km
to school
79 km
to school
39
Defining Algorithm
Is this sufficient to define a working algorithm?
79 km
75 km
40
Working Algorithm
Computation steadily approaches goal
79 km
75 km
to school
to school
41
Defining Algorithm
Extra requirements
79 km
75 km
79 km
km
0 km
78.999 km
42
Define a Step
Wherever the computation might be,
take best step towards school.
43
Computation Stuck
Some location too confusing for algorithm
Algorithm too lazy to define step for every
location.
44
Safe Locations
Algorithm specifies from which locations
it knows how to step.
45
Loop Invariant
The computation is presently in a safe location.
Maybe true and maybe not.
If not something has gone wrong.
46
Defining Algorithm
From every safe location,
define one step towards school.
47
Take a step
What is required of this step?
48
49
50
52
53
S (0 )
iS ( i)
i S ( i ) S ( i 1 )
54
Exit
Termination:
With sufficient progress,
the exit condition will be met.
When we exit, we know
exit condition is true
loop invariant is true
from these we must establish
the post conditions.
0 km
Exit
Exit
Exit
55
Lets Recap
56
Define Loop
Invariants
Define Measure of
Progress
79 km
to school
Define Step
Exit
Make Progress
Initial Conditions
Ending
Exit
79 km
75 km
km
0 km
Exit
Exit
57
Consider an Algorithm
Exit
Exit
79 km
75 km
km
0 km
Exit
Exit
58
Exit
59
60
79 km
75 km
Exit
79 km
75 km
61
Computation Terminates
km
0 km
Exit
0 km
79 km
75 km
0 km
Exit
62
Computation Terminates
Exit
Exit
63
Consider an Algorithm
Exit
Exit
79 km
75 km
km
0 km
Exit
Exit
This is sufficient!
64
This completes
One Higher Level Abstract View
of Iterative Algorithms
65
Assertions
66
Paradigm Shift
A Sequence of Actions
Max( a,b,c )
m=a
if( b>m )
m=b
endif
if( c>m )
m=c
endif
return(m)
68
A Sequence of Actions
Max( a,b,c )
preCond: Input has 3 numbers.
m=a
if( b>m )
m=b
endif
if( c>m )
m=c
endif
return(m)
postCond:
return max in {a,b,c}
69
A Sequence of Actions
Max( a,b,c )
preCond: Input has 3 numbers.
m=a
if( b>m )
m=b
endif
if( c>m )
m=c
endif
return(m)
postCond:
return max in {a,b,c}
70
A Sequence of Actions
Max( a,b,c )
preCond: Input has 3 numbers.
m=a
if( b>m )
m=b
endif
assert: m is max in {a,b}
if( c>m )
m=c
endif
Tell me!
return(m)
postCond:
return max in {a,b,c}
71
Value Correctness
Dont tack on a formal proof of correctness
after coding to make the professor happy.
It need not be mathematical mumbo jumbo.
Goal: To think about algorithms in such way
that their correctness is transparent.
72
A Sequencevs of Actions
A Sequence of Assertions
Max( a,b,c )
preCond: Input has 3 numbers.
m=a
assert: m is max in {a}
if( b>m )
m=b
endif
assert: m is max in {a,b}
if( c>m )
m=c
endif
assert: m is max in {a,b,c}
return(m)
postCond:
return max in {a,b,c}
It is help to have
different ways of
looking at it.
73
Purpose of Assertions
Useful for
thinking about algorithms
developing
describing
proving correctness
74
Definition of Assertions
An assertion is a statement
about the current state of the data
structure that is either true or false.
eg. the amount in your bank account
is not negative.
75
Definition of Assertions
It is made at some particular point during
the execution of an algorithm.
It should be true independent of
the path followed through the code
and independent of the input.
If it is false, then something has gone
wrong in the logic of the algorithm.
76
Definition of Assertions
An assertion is not a task for the
algorithm to perform.
It is only a comment that is added
for the benefit of the reader.
77
Don't Be Frightened
An assertion need not consist of formal
mathematical mumble jumble
Use an informal description
and a picture
78
Specifying a Computational
Problem
Max( A )
preCond:
Input is array A[1,n]
of n values.
i=1
m = A[1]
loop
exit when (i=n)
i=i+1
m = max(m,A[i])
endloop
return(m)
postCond:
m is max in A[1,n]
Preconditions:
Any assumptions that must be
true about the input instance.
Postconditions:
The statement of what must
be true when the
algorithm/program returns.
79
Definition of Correctness
<PreCond> & <code> <PostCond>
If the input meets the preconditions,
then the output must meet the
postconditions.
If the input does not meet the preconditions,
then nothing is required.
80
A Sequence of Assertions
<assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
Definition of Correctness
<assertion0>
any <conditions>
code
<assertionr>
81
A Sequence of Assertions
<assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
Definition of Correctness
<assertion0>
any <conditions>
code
<assertionr>
A Sequence of Assertions
<assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
Step 1
<assertion0>
<condition1>
code<1,true>
<assertion1>
<assertion0>
<condition1>
code<1,false>
<assertion1>
83
A Sequence of Assertions
<assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
Step 2
<assertion1>
<condition2>
code<2,true>
<assertion2>
<assertion1>
<condition2>
code<2,false>
<assertion2>
84
A Sequence of Assertions
<assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
Step r
<assertionr-1>
<conditionr>
code<r,true>
<assertionr>
<assertionr-1>
<conditionr>
code<r,false>
<assertionr>
85
A Sequence of Assertions
Max( a,b,c )
preCond:
Input has 3 numbers.
m=a
assert: m is max in {a}
if( b>m )
m=b
endif
assert: m is max in {a,b}
if( c>m )
m=c
endif
assert: m is max in {a,b,c}
return(m)
postCond:
return max in {a,b,c}
Example
<preCond>
code
<assertion0>
86
A Sequence of Assertions
Max( a,b,c )
preCond:
Input has 3 numbers.
m=a
assert: m is max in {a}
if( b>m )
m=b
endif
assert: m is max in {a,b}
if( c>m )
m=c
endif
assert: m is max in {a,b,c}
return(m)
postCond:
return max in {a,b,c}
Example
<assertion0>
<condition1>
code<1,true>
<assertion1>
<assertion0>
<condition1>
code<1,false>
<assertion1>
87
A Sequence of Assertions
Max( a,b,c )
preCond:
Input has 3 numbers.
m=a
assert: m is max in {a}
if( b>m )
m=b
endif
assert: m is max in {a,b}
if( c>m )
m=c
endif
assert: m is max in {a,b,c}
return(m)
postCond:
return max in {a,b,c}
Example
<assertion1>
<condition2>
code<2,true>
<assertion2>
<assertion1>
<condition2>
code<2,false>
<assertion2>
88
A Sequence of Assertions
Max( a,b,c )
preCond:
Input has 3 numbers.
m=a
assert: m is max in {a}
if( b>m )
m=b
endif
assert: m is max in {a,b}
if( c>m )
m=c
endif
assert: m is max in {a,b,c}
return(m)
postCond:
return max in {a,b,c}
Example
<assertion2>
code
<postCond>
89
Loop Invariants
Any assumptions that
must be true about the
state of computation
every time it is at the
top of the loop.
Max( A )
preCond:
Input is array A[1,n]
of n values.
i=1
m = A[1]
loop
exit when (i=n)
i=i+1
m = max(m,A[i])
endloop
return(m)
postCond:
m is max in A[1,n]
90
Loop Invariants
Any assumptions that
must be true about the
state of computation
every time it is at the
top of the loop.
Max( A )
preCond:
Input is array A[1,n]
of n values.
i=1
m = A[1]
loop
loop-invariant:
m is max in A[1,i]
exit when (i=n)
i=i+1
m = max(m,A[i])
endloop
return(m)
postCond:
m is max in A[1,n] 91
Loop Invariants
Not just to please the prof.
Not just to prove correctness.
Use them to think about your
algorithm!
Before you start coding.
What do you want to be
true in the middle of your
computation?
You need to know.
Let your reader know.
Max( A )
preCond:
Input is array A[1,n]
of n values.
i=1
m = A[1]
loop
loop-invariant:
m is max in A[1,i]
exit when (i=n)
i=i+1
m = max(m,A[i])
endloop
return(m)
postCond:
m is max in A[1,n] 92
Iterative Algorithms
Loop Invariants
<preCond>
codeA
loop
<loop-invariant>
exit when <exit Cond>
codeB
endloop
codeC
<postCond>
93
Iterative Algorithms
Loop Invariants
<preCond>
codeA
loop
<loop-invariant>
exit when <exit Cond> Definition of
codeB
<preCond>
endloop
<any conditions>
codeC
code
<postCond>
Correctness
<postCond>
94
Max( A )
preCond:
Input is array A[1,n]
of n values.
i=1
m = A[1]
loop
loop-invariant:
m is max in A[1,i]
exit when (i=n)
i=i+1
m = max(m,A[i])
endloop
return(m)
postCond:
m is max in A[1,n]
Iterative Algorithms
Loop Invariants
Example
Step 0
<preCond>
codeA
<loop-inv>
95
Max( A )
preCond:
Input is array A[1,n]
of n values.
i=1
m = A[1]
loop
loop-invariant:
m is max in A[1,i]
exit when (i=n)
i=i+1
m = max(m,A[i])
endloop
return(m)
postCond:
m is max in A[1,n]
Iterative Algorithms
Loop Invariants
Example
<loop-inv>
96
Max( A )
preCond:
Input is array A[1,n]
of n values.
i=1
m = A[1]
loop
loop-invariant:
m is max in A[1,i]
exit when (i=n)
i=i+1
m = max(m,A[i])
endloop
return(m)
postCond:
m is max in A[1,n]
Iterative Algorithms
Loop Invariants
Example
Last Step
<loop-invariant>
<postCond>
<exit Cond>
codeC
97
Partial Correctness
<loop-invariant>
<loop-invariant>
<exit Cond>
codeB
<loop-invariant>
98
Algorithm Termination
You need to define some
Measure of progress
to prove that your algorithm
eventually terminates.
99
Simple Example
Insertion Sort Algorithm
100
Repetition Is An Opportunity
Rudich www.discretemath.com
101
Code
Representation of an Algorithm
class InsertionSortAlgorithm extends SortAlgorithm {
void sort(int a[]) throws Exception {
for (int i = 1; i < a.length; i++) {
int j = i;
int B = a[i];
while ((j > 0) && (a[j-1] > B)) {
a[j] = a[j-1];
j--; }
a[j] = B;
}}
102
9 km
103
Designing an Algorithm
Define Problem
Define Loop
Invariants
Define Measure of
Progress
79 km
to school
Define Step
Exit
Make Progress
Initial Conditions
Ending
Exit
79 km
75 km
km
0 km
Exit
Exit
104
Problem Specification
Precondition: The input is a list of n values
with the same value possibly repeated.
Post condition: The output is a list consisting
of the same n values in non-decreasing order.
88 52
14
31
25 98
30
23
62
79
14,23,25,30,31,52,62,79,88,98
105
Location of Computation
State of computation determined
by values of all variables.
106
Location of Computation
State of computation determined
by values of all variables.
88 52 14
31 25 62 30
23
98
79
14,23,25,30,31,52,62,79,88,98
88 14<52
31 25<62 30
23
79<98
107
Computation Stuck
Location too confusing for algorithm
Algorithm too lazy to define step for
every location.
88 52 14
31 25 62 30
23
98
79
14,23,25,30,31,52,62,79,88,98
88 14<52
31 25<62 30
23
79<98
108
Safe Locations
Algorithm specifies from which locations
it knows how to step.
88 52 14
31 25 62 30
23
98
79
14,23,25,30,31,52,62,79,88,98
109
88 52 14
31 25 62 30
23
98
79
14,23,25,30,31,52,62,79,88,98
23,31,52,88
30 14
25 62
98
79
110
14,52,62,79
30 23
25 88
98
31
23,31,52,88
30 14
25 62
98
79
111
30 14
25 62
98
79
6 elements
to school
112
Define Step
23,31,52,88
30 14
25 62
98
79
113
Define Step
Select arbitrary element from side.
Insert it where it belongs.
23,31,52,88
30 14
25 62
98
79
23,31,52,62,88
30 14
25
98
79
114
23,31,52,88
Exit
79 km
75 km
30 14
25 62
98
79
6 elements
23,31,52,62,88
to school
Sorted sub-list
30 14
25
98
79
5 elements
to school
115
Beginning &
Ending
88 52 14
31 25 6 30
23
98
2
79
km
88 52 14
31 25 6 30
23
98
2
79
0 km
Exit
Exit
n elements
to school
0 elements
14,23,25,30,31,52,62,79,88,98
to school
14,23,25,30,31,52,62,79,88,9
116
Running Time
Inserting an element into a list of size i
takes (i) time.
Total = 1+2+3++n
=?
23,31,52,88
30 14
25 62
98
79
23,31,52,62,88
30 14
25
98
79
117
...
n-1 +
2........n
118
n-1 +
...
n-1 +
n-2 +
...
n ....... 2 1
2........n
119
n-1 +
...
n-1 +
n-2 +
...
n
n
2S dots
= n(n+1) dots in the
grid
n
n
n
S=
n (n+1)
2
n
n+1 n+1 n+1
n+1 n+1
120
121
122
Running Time
Inserting an element into a list of size i
takes (i) time.
Total = 1+2+3++n
= (n2)
23,31,52,88
30 14
25 62
98
79
23,31,52,62,88
30 14
25
98
79
123
Define Loop
Invariants
Define Measure of
Progress
79 km
to school
Define Step
Exit
Make Progress
Initial Conditions
Ending
Exit
79 km
75 km
km
0 km
Exit
Exit
124
0
i-1
Go around once
i
125
<loop-invariant>
<preCond>
codeA
loop
<loop-invariant>
exit when <exit Cond>
codeB
endloop
codeC
<postCond>
126
i-1
<loop-invariant>
<loop-invariant>
<exit Cond>
codeB
<preCond>
codeA
i
loop
<loop-invariant>
exit when <exit Cond>
codeB
endloop
codeC
<postCond>
127
<loop-invariant>
<postCond>
<exit Cond>
codeC
<preCond>
1
T+
codeA
loop
<loop-invariant>
exit when <exit Cond>
codeB
endloop
codeC
<postCond>
128
Insertion Sort
as a Relay Race
88 52 14
31 25 62 30
23
98
79
129
88 52 14
31 25 62 30
23
98
79
130
88 52 14
31 25 62 30
23
98
79
131
i-1
31
88 52 14
25 62 30
23
98
79
132
31
88 52 14
25 62 30
23
98
79
133
i-1
31,88
52 14
25 62 30
23
98
79
134
31,88
52 14
25 62 30
23
98
79
135
i-1
25,31,88
52 14
62 30
23
98
79
136
25,31,88
52 14
62 30
23
98
79
137
i-1
25,31,52,88
14
62 30
23
98
79
138
14,23,25,30,31,52,62,79,88,98
139
1
T+
14,23,25,30,31,52,62,79,88,98
140
Ok
I know you knew Insertion Sort
But hopefully you are beginning to appreciate
Loop Invariants
for describing algorithms
141
<
88 52
62
98
79
143
Designing an Algorithm
144
A Starry Night
How did Van Gogh come up with his famous
painting, A Starry Night?
There's no easy answer.
Designing algorithms is an art form.
145
146
147
148
Exemplification:
Try solving the problem
on small input examples.
Rudich www.discretemath.com
149
150
151
75 km
to school
to school
152
Flow Smoothly
153
154
155
156
157
No Assumptions:
Suppose a Martian jumped
into the top of the loop.
All you would know is that <loop-invariant> was true.
It alone must provide enough information
so that, after going around the loop, you can establish
that the loop invariant is again true.
158
code
A precondition
A postcondition
A statement that is always true
Eg: 1+1=2
The steps taken by the algorithm
159
Find Errors
161
Find Errors
If I = -5, s = 1-5 j = 0
162
Find Errors
Variables need to
be documented.
Fine: Purpose outlined
in LI
163
Find Errors
of current state.
Not actions!
Not algorithms!
164
Find Errors
165
Find Errors
Let s and i be
values of s and i
when at the top of the loop.
Let s and i be
values after going
around again.
166
Find Errors
LI
s = j=1i j.
Code s = s+i
i = i+1.
Together:
s = (j=1i j) + i.
i is added in twice.
i should be added.
167
Find Errors
i = 1 j=1i j = 1 = s.
168
Find Errors
Exit
Better to say:
loop
loop-invariant: _____
exit when i > I
169
Find Errors
Exit
LI s = j=1i j.
Exit i > I
Together:
s = j=1I+1 j.
Post Cond.
170
Find Errors
Test:
on typical value
i = 3 s=1+2+3.
on special values
i = 1 s=1.
i = 0 s=1.
171
Find Errors
PreCond: i=0
PostCond: i=131
Loop Invariant:
i is increasing
Step: i = i+2
Exit Cond: i=131
172
Find Errors
Loop Invariant:
??
Step: i = i+2
Exit Cond: i=131
Establishing Loop Invariant
PreCond: i=0
PostCond: i=131
<preCond>
codeA
<loop-invariant>
Find Errors
Loop Invariant:
??
Step: i = i+2
Exit Cond: i=131
Maintaining Loop Invariant
PreCond: i=0
PostCond: i=131
Exit
<loop-invariant>
<exit Cond>
codeB
<loop-invariant>
Find Errors
PreCond: i=0
PostCond: i=131
Loop Invariant:
??
Step: i = i+2
Exit Cond: i=131
<loop-invariant>
<exit Cond>
codeC
<postCond>
NEVER DO THIS!
175
Find Errors
Loop Invariant:
??
Step: i = i+2
Exit Cond: i=131
PreCond: i=0
PostCond: i=131
79 km
Exit
79 km
75 km
km
to school
Trivially true
Let measure be the value of i
(or 131-i)
176
Find Errors
PreCond: i=0
PostCond: i=131
0 km
Exit
Loop Invariant:
??
Step: i = i+2
Exit Cond: i=131
177
Find Errors
PreCond: i=0
PostCond: i=131
Loop Invariant:
??
Step: i = i+2
Exit Cond: i=131
0 km
Exit
178
Find Errors
PreCond: i=0
PostCond: i=131
Loop Invariant:
?? i is odd
Step: i = i+2
Exit Cond: i131
179
Find Errors
Loop Invariant:
i is odd
Step: i = i+2
Exit Cond: i131
Establishing Loop Invariant
PreCond: i=0
PostCond: i=131
<preCond>
codeA
i=0
?
i = i+1
i is odd
<loop-invariant>
180
Find Errors
PreCond: i=0
PostCond: i=131
Loop Invariant:
i is odd & i 132
Step: i = i+2
Exit Cond: i131
<loop-invariant>
<exit Cond>
codeC
i is odd
i131
& i 132
i=131
<postCond>
181
Find Errors
Loop Invariant:
i is odd & i 132
Step: i = i+2
Exit Cond: i131
Maintaining Loop Invariant
PreCond: i=0
PostCond: i=131
Exit
<loop-invariant>
<exit Cond>
codeB
Define Loop
Invariants
Define Measure of
Progress
79 km
to school
Define Step
Exit
Make Progress
Initial Conditions
Ending
Exit
79 km
75 km
km
0 km
Exit
Exit
183
184
Extra
186
Extra
Extra
187
Exit
79 km
75 km
Extra
Extra
i to i+1
188
Solution
Exit
189
Insertion Sort
The input consists of an array of integers
52,23,88,31,25,30,98,62,14,79
23,31,52,88
Solution
Insertion Sort
The input consists of an array of integers
52,23,88,31,25,30,98,62,14,79
23,31,52,88
23,25,31,52,88
of current state.
Not actions!
Not algorithms!
192
193
194
Solution
A student midterm answer:
We have a solution for the
prefix consisting of elements [1..i].
(plus some additional information)
196
197
200
Exit
79 km
75 km
i to i+1
Exit
Done when
output n objects.
201
Input:
Selection Sort
52,23,88,31,25,30,98,62,14,79
Exit
Done when
output n objects.
202
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
PostConditions
Find key in list (if there).
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
204
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
205
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
206
Define Step
Make Progress
Maintain Loop Invariant
key 25
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
207
Define Step
Cut sub-list in half.
Determine which half key would be in.
Keep that half.
key 25
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
208
Define Step
Cut sub-list in half.
Determine which half the key would be in.
Keep that half.
key 25
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key mid,
then key is in
left half.
209
Define Step
It is faster not to check if the middle
element is the key.
Simply continue.
key 43
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key mid,
then key is in
left half.
210
Exit
Make Progress
79 km
75 km
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
79 km
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
75 km
211
Initial Conditions
km
key 25
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
n km
212
Ending Algorithm
Exit
key 25
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
0 km
Exit
If the key is
contained in the
original list,
then the key is at
this location.
213
key 24
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If the key is
contained in the
original list,
then the key is at
this location.
Running Time
The sub-list is of size n, n/2, n/4, n/8,,1
Each step (1) time.
Total = (log n)
key 25
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key mid,
then key is in
left half.
215
Code
216
Define Loop
Invariants
Define Measure of
Progress
79 km
to school
Define Step
Exit
Make Progress
Initial Conditions
Ending
Exit
79 km
75 km
km
0 km
Exit
Exit
217
Study:
Many experienced programmers were asked
to code up binary search.
80% got it wrong
Good thing is was not for a
nuclear power plant.
218
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
219
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
j
Does not matter which,
but you need to be consistent.
220
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
i j
i j
mid
or mid
?
2
2
221
mid
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
i j
2
mid
222
Common Bugs
Cut sub-list in half.
Determine which half the key would be in.
Keep that half.
key 25
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key mid,
then key is in
left half.
223
Common Bugs
If the middle element is the key,
it can be skipped over.
Exit
key 43
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key mid,
then key is in
left half.
224
Common Bugs
Fix the bug.
key 43
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key mid,
then key is in
left half.
225
Common Bugs
Second fix,
by making the left half slightly bigger.
New bug?
key 43
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key mid,
then key is in
left half.
226
Exit
Common Bugs
79 km
75 km
Second fix,
by making the left half slightly bigger.
New bug?
key 43
3
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
No progress is made.
Loop for ever!
227
i j
1 mid
2
2 if key L(mid)
3
i = mid
else
j = mid -1
i j
OR
?
2
How many
possible
algorithms?
OR > ?
How many
correct
algorithms?
i = mid + 1
OR else
j = mid
Probability of
success by
guessing?
228
Moral
Use the loop invariant method to think
about algorithms.
Be careful with your definitions.
Be sure that the loop invariant is always
maintained.
Be sure progress is always made.
229
Loop Invariants
for
Iterative Algorithms
A second
Binary Search like
example
230
<preCond>
Key
25
A binary search tree.
38
25
51
17
31
21
28
42
35
40
63
49
55
71
<postCond>
Find key in BST (if there).
231
25
17
51
31
21
28
42
35
40
63
49
55
71
232
key 17
25
51
17
31
21
28
42
35
40
63
49
55
71
233
Define Step
Cut sub-tree in half.
Determine which half the key would be in.
Keep that half.
38
key 17
25
51
17
31
21
28
If key = root,
then key is
found
42
35
40
63
49
55
71
Define Loop
Invariants
Define Measure of
Progress
79 km
to school
Define Step
Exit
Make Progress
Initial Conditions
Ending
Exit
79 km
75 km
km
0 km
Exit
Exit
235
Loop Invariants
for
Iterative Algorithms
A third
Binary Search like
example
236
Card Trick
A volunteer, please.
237
Pick a Card
Done
238
Loop Invariant:
The selected card is one of
these.
239
Which column?
left
240
Loop Invariant:
The selected card is one of
these.
241
242
243
244
Which column?
right
245
Loop Invariant:
The selected card is one of
these.
246
247
248
Which column?
left
249
Loop Invariant:
The selected card is one of
these.
250
251
Wow!
252
Input:
Output:
p=52
88 52
14
31
25 98
30
23
62
79
14
31 30 23
25
88
52
62
98
79
253
25 98 30 14 62 79 88
or
p=52
31 23 71 25 98 30
p
62 79 88
p
254
25 98 30 14 62 79 88
4 elements
not
looked at
255
Define Step
Make Progress
Maintain Loop Invariant
p=52
31 23
p
25 98 30 14 62 79 88
p
256
Define Step
Make Progress
Maintain Loop Invariant
p=52
31 23
25 98 30 14 62 79 88
p=52
31 23 14 25 98 30
p
62 79 88
p
257
Define Step
Four cases
p=52
31 23
p=52
25 98 30 14 62 79 88
31 23 14 25 98 30
62 79 88
p=52
31 23 14 25 98 30
62 79 88
31 23 14 25 98 30
62 79 88
p=52
31 23
25 98 30 71 62 79 88
31 23 71 25 98 30
31 23
25 98 30 71 62 79 88
31 23
62 79 88
25 98 30 71 62 79 88
258
Beginning &
Ending
p=52
88 52 14
31
25 62 30
23
98
79
km
0 km
Exit
Exit
p=52
88 25 31 98 62 14 30 79 23
n-1 elements
not
looked at
p=52
23 30 25 31 14
62 98 79 88
0 elements
not
looked at
25
23 31 14
30
62
52
98
79
88
259
Running Time
Each iteration takes (1) time.
Total = (n)
p=52
31 23
25 98 30 14 62 79 88
p=52
31 23 14 25 98 30
p
62 79 88
p
260
Define Loop
Invariants
Define Measure of
Progress
79 km
to school
Define Step
Exit
Make Progress
Initial Conditions
Ending
Exit
79 km
75 km
km
0 km
Exit
Exit
261
31 88 25 52 98 62 14 30 79 23
p=52
88 25 31 98 62 14 30 79 23
23 88 25 31 98 62 14 30 79
Tracing an
Example
23
25 31 98 62 14 30 79 88
23
25 31 98 62 14 30 79 88
23 30 25 31 98 62 14
79 88
23 30 25 31 98 62 14
79 88
23 30 25 31 98 62 14
79 88
23 30 25 31
62 14 98 79 88
23 30 25 31 14 62
23 30 25 31 14
98 79 88
62 98 79 88
262
263
GCD(a,b)
= <64,44>
Input: <a,b>
Output: GCD(a,b) = 4
<a,b> <a-b,b>
GCD(a,b) = GCD(a-b,b)
GCD(64,44) = GCD(20,44) = 4
Maintain values <x,y> such that
GCD(a,b) = GCD(x,y)
<x,y> <x-y,y>
GCD(a,b) = GCD(x,y)
= GCD(x-y,y) = GCD(x,y)
Exit
79 km
75 km
smaller
264
GCD(a,b)
Input: <a,b> = <64,44>
<x,y> = <64,44>
= <20,44>
GCD(a,b) = GCD(x,y)
= <44,20>
= <24,20>
= < 4,20>
= <20, 4>
= <16, 4>
= <12, 4>
Running time?
= < 8, 4>
= < 4, 4>
= < 0, 4>
GCD(a,b) = GCD(x,y) = 4
265
GCD(a,b)
Input: <a,b> = <9999999999999,2>
<x,y> = <9999999999999,2>
= <9999999999997,2>
= <9999999999995,2>
= <9999999999993,2>
= <9999999999991,2>
Time = O(a)
Poly Time?
266
Definition of Time
# of seconds (machine dependent).
# lines of code executed.
# of times a specific operation is performed
(e.g., addition).
Which?
268
Definition of Time
# of seconds (machine dependent).
# lines of code executed.
# of times a specific operation is performed
(e.g., addition).
These are all reasonable definitions of time,
because they are within a constant factor of
each other.
269
270
83920
Size of paper
# of bits
# of digits
Value
- n = 2 in2
- n = 17 bits
- n = 5 digits
- n = 83920
271
83920
Size of paper
# of bits
# of digits
Value
- n = 2 in2
- n = 17 bits
- n = 5 digits
- n = 83920
Intuitive
272
83920
Size of paper
# of bits
# of digits
Value
- n = 2 in2
- n = 17 bits
- n = 5 digits
- n = 83920
Intuitive
Formal
273
83920
Size of paper
# of bits
# of digits
Value
- n = 2 in2
- n = 17 bits
- n = 5 digits
- n = 83920
Intuitive
Formal
Reasonable
# of bits =
3.32 * # of digits
274
83920
Size of paper
# of bits
# of digits
Value
- n = 2 in2
- n = 17 bits
- n = 5 digits
- n = 83920
Intuitive
Formal
Reasonable
Unreasonable
# of bits = log2(Value)
Value = 2# of bits
275
GCD(a,b)
Input: <a,b> = <9999999999999,2>
<x,y> = <9999999999999,2>
= <9999999999997,2>
= <9999999999995,2>
= <9999999999993,2>
= <9999999999991,2>
Time = O(a) = 2O(n)
Size = number of digits n = O(log(a))
276
GCD(a,b)
Speedup
<x,y> <x-y,y>
<x-2y,y>
<x-3y,y>
<x-4y,y>
<x-iy,y>
<remainder with x/y,y>
= <x mod y,y>
<y,x mod y> smaller than y
277
GCD(a,b)
Input: <a,b> = <44,64>
<x,y> = <44,64>
GCD(a,b) = GCD(x,y)
= <64,44>
<x,y> <y,x mod y>
= <44,20>
= <20, 4>
= < 4, 0>
GCD(a,b) = GCD(x,y) = 4
278
GCD(a,b)
279
GCD(a,b)
280
281
[A-Z]
282
Psychology study:
Humans can only think about
5 things at once.
Is the first letter
of the name
on the first exam
in
[A-E]
[F-K]
[L-O]
[P-T]
[U-Z]
283
Bucket Sort
Put exams one at a time
in the correct bucket.
284
[A-E]
[F-K]
[L-O]
[P-T]
[U-Z]
285
[]
sorted
[A]
[B]
[C]
[D]
Bucket Sort
Put exams one at a time
in the correct bucket.
[E]
286
287
[A]
[B]
[C]
[D]
[E]
[F-K]
[L-O]
[P-T]
[U-Z]
288
[AU-AZ]
[B]
sorted
[AA-AE]
[E]
[F-K]
[U-Z]
289
[AA-AE]
[]
[AU-AZ]
[B]
sorted
[E]
[F-K]
[AA-AE]
[U-Z]
290
[AF-AK]
[]
[AU-AZ]
[B]
sorted
[E]
[F-K]
[AA-AE]
When sufficiently small
sort by hand
[U-Z]
sorted
291
[AU-AZ]
[B]
sorted
[AF-AK]
[E]
[F-K]
[U-Z]
292
[AF-AK]
[AA-AE]
[AU-AZ]
[B]
sorted
[E]
[F-K]
[U-Z]
[AF-AK]
sorted
293
[AU-AZ]
[B]
sorted
[AL-AO]
[E]
[F-K]
[U-Z]
294
[AU-AZ]
[B]
sorted
[AL-AO]
[E]
[F-K]
[U-Z]
[AL-AO]
[AP-AT]
[AU-AZ]
sorted
sorted
sorted
295
[E]
[F-K]
sorted
[B]
[U-Z]
296
[E]
[F-K]
sorted
[B]
[U-Z]
297
[A]
sorted
[E]
[F-K]
[U-Z]
298
[BU-BZ]
[C]
sorted
[BA-BE]
[E]
[F-K]
[U-Z]
299
[ZU-ZZ]
sorted
[ZU-ZZ]
sorted
300
301
RadixSort
Input: A of stack of N punch cards.
Each card contains d digits.
Each digit between 0&(k-1)
Output: Sort the cards.
Bucket Sort Machine:
Select one digit
Separates cards into k piles.
wrt selected digit.
344
125
333
134
224
334
143
225
325
243
125
224
225
325
333
134
334
344
143
243
Stable sort: If two cards are the same for that digit,
their order does not change.
302
RadixSort
344
125
333
134
224
334
143
225
325
243
125
134
143
224
225
243
344
333
334
325
125
224
Sort wrt which 225
digit Second? 325
134
The next most 333
334
significant.
143
243
344
303
RadixSort
344
125
333
134
224
334
143
225
325
243
333
143
243
344
134
224
334
125
225
325
224
125
Sort wrt which 225
digit Second? 325
333
The next least 134
334
significant.
143
243
344
304
RadixSort
344
125
333
134
224
334
143
225
325
243
333
143
243
344
134
224
334
125
225
325
2 24
1 25
Sort wrt which 2 25
digit Second? 3 25
3 33
The next least 1 34
3 34
significant.
1 43
2 43
3 44
Is sorted wrt first i digits.
305
RadixSort
2 24
1 25
2 25
3 25
3 33
1 34
3 34
1 43
2 43
3 44
i+1
Is sorted wrt
first i digits.
1 25
1 34
1 43
2 24
2 25
2 43
3 25
3 33
3 34
3 44
Is sorted wrt
first i+1 digits.
These are in the
correct order
because sorted
wrt high order digit.
306
RadixSort
2 24
1 25
2 25
3 25
3 33
1 34
3 34
1 43
2 43
3 44
i+1
Is sorted wrt
first i digits.
1 25
1 34
1 43
2 24
2 25
2 43
3 25
3 33
3 34
3 44
Is sorted wrt
first i+1 digits.
These are in the
correct order
because was sorted &
stable sort left sorted.
307
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 3 3 3
Input: N records each labeled with a digit
between 0&(k-1).
Output: Stable sort the numbers.
Algorithm: Count to determine where records go.
Go through the records in order
putting them where they go.
308
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Stable sort: If two records are the same for that digit,
their order does not change.
Therefore, the 4th record in input with digit 1 must be
the 4th record in output with digit 1.
It belongs in output index 8, because
8 records go before it
ie 5 records with a smaller digit &
3 records with the same digit
Count
These309
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output:
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
# of records with digit v:
0
5
1
9
2
3
3
2
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output:
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
# of records with digit v:
# of records with digit < v:
0
5
0
1
9
5
2 3
3 2
14 17
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output:
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
# of records with digit v:
# of records with digit < v:
0
5
0
1
9
5
2 3
3 2
14 17
Computed
N records, k different values. Time to count?
(k)
312
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
# of records with digit < v:
Location of first record
with digit v.
0
0
1
5
2 3
14 17
313
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1
Output: 0 ?
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of first record
with digit v.
0
0
1
5
2 3
14 17
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1
Output:
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
0
1
5
2 3
14 17
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1
Output: 0
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
0
1
6
2 3
14 17
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1
Output: 0 0
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
1
1
6
2 3
14 17
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1 1
Output: 0 0
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
2
1
6
2 3
14 17
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1 1
Output: 0 0
3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
2
1
7
2 3
14 17
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1 1 1
3
Output: 0 0
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
2
1
7
2 3
14 18
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1 1 1 1
3
Output: 0 0
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
2
1
8
2 3
14 18
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1 1 1 1
3 3
Output: 0 0
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
2
1
9
2 3
14 18
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1 1 1 1 1
3 3
Output: 0 0
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
2
1
9
2 3
14 19
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1 1 1 1 1
3 3
Output: 0 0 0
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
2
1 2 3
10 14 19
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1 1 1 1 1
3 3
Output: 0 0 0
2
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
3
1 2 3
10 14 19
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1 1 1 1 1 1
2
3 3
Output: 0 0 0
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
3
1 2 3
10 15 19
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
1 1 1 1 1 1
2
3 3
Output: 0 0 0
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
3
1 2 3
10 15 19
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v:
Location of next record
with digit v.
0
5
1 2 3
14 17 19
328
Radix/Counting Sort
Input: N numbers, each L bits long.
Output: Sort the numbers.
N
111101110100101000101
101001010100010110111
110100011010011110101
L
329
Radix/Counting Sort
Input: N numbers, each L bits long.
Each card contains d digits.
Each digit between 0&(k-1)
Output: Sort the numbers.
Use Radix Sort: Sort wrt each digit using Counting Sort.
330
Radix/Counting Sort
331
Radix/Counting Sort
Time = (Time of Radix Sort)
= (# of digits) (Time of Counting Sort)
= L/log k
(N+k) (log N + log k) bit ops
Set k to minimize time.
Wants k big.
Set k=N
332
Radix/Counting Sort
Time = (Time of Radix Sort)
= (# of digits) (Time of Counting Sort)
= L/log k
(N+k) (log N + log k) bit ops
= L/log N N
log N bit ops
= (L N) bit ops = (n) bit ops
Size = Size of N numbers, each L bits long.
= (L N) = n
Linear Time
But sorting should take (N log N) time!!!
333
Radix/Counting Sort
Time = (L N) bit ops (N log N) bit ops
Size = Size of N numbers, each L bits long.
= (L N)
L => log N if you want N distinct numbers.
335
Values:
Indexes:
23
67
34
21
87
13
11
Output:
Values:
Indexes:
11
13
21
23
34
64
87
Lower Bound:
Input: An Algorithm for Sorting A
Output: An instance I
on which alg A either
Values:
Indexes:
23
67
34
21
87
13
11
11
13
21
23
34
64
87
337
Define Loop
Invariants
Define Measure of
Progress
79 km
to school
Define Step
Exit
Make Progress
Initial Conditions
Ending
Exit
79 km
75 km
km
0 km
Exit
Exit
338
23
67
34
21
87
13
11
339
340
341
3
4
2
5
8
4
2
1
6
5
3
3
6
6
Oh dear!
The first t time steps of my
algorithm A are the same given
any of these instances!!!
342
3
4
2
5
8
4
2
1
6
5
3
3
6
6
Oh dear!
The first 0 time steps of my
algorithm A are the same given
any of these instances!!!
343
3
4
2
5
79 km
km
8
4
2
1
6
5
3
3
6
6
3
4
2
5
8
4
2
1
6
5
3
3
6
6
3
4
2
5
8
4
2
1
6
5
3
3
6
6
3
4
2
5
8
4
2
1
6
5
3
3
6
6
347
4
3
5
2
6
5
7
8
8
6
Oh dear!
The first t+1 time steps of my
algorithm A are the same given
any of these instances!!!
348
4
3
79 km
5
2
6
5
7
8
8
6
349
4
3
Exit
5
2
6
5
7
8
8
6
T = log(N!)
350
T = log(N!) = (N log(N)).
351
Exit
Values:
Indexes:
23
67
34
21
87
13
11
352
Exit
23
67
34
21
87
13
11
Exit
Oops!
T = log(N!) = (N log(N)).
353
Exit
23
67
34
21
87
13
11
Exit
Oops!
I must give
the wrong
answer on one
of these!
354
355
356