SlideShare une entreprise Scribd logo
1  sur  226
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
The Goal is to
UNDERSTAND
Recursive 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 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
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?
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:
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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 ….
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:
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
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
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:
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
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.
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.
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
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=?
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
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
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
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
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.
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.
Friends & Strong Induction
Recursive Algorithm:
•Assume you have an algorithm that works.
•Use it to write an algorithm that works.
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!
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
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.
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.
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.
Friends & Strong Induction
Carefully write the specifications for the problem.
Preconditions: Set of legal
instances (inputs)
Why?
Postconditions: Required output
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
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
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
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:
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
n=1 X
n=2 Y
Output
n=1 X
n=2
n=3
Y
A ? B ? C
Output
n=1 X
n=2
n=3
Y
A Y B X C
Output
n=1 X
n=2
n=3
Y
AYBXC
Output
n=1 X
n=2
n=3
n=4
Y
AYBXC
A ? B ? C
Output
n=1 X
n=2
n=3
n=4
Y
AYBXC
A AYBXC B Y C
Output
n=1 X
n=2
n=3
n=4
Y
AYBXC
AAYBXCBYC
Output
n=1 X
n=2
n=3
n=4
n=5
Y
AYBXC
AAYBXCBYC
A ? B ? C
Output
n=1 X
n=2
n=3
n=4
n=5
Y
AYBXC
AAYBXCBYC
A AAYBXCBYC
B AYBXC C
Output
n=1 X
n=2
n=3
n=4
n=5
Y
AYBXC
AAYBXCBYC
AAAYBXCBYCBAYBXCC
Output
Towers of Hanoi
Towers of Hanoi
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.
Towers of Hanoi
Time: T(1)=1, T(n) = 2T(n-1) + 1 = Θ(2n
)
Time: T(1) =
≈ 2T(n-1) +3
T(n-1) + T(n-2) + 3T(n) =
11 T(2) =
= 2Θ(n)
defined?
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
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.
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
Merge Sort
88
14
9825
62
52
79
30
23
31
Divide and Conquer
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.
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
Merge Sort Sort
Time: T(n) =
= Θ(n log(n))
2T(n/2) + Θ(n)
Quick Sort
88
14
9825
62
52
79
30
23
31
Divide and Conquer
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 ≤
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.
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
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
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.
Quick Sort
Best Time:
Worst Time:
Expected Time:
T(n) = 2T(n/2) + Θ(n)
= Θ(n log(n))
Quick Sort
T(n) = 2T(n/2) + Θ(n)
= Θ(n log(n))
Best Time:
Worst Time:
Expected Time:
= Θ(n2
)
T(n) = T(0) + T(n-1) + Θ(n)
Quick Sort
T(n) = 2T(n/2) + Θ(n)
= Θ(n log(n))
Best Time:
T(n) = T(0) + T(n-1) + Θ(n)Worst Time:
Expected Time:
= Θ(n2
)
T(n) = T(1
/3n) + T(2
/3n) + Θ(n)
= Θ(n log(n))
Quick Sort
Expected Time: T(n) = T(1
/3n) + T(2
/3n) + Θ(n)
= Θ(n log(n))
Top work Θ(n)
2nd
level
3rd
level
# levels =
Θ(n)
Θ(n)
1
/3
1
/3
1
/3
2
/3
2
/3
2
/3
2
/3
= Θ(log(n))
3
/2
log (n)
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
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
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
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
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=
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:
T(n) =
= Θ(n)
1 T(n/2) + Θ(n)
T(n) = 1 T(2
/3n) + Θ(n)Expected Time:
= Θ(n)
Kth
Element Problem Specification
T(n) = 1 T(n-1) + Θ(n)Worst Time:
= Θ(n2
)
Best Time:
Recursion on Trees
3
8
1
3 2
2
7
6
5
9
4
1
Recursion on Trees
A binary tree is:
- the empty tree
- a node with a right and a left subtrees.
3
8
1
3 2
2
7
6
5
9
4
1
tree because
Recursion on Trees
A binary tree is:
- the empty tree
- a node with a right and a left subtrees.
3
8
1
3 2
2
7
6
5
9
4
1
tree
tree
node
Recursion on Trees
A binary tree is:
- the empty tree
- a node with a right and a left subtrees.
3
8
1
3 2
2
7
6
5
9
4
1
tree because
Recursion on Trees
A binary tree is:
- the empty tree
- a node with a right and a left subtrees.
3
8
1
3 2
2
7
6
5
9
4
1
tree tree
node
Recursion on Trees
A binary tree is:
- the empty tree
- a node with a right and a left subtrees.
3
8
1
3 2
2
7
6
5
9
4
1
tree because
Recursion on Trees
A binary tree is:
- the empty tree
- a node with a right and a left subtrees.
3
8
1
3 2
2
7
6
5
9
4
1
tree
node
tree
Recursion on Trees
A binary tree is:
- the empty tree
- a node with a right and a left subtrees.
3
8
1
3 2
2
7
6
5
9
4
1tree because
empty
Recursion on Trees
number of nodes = ?
3
8
1
3 2
2
7
6
5
9
4
1
6
5
Get help from friends
Recursion on Trees
number of nodes
3
8
1
3 2
2
7
6
5
9
4
1
= number on left + number on right + 1
= 6 + 5 + 1 = 12
6
5
Recursion on Trees
Base Case ?
3
8
1
3 2
2
7
6
5
9
4
1
number of nodes
1
Are we done?
Recursion on Trees
Base Case ?
3
8
1
3 2
2
7
6
5
9
4
1
number of nodes
?
Recursion on Trees
Base Case ?
3
8
1
3 2
2
7
6
5
9
4
1
number of nodes
0
Better base case!
Recursion on Trees
Base Case ?
3
8
1
3 2
2
7
6
5
9
4
1
number of nodes
1
Does this still need to be a base case?
Recursion on Trees
3
8
1
3 2
2
7
6
5
9
4
1
00
number of nodes
= number on left + number on right + 1
= 0 + 0 + 1 = 1
No!
Recursion on Trees
3
8
1
3 2
2
7
6
5
9
4
1
00
Most programmers don’t use
the empty tree as a base case.
This causes a lot more work.
Recursion on Trees
Recursion on Trees
3
Designing Program/Test Cases
generic generic
3
generic
3 0
00
0+0+1=1
n1 n2
n1 +n2 + 1
0n1
n1 + 1
Same code works!
Same code works!
Try same code
Try same code
Time: T(n) =
= Θ(n)
T(left) + T(right) + Θ(1)
Recursion on Trees
One stack frame for each node in the tree
Recursion on Trees
sum of nodes = ?
3
8
1
3 2
2
7
6
5
9
4
1
23
25
Get help from friends
Recursion on Trees
sum of nodes
3
8
1
3 2
2
7
6
5
9
4
1
= sum on left + sum on right + value at root
= 23 + 25 + 3 = 51
23
25
Recursion on Trees
Base Case ?
3
8
1
3 2
2
7
6
5
9
4
1
sum of nodes = ?
0
Recursion on Trees
Recursion on Trees
3
generic generic
3
generic
3 0
s1 s2
s1 +s2 + 3
0s1
s1 + 3
00
0+0+ 3 =3
Designing Program/Test Cases
Recursion on Trees
max of nodes = ?
3
8
1
3 2
2
7
6
5
9
4
1
8
9
Get help from friends
Recursion on Trees
max of nodes
3
8
1
3 2
2
7
6
5
9
4
1
= max( max on left, max on right, value at root)
= max( 8, 9, 3) = 9
8
9
Recursion on Trees
Base Case ?
3
8
1
3 2
2
7
6
5
9
4
1
max of nodes = ?
?
3+4+2+8+…
Sum so far is 17
Sum so far is 0
Recursion on Trees
NewSum = OldSum + nextElement
= 0 + 3
= 3
3*4*2*3*…
Product so far is 72
Product so far is 1
Recursion on Trees
NewProd = OldProd × nextElement
= 1 × 3
= 3
Max(3,4,2,3,…
Max so far is 4
Max so far is -∞
Recursion on Trees
NewMax = max(OldMax, next Element)
= max( -∞ , 3 )
= 3
Recursion on Trees
Recursion on Trees
height of tree = ?
3
8
1
3 2
2
7
6
5
9
4
1
3
4
Get help from friends
Recursion on Trees
height of tree
3
8
1
3 2
2
7
6
5
9
4
1
= max(height of left,height on right) + 1
= max(3,4) + 1 = 4
3
4
Recursion on Trees
Base Case ?
3
8
1
3 2
2
7
6
5
9
4
1
height of tree
?
Recursion on Trees
3
8
1
3 2
2
7
6
5
9
4
1
height of tree?
2 nodes or 1 edge
0 nodes
? edges
Base Case ?
Recursion on Trees
3
8
1
3 2
2
7
6
5
9
4
1
Work it out backwards
? edges
height =
= max(height of left,height on right) + 1
= max(?,?) + 1
??
0 edge
Recursion on Trees
3
8
1
3 2
2
7
6
5
9
4
1
Work it out backwards
-1edges
height =
= max(height of left,height on right) + 1
= max(-1,-1) + 1
-1-1
0 edge
Recursion on Trees
Recursion on Trees
number of leaves = ?
3
8
1
3 2
2
7
6
5
9
4
1
3
2
Get help from friends
Recursion on Trees
number of leaves
3
8
1
3 2
2
7
6
5
9
4
1
= number on left + number on right
= 3 + 2 = 5
3
2
Recursion on Trees
Base Case ?
3
8
1
3 2
2
7
6
5
9
4
1
number of leaves
0
Recursion on Trees
3
generic generic
3
generic
3 0
L1 L2
L1 +L2
0L1
L1
00
0+0 = 0
Needs to be
another base case.
1 No!
Designing Program/Test Cases
Recursion on Trees
Recursion on Trees
Recursion on Trees
Recursion on Trees
?
Drops the tree.
38
25
17
4 21
31
28 35
51
42
40 49
63
55 71
Define Problem: Binary Search Tree
• PreConditions
– Key 25
– A binary search tree.
– PostConditions
– Find key in BST (if there).
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
≤ ≤
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
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
Recursion on Trees
Recursion on Trees
Time: T(n) =
= Θ(n log(n))
2T(n/2) + Θ(
Recursion on Trees
Computing max is too much work.
n)
Recursion on Trees
Extra info from belowExtra info from below
Time: T(n) =
= Θ(n log(n))
2T(n/2) + Θ(
Recursion on Trees
Computing max is too much work.
n)
1
n
Recursion on Trees
Extra info from above
-∞, ∞
Heaps, Heap Sort, &
Priority Queues
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?
Heap Data Structure
Completely Balanced Binary Tree
Implemented by an Array
Make Heap
Get help from friends
Heapify
?
Maximum needs
to be at root.
Where is the maximum?
Find the maximum. Put it in place
?
Repeat
Heapify
Heap
Running Time:
Heapify
Make Heap
Get help from friends
T(n) = 2T(n/2) + log(n)
Running time:
= Θ(n)
Heapify
Heap
Heaps Heap
?
Another algorithm
?
Heap
?
Heap
?
Heap
Running Time:
i
log(n) -i
2log(n) -i
Priority Queues
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
Heap Sort
Largest i values are sorted on side.
Remaining values are in a heap.
Exit
79 km 75 km
Exit
Heap Data Structure
Heap Array
5 3 4 2 1
98
76
Heap
Array
Heap Sort
Largest i values
are sorted on side.
Remaining values are
in a heap
Exit
Put next value
where it belongs.
79 km 75 km
Exit
Heap
?
Heap Sort
Heap Sort
?
? ?
? ?
? ?
Sorted
Heap Sort
Running Time:
Recursion on Trees
Evaluate Equation Tree = ?
12
7
Get help from friends
Recursion on Trees
Evaluate Equation Tree
12
7
= rootop( value on left, value on right )
= rootop(12,7) = 12 + 7 = 19
Recursion on Trees
Evaluate Equation Tree
7
Base Case ?
Recursion on Trees
Derivatives
Recursion on Trees
Printing a Tree
When I taught this
one year, a student
needed just this
algorithm for his job.
Recursion on Trees
Typing line by line?
Get help from friends
Recursion on Trees
One stack frame prints:
His input gives:
his tree
whether left or right
sting for each line
Recursion on Trees
He gives his friends:
their trees
whether left or right
sting for each line
Parsing
(1×x-x×1/(x×x)×x-(x/x)×1)/(x×x)
Input:
Output:
Parsing
Parsing
Parsing
Parsing
Parsing
Recursive Images
if n=0, draw
else draw
And recursively
Draw here with n-1
if n=1
n=0
Recursive Images
if n=0, draw
else draw
And recursively
Draw here with n-1
if n=2
n=1
Recursive Images
if n=0, draw
else draw
And recursively
Draw here with n-1
if n=3
n=2
Recursive Images
if n=0, draw
else draw
And recursively
Draw here with n-1
if n=30
Recursive Images
if n=1if n=2if n=3if n=4if n=5
if n=0
Recursive Images
if n=1
if n=0
if n=2if n=3if n=4if n=5
Recursive Images
L(n) = 4
/3 L(n-1) ⇒ ∞= (4
/3)n
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.
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.
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.
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.
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.
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.
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.
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.
Feed Back
Thanks for the
feedback that you have
given me.
End

Contenu connexe

Tendances

Pc30 June 2001 Exam Ans Key
Pc30 June 2001 Exam Ans KeyPc30 June 2001 Exam Ans Key
Pc30 June 2001 Exam Ans Key
ingroy
 
Howard, anton calculo i- um novo horizonte - exercicio resolvidos v1
Howard, anton   calculo i- um novo horizonte - exercicio resolvidos v1Howard, anton   calculo i- um novo horizonte - exercicio resolvidos v1
Howard, anton calculo i- um novo horizonte - exercicio resolvidos v1
cideni
 
Howard, anton cálculo ii- um novo horizonte - exercicio resolvidos v2
Howard, anton   cálculo ii- um novo horizonte - exercicio resolvidos v2Howard, anton   cálculo ii- um novo horizonte - exercicio resolvidos v2
Howard, anton cálculo ii- um novo horizonte - exercicio resolvidos v2
Breno Costa
 

Tendances (20)

Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Calculus 10th edition anton solutions manual
Calculus 10th edition anton solutions manualCalculus 10th edition anton solutions manual
Calculus 10th edition anton solutions manual
 
Econometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions ManualEconometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions Manual
 
Techniques of integration
Techniques of integrationTechniques of integration
Techniques of integration
 
Sbma 4603 numerical methods Assignment
Sbma 4603 numerical methods AssignmentSbma 4603 numerical methods Assignment
Sbma 4603 numerical methods Assignment
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Pc30 June 2001 Exam Ans Key
Pc30 June 2001 Exam Ans KeyPc30 June 2001 Exam Ans Key
Pc30 June 2001 Exam Ans Key
 
Maths 301 key_sem_1_2009_2010
Maths 301 key_sem_1_2009_2010Maths 301 key_sem_1_2009_2010
Maths 301 key_sem_1_2009_2010
 
Who wants to pass this course
Who wants to pass this courseWho wants to pass this course
Who wants to pass this course
 
Maths05
Maths05Maths05
Maths05
 
Chapter 2(limits)
Chapter 2(limits)Chapter 2(limits)
Chapter 2(limits)
 
Iit jee question_paper
Iit jee question_paperIit jee question_paper
Iit jee question_paper
 
Finite elements : basis functions
Finite elements : basis functionsFinite elements : basis functions
Finite elements : basis functions
 
Howard, anton calculo i- um novo horizonte - exercicio resolvidos v1
Howard, anton   calculo i- um novo horizonte - exercicio resolvidos v1Howard, anton   calculo i- um novo horizonte - exercicio resolvidos v1
Howard, anton calculo i- um novo horizonte - exercicio resolvidos v1
 
Palm ch1
Palm ch1Palm ch1
Palm ch1
 
Howard, anton cálculo ii- um novo horizonte - exercicio resolvidos v2
Howard, anton   cálculo ii- um novo horizonte - exercicio resolvidos v2Howard, anton   cálculo ii- um novo horizonte - exercicio resolvidos v2
Howard, anton cálculo ii- um novo horizonte - exercicio resolvidos v2
 
Mid point
Mid pointMid point
Mid point
 
Matdis 3.4
Matdis 3.4Matdis 3.4
Matdis 3.4
 
09 p.t (straight line + circle) solution
09 p.t (straight line + circle) solution09 p.t (straight line + circle) solution
09 p.t (straight line + circle) solution
 
9 chap
9 chap9 chap
9 chap
 

En vedette (9)

Recursion
RecursionRecursion
Recursion
 
Recursion
RecursionRecursion
Recursion
 
PyOhio Recursion Slides
PyOhio Recursion SlidesPyOhio Recursion Slides
PyOhio Recursion Slides
 
Ch 7 recursion
Ch 7 recursionCh 7 recursion
Ch 7 recursion
 
Recursion
RecursionRecursion
Recursion
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIATypes Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Recursion
RecursionRecursion
Recursion
 
Recursion
RecursionRecursion
Recursion
 

Similaire à 4 recursion

xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docxxy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
ericbrooks84875
 
Gabarito completo anton_calculo_8ed_caps_01_08
Gabarito completo anton_calculo_8ed_caps_01_08Gabarito completo anton_calculo_8ed_caps_01_08
Gabarito completo anton_calculo_8ed_caps_01_08
joseotaviosurdi
 
01 sets, relations and functions
01   sets, relations and functions01   sets, relations and functions
01 sets, relations and functions
vivieksunder
 

Similaire à 4 recursion (17)

xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docxxy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
 
Gabarito completo anton_calculo_8ed_caps_01_08
Gabarito completo anton_calculo_8ed_caps_01_08Gabarito completo anton_calculo_8ed_caps_01_08
Gabarito completo anton_calculo_8ed_caps_01_08
 
Cálculo ii howard anton - capítulo 16 [tópicos do cálculo vetorial]
Cálculo ii   howard anton - capítulo 16 [tópicos do cálculo vetorial]Cálculo ii   howard anton - capítulo 16 [tópicos do cálculo vetorial]
Cálculo ii howard anton - capítulo 16 [tópicos do cálculo vetorial]
 
Banco de preguntas para el ap
Banco de preguntas para el apBanco de preguntas para el ap
Banco de preguntas para el ap
 
CBSE XII MATHS SAMPLE PAPER BY KENDRIYA VIDYALAYA
CBSE XII MATHS SAMPLE PAPER BY KENDRIYA VIDYALAYA CBSE XII MATHS SAMPLE PAPER BY KENDRIYA VIDYALAYA
CBSE XII MATHS SAMPLE PAPER BY KENDRIYA VIDYALAYA
 
12 cbse-maths-2014-solution set 1
12 cbse-maths-2014-solution set 1 12 cbse-maths-2014-solution set 1
12 cbse-maths-2014-solution set 1
 
2.10 translations of graphs t
2.10 translations of graphs t2.10 translations of graphs t
2.10 translations of graphs t
 
H 2008 2011
H 2008   2011H 2008   2011
H 2008 2011
 
01 sets, relations and functions
01   sets, relations and functions01   sets, relations and functions
01 sets, relations and functions
 
Class XII CBSE Mathematics Sample question paper with solution
Class XII CBSE Mathematics Sample question paper with solutionClass XII CBSE Mathematics Sample question paper with solution
Class XII CBSE Mathematics Sample question paper with solution
 
Shi20396 ch05
Shi20396 ch05Shi20396 ch05
Shi20396 ch05
 
mathematics question bank for engineering students
mathematics question bank for engineering studentsmathematics question bank for engineering students
mathematics question bank for engineering students
 
Turning Point of the Pokemon Battle. pptx
Turning Point of the  Pokemon Battle. pptxTurning Point of the  Pokemon Battle. pptx
Turning Point of the Pokemon Battle. pptx
 
Maths 301 key_sem_1_2007_2008
Maths 301 key_sem_1_2007_2008Maths 301 key_sem_1_2007_2008
Maths 301 key_sem_1_2007_2008
 
MAT 2B SR DI M01 INTRO(26 May 2016).ppt
MAT 2B SR DI M01 INTRO(26 May 2016).pptMAT 2B SR DI M01 INTRO(26 May 2016).ppt
MAT 2B SR DI M01 INTRO(26 May 2016).ppt
 
Calculus B Notes (Notre Dame)
Calculus B Notes (Notre Dame)Calculus B Notes (Notre Dame)
Calculus B Notes (Notre Dame)
 
Solutions Manual for Calculus Early Transcendentals 10th Edition by Anton
Solutions Manual for Calculus Early Transcendentals 10th Edition by AntonSolutions Manual for Calculus Early Transcendentals 10th Edition by Anton
Solutions Manual for Calculus Early Transcendentals 10th Edition by Anton
 

Plus de ashish bansal (14)

Data struters
Data strutersData struters
Data struters
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Cis435 week04
Cis435 week04Cis435 week04
Cis435 week04
 
Cis435 week03
Cis435 week03Cis435 week03
Cis435 week03
 
Cis435 week02
Cis435 week02Cis435 week02
Cis435 week02
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
35 algorithm-types
35 algorithm-types35 algorithm-types
35 algorithm-types
 
32 algorithm-types
32 algorithm-types32 algorithm-types
32 algorithm-types
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 
5 searching
5 searching5 searching
5 searching
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 

4 recursion

  • 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
  • 58. n=1 X n=2 n=3 Y A ? B ? C Output
  • 59. n=1 X n=2 n=3 Y A Y B X C Output
  • 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 )
  • 71. Time: T(1) = ≈ 2T(n-1) +3 T(n-1) + T(n-2) + 3T(n) = 11 T(2) = = 2Θ(n) defined?
  • 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
  • 78.
  • 79. Merge Sort Sort Time: T(n) = = Θ(n log(n)) 2T(n/2) + Θ(n)
  • 81. 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 ≤
  • 82. 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.
  • 83. 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
  • 84.
  • 85. 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
  • 86. 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.
  • 87. Quick Sort Best Time: Worst Time: Expected Time: T(n) = 2T(n/2) + Θ(n) = Θ(n log(n))
  • 88. Quick Sort T(n) = 2T(n/2) + Θ(n) = Θ(n log(n)) Best Time: Worst Time: Expected Time: = Θ(n2 ) T(n) = T(0) + T(n-1) + Θ(n)
  • 89. Quick Sort T(n) = 2T(n/2) + Θ(n) = Θ(n log(n)) Best Time: T(n) = T(0) + T(n-1) + Θ(n)Worst Time: Expected Time: = Θ(n2 ) T(n) = T(1 /3n) + T(2 /3n) + Θ(n) = Θ(n log(n))
  • 90. Quick Sort Expected Time: T(n) = T(1 /3n) + T(2 /3n) + Θ(n) = Θ(n log(n)) Top work Θ(n) 2nd level 3rd level # levels = Θ(n) Θ(n) 1 /3 1 /3 1 /3 2 /3 2 /3 2 /3 2 /3 = Θ(log(n)) 3 /2 log (n)
  • 91. 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
  • 92. 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
  • 93. 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
  • 94. 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
  • 95.
  • 96. 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=
  • 97. 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:
  • 98. T(n) = = Θ(n) 1 T(n/2) + Θ(n) T(n) = 1 T(2 /3n) + Θ(n)Expected Time: = Θ(n) Kth Element Problem Specification T(n) = 1 T(n-1) + Θ(n)Worst Time: = Θ(n2 ) Best Time:
  • 99. Recursion on Trees 3 8 1 3 2 2 7 6 5 9 4 1
  • 100. Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left subtrees. 3 8 1 3 2 2 7 6 5 9 4 1 tree because
  • 101. Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left subtrees. 3 8 1 3 2 2 7 6 5 9 4 1 tree tree node
  • 102. Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left subtrees. 3 8 1 3 2 2 7 6 5 9 4 1 tree because
  • 103. Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left subtrees. 3 8 1 3 2 2 7 6 5 9 4 1 tree tree node
  • 104. Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left subtrees. 3 8 1 3 2 2 7 6 5 9 4 1 tree because
  • 105. Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left subtrees. 3 8 1 3 2 2 7 6 5 9 4 1 tree node tree
  • 106. Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left subtrees. 3 8 1 3 2 2 7 6 5 9 4 1tree because empty
  • 107. Recursion on Trees number of nodes = ? 3 8 1 3 2 2 7 6 5 9 4 1 6 5 Get help from friends
  • 108. Recursion on Trees number of nodes 3 8 1 3 2 2 7 6 5 9 4 1 = number on left + number on right + 1 = 6 + 5 + 1 = 12 6 5
  • 109. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 number of nodes 1 Are we done?
  • 110. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 number of nodes ?
  • 111. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 number of nodes 0 Better base case!
  • 112. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 number of nodes 1 Does this still need to be a base case?
  • 113. Recursion on Trees 3 8 1 3 2 2 7 6 5 9 4 1 00 number of nodes = number on left + number on right + 1 = 0 + 0 + 1 = 1 No!
  • 114. Recursion on Trees 3 8 1 3 2 2 7 6 5 9 4 1 00 Most programmers don’t use the empty tree as a base case. This causes a lot more work.
  • 116. Recursion on Trees 3 Designing Program/Test Cases generic generic 3 generic 3 0 00 0+0+1=1 n1 n2 n1 +n2 + 1 0n1 n1 + 1 Same code works! Same code works! Try same code Try same code
  • 117. Time: T(n) = = Θ(n) T(left) + T(right) + Θ(1) Recursion on Trees One stack frame for each node in the tree
  • 118. Recursion on Trees sum of nodes = ? 3 8 1 3 2 2 7 6 5 9 4 1 23 25 Get help from friends
  • 119. Recursion on Trees sum of nodes 3 8 1 3 2 2 7 6 5 9 4 1 = sum on left + sum on right + value at root = 23 + 25 + 3 = 51 23 25
  • 120. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 sum of nodes = ? 0
  • 122. Recursion on Trees 3 generic generic 3 generic 3 0 s1 s2 s1 +s2 + 3 0s1 s1 + 3 00 0+0+ 3 =3 Designing Program/Test Cases
  • 123. Recursion on Trees max of nodes = ? 3 8 1 3 2 2 7 6 5 9 4 1 8 9 Get help from friends
  • 124. Recursion on Trees max of nodes 3 8 1 3 2 2 7 6 5 9 4 1 = max( max on left, max on right, value at root) = max( 8, 9, 3) = 9 8 9
  • 125. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 max of nodes = ? ?
  • 126. 3+4+2+8+… Sum so far is 17 Sum so far is 0 Recursion on Trees NewSum = OldSum + nextElement = 0 + 3 = 3
  • 127. 3*4*2*3*… Product so far is 72 Product so far is 1 Recursion on Trees NewProd = OldProd × nextElement = 1 × 3 = 3
  • 128. Max(3,4,2,3,… Max so far is 4 Max so far is -∞ Recursion on Trees NewMax = max(OldMax, next Element) = max( -∞ , 3 ) = 3
  • 130. Recursion on Trees height of tree = ? 3 8 1 3 2 2 7 6 5 9 4 1 3 4 Get help from friends
  • 131. Recursion on Trees height of tree 3 8 1 3 2 2 7 6 5 9 4 1 = max(height of left,height on right) + 1 = max(3,4) + 1 = 4 3 4
  • 132. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 height of tree ?
  • 133. Recursion on Trees 3 8 1 3 2 2 7 6 5 9 4 1 height of tree? 2 nodes or 1 edge 0 nodes ? edges Base Case ?
  • 134. Recursion on Trees 3 8 1 3 2 2 7 6 5 9 4 1 Work it out backwards ? edges height = = max(height of left,height on right) + 1 = max(?,?) + 1 ?? 0 edge
  • 135. Recursion on Trees 3 8 1 3 2 2 7 6 5 9 4 1 Work it out backwards -1edges height = = max(height of left,height on right) + 1 = max(-1,-1) + 1 -1-1 0 edge
  • 137. Recursion on Trees number of leaves = ? 3 8 1 3 2 2 7 6 5 9 4 1 3 2 Get help from friends
  • 138. Recursion on Trees number of leaves 3 8 1 3 2 2 7 6 5 9 4 1 = number on left + number on right = 3 + 2 = 5 3 2
  • 139. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 number of leaves 0
  • 140. Recursion on Trees 3 generic generic 3 generic 3 0 L1 L2 L1 +L2 0L1 L1 00 0+0 = 0 Needs to be another base case. 1 No! Designing Program/Test Cases
  • 145. 38 25 17 4 21 31 28 35 51 42 40 49 63 55 71 Define Problem: Binary Search Tree • PreConditions – Key 25 – A binary search tree. – PostConditions – Find key in BST (if there).
  • 146. 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 ≤ ≤
  • 147. 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
  • 148. 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
  • 151. Time: T(n) = = Θ(n log(n)) 2T(n/2) + Θ( Recursion on Trees Computing max is too much work. n)
  • 152. Recursion on Trees Extra info from belowExtra info from below
  • 153. Time: T(n) = = Θ(n log(n)) 2T(n/2) + Θ( Recursion on Trees Computing max is too much work. n) 1 n
  • 154. Recursion on Trees Extra info from above -∞, ∞
  • 155. Heaps, Heap Sort, & Priority Queues
  • 156. 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?
  • 157. Heap Data Structure Completely Balanced Binary Tree Implemented by an Array
  • 158. Make Heap Get help from friends
  • 159. Heapify ? Maximum needs to be at root. Where is the maximum?
  • 160. Find the maximum. Put it in place ? Repeat Heapify
  • 162.
  • 163.
  • 164. Make Heap Get help from friends T(n) = 2T(n/2) + log(n) Running time: = Θ(n) Heapify Heap
  • 166. ? Heap
  • 167. ? Heap
  • 168. ?
  • 169. Heap
  • 172. 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
  • 173. Heap Sort Largest i values are sorted on side. Remaining values are in a heap. Exit 79 km 75 km Exit
  • 174. Heap Data Structure Heap Array 5 3 4 2 1 98 76 Heap Array
  • 175. Heap Sort Largest i values are sorted on side. Remaining values are in a heap Exit Put next value where it belongs. 79 km 75 km Exit Heap ?
  • 177. Heap Sort ? ? ? ? ? ? ? Sorted
  • 179. Recursion on Trees Evaluate Equation Tree = ? 12 7 Get help from friends
  • 180. Recursion on Trees Evaluate Equation Tree 12 7 = rootop( value on left, value on right ) = rootop(12,7) = 12 + 7 = 19
  • 181. Recursion on Trees Evaluate Equation Tree 7 Base Case ?
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190. Recursion on Trees Printing a Tree When I taught this one year, a student needed just this algorithm for his job.
  • 191. Recursion on Trees Typing line by line? Get help from friends
  • 192. Recursion on Trees One stack frame prints: His input gives: his tree whether left or right sting for each line
  • 193. Recursion on Trees He gives his friends: their trees whether left or right sting for each line
  • 194.
  • 195.
  • 196.
  • 197.
  • 204. Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=1 n=0
  • 205. Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=2 n=1
  • 206. Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=3 n=2
  • 207. Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=30
  • 208. Recursive Images if n=1if n=2if n=3if n=4if n=5 if n=0
  • 209. Recursive Images if n=1 if n=0 if n=2if n=3if n=4if n=5
  • 210. Recursive Images L(n) = 4 /3 L(n-1) ⇒ ∞= (4 /3)n
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217. 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.
  • 218. 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.
  • 219. 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.
  • 220. 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.
  • 221. 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.
  • 222. 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.
  • 223. 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.
  • 224. 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.
  • 225. Feed Back Thanks for the feedback that you have given me.
  • 226. End

Notes de l'éditeur

  1. 78yhy77uuuuuuuuuuu7
  2. 78yhy77uuuuuuuuuuu7