1.
Recursion
Jeff Edmonds
York University
COSC 3101LectureLecture 44
Code
Stack of Stack Frames
Tree of Stack Frames
Friends and Strong Induction
Towers of Hanoi
Merge & Quick Sort
Simple Recursion on Trees
Generalizing the Problem
Heap Sort & Priority Queues
Trees Representing Equations
Pretty Print
Parsing
Recursive Images
Feedback
2.
The Goal is to
UNDERSTAND
Recursive Algorithms
Fundamentally to their core
3.
Representation:
Understand the relationship between
different representations of the same
information or idea
1
2
3
Rudich www.discretemath.com
4.
Different Representations
of Recursive Algorithms
Code - Implement on Computer
Stack of Stack Frames - Run on Computer
Tree of Stack Frames - View entire computation
Friends & Strong
Induction
- Worry about one step at
a time.
ProsViews
5.
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Code
Representation of an Algorithm
Pros and Cons?
6.
Code
Representation of an Algorithm
• 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
Pros: Cons:
7.
Different Representations
of Recursive Algorithms
Code - Implement on Computer
Stack of Stack Frames - Run on Computer
Tree of Stack Frames - View entire computation
Friends & Strong
Induction
- Worry about one step at
a time.
ProsViews
8.
X = 2133
Y = 2312
ac =
bd =
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
Stack Frame: A particular
execution of one routine on one
particular input instance.
9.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
10.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac =
bd =
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
11.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = ?
bd =
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
12.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 2
Y = 2
XY=
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
13.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 2
Y = 2
XY = 4
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
14.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = 4
bd =
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
15.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = 4
bd = ?
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
16.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = 4
bd = ?
(a+b)(c+d) =
XY =
X = 1
Y = 3
XY = 3
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
17.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = 4
bd = 3
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
18.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = 4
bd = 3
(a+b)(c+d) = ?
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
19.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = 4
bd = 3
(a+b)(c+d) = ?
XY =
X = 3
Y = 5
XY = 15
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
20.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = 4
bd = 3
(a+b)(c+d) = 15
XY = ?
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
21.
X = 2133
Y = 2312
ac = ?
bd =
(a+b)(c+d) =
XY =
X = 21
Y = 23
ac = 4
bd = 3
(a+b)(c+d) = 15
XY = 483
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
22.
X = 2133
Y = 2312
ac = 483
bd =
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
23.
X = 2133
Y = 2312
ac = 483
bd = ?
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
24.
X = 2133
Y = 2312
ac = 483
bd = ?
(a+b)(c+d) =
XY =
X = 33
Y = 12
ac = ?
bd =
(a+b)(c+d) =
XY = 15
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
25.
X = 2133
Y = 2312
ac = 483
bd = ?
(a+b)(c+d) =
XY =
X = 33
Y = 12
ac = ?
bd =
(a+b)(c+d) =
XY = 15
X = 3
Y = 1
XY = 3
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
26.
X = 2133
Y = 2312
ac = 483
bd = ?
(a+b)(c+d) =
XY =
X = 33
Y = 12
ac = 3
bd = ?
(a+b)(c+d) =
XY = 15
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
27.
X = 2133
Y = 2312
ac = 483
bd = ?
(a+b)(c+d) =
XY =
X = 33
Y = 12
ac = 3
bd = ?
(a+b)(c+d) =
XY = 15
X = 3
Y = 2
XY = 6
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
28.
X = 2133
Y = 2312
ac = 483
bd = ?
(a+b)(c+d) =
XY =
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = ?
XY = 15
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
29.
X = 2133
Y = 2312
ac = 483
bd = ?
(a+b)(c+d) =
XY =
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = ?
XY = 396
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Stack of Stack Frames
An so on ….
30.
Stack of Stack Frames
Representation of an Algorithm
• Trace what actually
occurs in the computer
• Concrete.
• It is what students
attempt to describe
when told not to give
code.
• Described in words it
is impossible to follow
who is doing what.
• Does not explain why
it works.
• Demonstrates for only
one of many inputs.
Pros: Cons:
31.
Different Representations
of Recursive Algorithms
Code - Implement on Computer
Stack of Stack Frames - Run on Computer
Tree of Stack Frames - View entire computation
Friends & Strong
Induction
- Worry about one step at
a time.
ProsViews
32.
X = 2133
Y = 2312
ac = 483
bd = 396
(a+b)(c+d) = 1890
XY = 4931496
X = 21
Y = 23
ac = 4
bd = 3
(a+b)(c+d) = 15
XY = 483
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = 18
XY = 396
X = 54
Y = 35
ac = 15
bd = 20
(a+b)(c+d) = 72
XY = 1890
X = 2
Y = 2
XY=4
X = 1
Y = 3
XY=3
X = 3
Y = 5
XY=15
X = 3
Y = 1
XY=3
X = 3
Y = 2
XY=6
X = 6
Y = 3
XY=18
X = 5
Y = 3
XY=15
X = 4
Y = 5
XY=20
X = 9
Y = 8
XY=72
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Tree of Stack Frames
33.
Stack of Stack Frames
Representation of an Algorithm
• View the entire
computation.
• Good for computing
the running time.
• Must describe entire tree.
– For each stack frame
• input instance
• computation
• solution returned.
– who calls who
• Structure of tree may be
complex.
Pros: Cons:
34.
Different Representations
of Recursive Algorithms
Code - Implement on Computer
Stack of Stack Frames - Run on Computer
Tree of Stack Frames - View entire computation
Friends & Strong
Induction
- Worry about one step at
a time.
ProsViews
35.
X = 2133
Y = 2312
ac = 483
bd = 396
(a+b)(c+d) = 1890
XY = 4931496
X = 21
Y = 23
ac = 4
bd = 3
(a+b)(c+d) = 15
XY = 483
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = 18
XY = 396
X = 54
Y = 35
ac = 15
bd = 20
(a+b)(c+d) = 72
XY = 1890
X = 2
Y = 2
XY=4
X = 1
Y = 3
XY=3
X = 3
Y = 5
XY=15
X = 3
Y = 1
XY=3
X = 3
Y = 2
XY=6
X = 6
Y = 3
XY=18
X = 5
Y = 3
XY=15
X = 4
Y = 5
XY=20
X = 9
Y = 8
XY=72
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
One Friend for each
stack frame.
Each worries only
about his job.
36.
X = 2133
Y = 2312
ac = 483
bd = 396
(a+b)(c+d) = 1890
XY = 4931496
X = 21
Y = 23
ac = 4
bd = 3
(a+b)(c+d) = 15
XY = 483
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = 18
XY = 396
X = 54
Y = 35
ac = 15
bd = 20
(a+b)(c+d) = 72
XY = 1890
X = 2
Y = 2
XY=4
X = 1
Y = 3
XY=3
X = 3
Y = 5
XY=15
X = 3
Y = 1
XY=3
X = 3
Y = 2
XY=6
X = 6
Y = 3
XY=18
X = 5
Y = 3
XY=15
X = 4
Y = 5
XY=20
X = 9
Y = 8
XY=72
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
Worry about one
step at a time.
Imagine that you
are one specific
friend.
37.
X = 33
Y = 12
ac =
bd =
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
•Consider your input instance
38.
X = 33
Y = 12
ac = ?
bd = ?
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
•Consider your input instance
•Allocate work
•Construct one or more
subinstances
X = 3
Y = 1
XY=?
X = 3
Y = 2
XY=?
X = 6
Y = 3
XY=?
39.
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = 18
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
•Consider your input instance
•Allocate work
•Construct one or more
subinstances
•Assume by magic your friends
give you the answer for these.
X = 3
Y = 1
XY=3
X = 3
Y = 2
XY=6
X = 6
Y = 3
XY=18
40.
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = 18
XY = 396
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
•Consider your input instance
•Allocate work
•Construct one or more
subinstances
•Assume by magic your friends
give you the answer for these.
•Use this help to solve your own
instance.
X = 3
Y = 1
XY=3
X = 3
Y = 2
XY=6
X = 6
Y = 3
XY=18
41.
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = 18
XY = 396
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
•Consider your input instance
•Allocate work
•Construct one or more
subinstances
•Assume by magic your friends
give you the answer for these.
•Use this help to solve your own
instance.
•Do not worry about anything else.
•Who your boss is.
•How your friends solve their
instance.X = 3
Y = 1
XY=3
X = 3
Y = 2
XY=6
X = 6
Y = 3
XY=18
42.
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = 18
XY = 396
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
This technique is often
referred to as
Divide and Conquer
X = 3
Y = 1
XY=3
X = 3
Y = 2
XY=6
X = 6
Y = 3
XY=18
43.
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
Consider generic instances.
ac bd (a+b)(c+d)
MULT(X,Y):
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Remember!
Do not worry about
•Who your boss is.
•How your friends solve
their instance.
44.
X = 2133
Y = 2312
ac = 483
bd = 396
(a+b)(c+d) = 1890
XY = 4931496
X = 21
Y = 23
ac = 4
bd = 3
(a+b)(c+d) = 15
XY = 483
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = 18
XY = 396
X = 54
Y = 35
ac = 15
bd = 20
(a+b)(c+d) = 72
XY = 1890
X = 2
Y = 2
XY=4
X = 1
Y = 3
XY=3
X = 3
Y = 5
XY=15
X = 3
Y = 1
XY=3
X = 3
Y = 2
XY=6
X = 6
Y = 3
XY=18
X = 5
Y = 3
XY=15
X = 4
Y = 5
XY=20
X = 9
Y = 8
XY=72
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
This solves the
problem for every
possible instance.
45.
Friends & Strong Induction
Recursive Algorithm:
•Assume you have an algorithm that works.
•Use it to write an algorithm that works.
46.
Friends & Strong Induction
Recursive Algorithm:
•Assume you have an algorithm that works.
•Use it to write an algorithm that works.
If I could get in,
I could get the key.
Then I could unlock the door
so that I can get in.
Circular Argument!
47.
Friends & Strong Induction
Recursive Algorithm:
•Assume you have an algorithm that works.
•Use it to write an algorithm that works.
To get into my house
I must get the key from a smaller house
48.
X = 33
Y = 12
ac = ?
bd = ?
(a+b)(c+d) =
XY =
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
•Allocate work
•Construct one or more
subinstances
X = 3
Y = 1
XY=?
X = 3
Y = 2
XY=?
X = 6
Y = 3
XY=?
Each subinstance must be
a smaller instance
to the same problem.
49.
Friends & Strong Induction
Recursive Algorithm:
•Assume you have an algorithm that works.
•Use it to write an algorithm that works.
Use brute force
to get into
the smallest house.
50.
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
e = MULT(a,c) and f =MULT(b,d)
RETURN
e 10n
+ (MULT(a+b, c+d) – e - f) 10n/2
+ f
Friends & Strong Induction
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Use brute force
to solve the base case
instances.
51.
Friends & Strong Induction
Carefully write the specifications for the problem.
Preconditions: Set of legal
instances (inputs)
Why?
Postconditions: Required output
52.
Friends & Strong Induction
Carefully write the specifications for the problem.
Preconditions: Set of legal
instances (inputs)
•To be sure that we solve
the problem for every legal
instance.
•So that we know
–what we can give to a
friend.
Postconditions: Required output •So that we know
–what is expected of us.
–what we can expect
from our friend.
Related to Loop Invariants
53.
Induction
S
i S S S S i S i
n S n
( )
, [ ( ) , ( ) , ( ) , ... , ( ) ( ) ]
( )
0
0 1 2 1∀ − ⇒
⇒ ∀
S
iS i S i
n S n
( )
( ) ( )
( )
0
1∀ ⇒ +
⇒ ∀
Strong Induction
54.
Friends & Strong Induction
⇒
⇒ ∀ − ⇒
⇒ ∀ ⇒
S
i S S S S i S i
n S n
( )
, [ ( ) , ( ) , ( ) , ... , ( ) ( ) ]
( )
0
0 1 2 1
S n( ) =
•Induction Hypothesis:
``The recursive algorithm works
for every instance of size n.''
``The algorithm
works for every
instance of any
size.''
Base case
size i
55.
Friends & Strong Induction
Representation of an Algorithm
• Worry about one step
at a time.
• An abstraction within
which to develop,
think about, and
describe algorithms in
such way that their
correctness is
transparent.
• Students resist it.
• Students expect too much from
their friends.
Each subinstance must be
a smaller instance
to the same problem.
• Students expect too much from
their boss.
You only know your instance.
You do not know your
boss’s instance.
Pros: Cons:
56.
Output
Try explaining what this
algorithm does by tracing it out.
I dare you!!!
It is much easier to
TAKE ONE STEP AT A TIME
69.
Towers of Hanoi
Get a job at the Towers of Hanoi company
at level n=1,2,3,4,…..
Or get transferred in as the president
& you decide what your vice president does.
70.
Towers of Hanoi
Time: T(1)=1, T(n) = 2T(n-1) + 1 = Θ(2n
)
72.
Sorting 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
14
9825
62
52
79
30
23
31 14,23,25,30,31,52,62,79,88,98
73.
Recursive Sorts
• Given list of objects to be sorted
• Split the list into two sublists.
• Recursively have a friend sort the two sublists.
• Combine the two sorted sublists into one entirely sorted list.
74.
Minimal effort splitting
Lots of effort recombining
Lots of effort splitting
Minimal effort recombining
Four Recursive Sorts
Merge Sort Insertion Sort
Quick Sort Selection Sort
Size of Sublists
n/2,n/2 n-1,1
76.
Merge Sort
88
14
9825
62
52
79
30
23
31
Split Set into Two
(no real work)
25,31,52,88,98
Get one friend to
sort the first half.
14,23,30,62,79
Get one friend to
sort the second half.
77.
Merge Sort
Merge two sorted lists into one
25,31,52,88,98
14,23,30,62,79
14,23,25,30,31,52,62,79,88,98
80.
Quick Sort
88
14
9825
62
52
79
30
23
31
Partition set into two using
randomly chosen pivot
14
25
30
2331
88
98
62
79
≤ 52 ≤
81.
Quick Sort
14
25
30
2331
88
98
62
79
≤ 52 ≤
14,23,25,30,31
Get one friend to
sort the first half.
62,79,98,88
Get one friend to
sort the second half.
82.
Quick Sort
14,23,25,30,31
62,79,98,88
52
Glue pieces together.
(No real work)
14,23,25,30,31,52,62,79,88,98
83.
Quick Sort
88
14
9825
62
52
79
30
23
31
Let pivot be the first
element in the list?
14
25
30
23
88
98
62
79
≤ 31 ≤
52
84.
Quick Sort
≤ 14 ≤
14,23,25,30,31,52,62,79,88,98
23,25,30,31,52,62,79,88,98
If the list is already sorted,
then the slit is worst case unbalanced.
89.
Kth
Element Problem Specification
• Precondition: The input is a list of n values
and an integer k.
• Post condition: The kth
smallest in the list.
(or the first k smallest)
88
14
9825
62
52
79
30
23
31
14,23,25,30,31,52,62,79,88,98
k=3
Output: 25
90.
88
14
9825
62
52
79
30
23
31
Partition set into two using
randomly chosen pivot
14
25
30
2331
88
98
62
79
≤ 52 ≤
Kth
Element Problem Specification
91.
14
25
30
2331
88
98
62
79
≤ 52 ≤
Kth
Element Problem Specification
k=3 r=5 elements on left
14,23,25,30,31
Get one friend to
find k=3rd
element in first half.
Output: 25
92.
14
25
30
2331
88
98
62
79
≤ 52 ≤
Kth
Element Problem Specification
k=8 r=5 elements on left
Get one friend to
find k=8-5=3rd
element in second half.
Output: 79
52,62,79,98,88
93.
T(n) =
Kth
Element Problem Specification
= Θ(n)
1 T(n/2) + Θ(n)Best Time:
Expected Time:
Worst Time:
n
log a
/log b
# of base cases =
= n0
= 1n
log 1
/log 2=
94.
T(n) =
= Θ(n)
1 T(n/2) + Θ(n)
Expected Time:
Kth
Element Problem Specification
T(n) = 1 T(n-1) + Θ(n)Worst Time:
= Θ(n2
)
Best Time:
143.
38
25
17
4 21
31
28 35
51
42
40 49
63
55 71
Binary Search Tree
Its left children ≤ Any node ≤ Its right children
≤ ≤
144.
Define Loop Invariant
• Maintain a sub-tree.
• If the key is contained in the original tree,
then the key is contained in the sub-tree.
key 17
38
25
17
4 21
31
28 35
51
42
40 49
63
55 71
145.
Define Step
• Cut sub-tree in half.
• Determine which half the key would be in.
• Keep that half.
key 17
38
25
17
4 21
31
28 35
51
42
40 49
63
55 71
If key < root,
then key is
in left half.
If key > root,
then key is
in right half.
If key = root,
then key is
found
153.
Heap Definition
•Completely Balanced Binary Tree
•The value of each node
≥ each of the node's children.
•Left or right child could be larger.
Where can 1 go?
Maximum is at root.
Where can 8 go?
Where can 9 go?
154.
Heap Data Structure
Completely Balanced Binary Tree
Implemented by an Array
167.
Insertion Sort
Largest i values are sorted on side.
Remaining values are off to side.
6,7,8,9
<
3
4
1
5
2
Exit
79 km 75 km
Exit
Max is easier to find if a heap.
Insertion
168.
Heap Sort
Largest i values are sorted on side.
Remaining values are in a heap.
Exit
79 km 75 km
Exit
196.
Please feel free
to ask questions!
Please give me feedback
so that I can better
serve you.
Thanks for the
feedback that you have
given me already.
197.
It is too hard.
Feed Back
• Yes it is.
• Are you reading?
• Are you thinking about it on your own?
• Are you talking to friends?
• I have taught both harder and easier.
• Then students work harder and less,
• learn more material or less material.
• And say it is hard the same.
198.
Other classes I can just show up
without working between classes
and follow.
This should be like that.
Feed Back
No. I facilitate learning.
Not spoon feed.
199.
It is too abstract. .
=
•I hope that you will see the
simplicity and the beauty in it.
Feed Back
•Yes it is.
•My goal is to teach you to
think abstractly.
200.
Could you trace out the
algorithms on examples?
Feed Back
•We don’t really have time.
•This would be good for you to
do on your own.
•I do try to,
when I think it will help.
201.
Could you give more details
of the code?
Feed Back
•It is not a coding course.
•I want to give you the big
picture not of one algorithm but
algorithms in general.
•It’s a trade off. One can get
lost in the details.
202.
I can’t follow the lectures.
•Are you reading before class.
Feed Back
•Ask questions.
I am so lost
I don’t know what to ask.
•Tell me where you first got lost.
•Ask me to slow down.
203.
Assignments are too hard.
Feed Back
Yes. I want you to think about new
and hard things on your own.
•Sometimes this is a mistake.
•I want you to have thought about material
before we teach it so that you appreciate it.
Assignments need material
not covered in class.
204.
Feed Back
Thanks for the
feedback that you have
given me.
Be the first to comment