4 recursion
-
Upload
bansal-ashish -
Category
Documents
-
view
83 -
download
2
Transcript of 4 recursion
Recursion
Jeff Edmonds
York UniversityCOSC 3101LectureLecture
44
CodeStack of Stack FramesTree of Stack FramesFriends and Strong InductionTowers of HanoiMerge & Quick SortSimple Recursion on TreesGeneralizing the ProblemHeap Sort & Priority QueuesTrees Representing Equations Pretty PrintParsingRecursive ImagesFeedback
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 Representationsof 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 Representationsof 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 = 2133Y = 2312ac = 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 = 2133Y = 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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23ac = 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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23
ac = ?bd = (a+b)(c+d) = XY =
X = 2Y = 2XY=
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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23
ac = ?bd = (a+b)(c+d) = XY =
X = 2Y = 2XY = 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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23ac = 4bd = (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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23ac = 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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23ac = 4
bd = ?(a+b)(c+d) = XY =
X = 1Y = 3XY = 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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23ac = 4bd = 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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23ac = 4bd = 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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23ac = 4bd = 3
(a+b)(c+d) = ?XY =
X = 3Y = 5XY = 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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23ac = 4bd = 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 = 2133Y = 2312
ac = ?bd = (a+b)(c+d) = XY =
X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 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 = 2133Y = 2312ac = 483bd = (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 = 2133Y = 2312ac = 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 = 2133Y = 2312ac = 483
bd = ?(a+b)(c+d) = XY =
X = 33Y = 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 = 2133Y = 2312ac = 483
bd = ?(a+b)(c+d) = XY =
X = 33Y = 12
ac = ?bd = (a+b)(c+d) = XY = 15
X = 3Y = 1XY = 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 = 2133Y = 2312ac = 483
bd = ?(a+b)(c+d) = XY =
X = 33Y = 12ac = 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 = 2133Y = 2312ac = 483
bd = ?(a+b)(c+d) = XY =
X = 33Y = 12ac = 3
bd = ?(a+b)(c+d) = XY = 15
X = 3Y = 2XY = 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 = 2133Y = 2312ac = 483
bd = ?(a+b)(c+d) = XY =
X = 33Y = 12ac = 3bd = 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 = 2133Y = 2312ac = 483
bd = ?(a+b)(c+d) = XY =
X = 33Y = 12ac = 3bd = 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 Representationsof 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 = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496
X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483
X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396
X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890
X = 2Y = 2XY=4
X = 1Y = 3XY=3
X = 3Y = 5XY=15
X = 3Y = 1XY=3
X = 3Y = 2XY=6
X = 6Y = 3XY=18
X = 5Y = 3XY=15
X = 4Y = 5XY=20
X = 9Y = 8XY=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 Representationsof 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 = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496
X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483
X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396
X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890
X = 2Y = 2XY=4
X = 1Y = 3XY=3
X = 3Y = 5XY=15
X = 3Y = 1XY=3
X = 3Y = 2XY=6
X = 6Y = 3XY=18
X = 5Y = 3XY=15
X = 4Y = 5XY=20
X = 9Y = 8XY=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 InductionOne Friend for each
stack frame.
Each worries only about his job.
X = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496
X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483
X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396
X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890
X = 2Y = 2XY=4
X = 1Y = 3XY=3
X = 3Y = 5XY=15
X = 3Y = 1XY=3
X = 3Y = 2XY=6
X = 6Y = 3XY=18
X = 5Y = 3XY=15
X = 4Y = 5XY=20
X = 9Y = 8XY=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 InductionWorry about one
step at a time.Imagine that you are one specific
friend.
X = 33Y = 12ac = 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 = 33Y = 12ac = ?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 = 3Y = 1XY=?
X = 3Y = 2XY=?
X = 6Y = 3XY=?
X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY =
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 = 3Y = 1XY=3
X = 3Y = 2XY=6
X = 6Y = 3XY=18
X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 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 = 3Y = 1XY=3
X = 3Y = 2XY=6
X = 6Y = 3XY=18
X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 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 = 1XY=3
X = 3Y = 2XY=6
X = 6Y = 3XY=18
X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 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 = 3Y = 1XY=3
X = 3Y = 2XY=6
X = 6Y = 3XY=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 InductionConsider 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 = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496
X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483
X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396
X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890
X = 2Y = 2XY=4
X = 1Y = 3XY=3
X = 3Y = 5XY=15
X = 3Y = 1XY=3
X = 3Y = 2XY=6
X = 6Y = 3XY=18
X = 5Y = 3XY=15
X = 4Y = 5XY=20
X = 9Y = 8XY=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 InductionThis solves the
problem for every possible instance.
Friends & Strong InductionRecursive Algorithm:
•Assume you have an algorithm that works.•Use it to write an algorithm that works.
Friends & Strong InductionRecursive 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 InductionRecursive Algorithm:
•Assume you have an algorithm that works.•Use it to write an algorithm that works.
To get into my houseI must get the key from a smaller house
X = 33Y = 12ac = ?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 = 3Y = 1XY=?
X = 3Y = 2XY=?
X = 6Y = 3XY=?
Each subinstance must be a smaller instance
to the same problem.
Friends & Strong InductionRecursive 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 InductionCarefully write the specifications for the problem.
Preconditions: Set of legal instances (inputs)
Why?
Postconditions: Required output
Friends & Strong InductionCarefully 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
nS n
( )
, [ ( ) , ( ) , ( ) , . . . , ( ) ( )]
( )
0
0 1 2 1
S
iS i S i
nS n
( )
( ) ( )
( )
0
1
Strong Induction
Friends & Strong Induction
S
i S S S S i S i
nS 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 instanceto 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
YA ? B ? C
Output
n=1 X
n=2
n=3
YA Y B X C
Output
n=1 X
n=2
n=3
YAYBXC
Output
n=1 X
n=2
n=3
n=4
YAYBXC
A ? B ? C
Output
n=1 X
n=2
n=3
n=4
YAYBXC
A AYBXC B Y C
Output
n=1 X
n=2
n=3
n=4
YAYBXC
AAYBXCBYC
Output
n=1 X
n=2
n=3
n=4
n=5
YAYBXC
AAYBXCBYC
A ? B ? C
Output
n=1 X
n=2
n=3
n=4
n=5
YAYBXC
AAYBXCBYC
A AAYBXCBYC B AYBXC C
Output
n=1 X
n=2
n=3
n=4
n=5
YAYBXC
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) +3T(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 149825
62
52
79
3023
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 splittingMinimal 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 149825
62
52
79
3023
31
Divide and Conquer
Merge Sort
88 149825
62
52
79
3023
31Split 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 149825
62
52
79
3023
31
Divide and Conquer
Quick Sort
88 149825
62
52
79
3023
31
Partition set into two using randomly chosen pivot
14
2530
23
31
88 9862
79≤ 52 ≤
Quick Sort
14
2530
23
31
88 9862
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 149825
62
52
79
3023
31
Let pivot be the first element in the list?
14
2530
23
88 9862
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 SortExpected Time: T(n) = T(1/3n) + T(2/3n) + (n)
= (n log(n))
Top work (n)
2nd level3rd level
# levels =
(n)(n)
1/31/31/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 149825
62
52
79
3023
31
14,23,25,30,31,52,62,79,88,98
k=3
Output: 25
88 149825
62
52
79
3023
31
Partition set into two using randomly chosen pivot
14
2530
23
31
88 9862
79≤ 52 ≤
Kth Element Problem Specification
14
2530
23
31
88 9862
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
2530
23
31
88 9862
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 TreesA 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 TreesA 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
treetree
node
Recursion on TreesA 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 TreesA 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 TreesA 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 TreesA 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
nodetree
Recursion on TreesA 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 becauseempty
Recursion on Treesnumber of nodes = ?
3
8
1
3 2
2
7
6
5
9
4
1
65
Get help from friends
Recursion on Treesnumber 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
65
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 Treessum of nodes = ?
3
8
1
3 2
2
7
6
5
9
4
1
2325
Get help from friends
Recursion on Treessum 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
2325
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 Treesmax of nodes = ?
3
8
1
3 2
2
7
6
5
9
4
1
89
Get help from friends
Recursion on Treesmax 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
89
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 Treesheight of tree = ?
3
8
1
3 2
2
7
6
5
9
4
1
34
Get help from friends
Recursion on Treesheight 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
34
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 Treesnumber of leaves = ?
3
8
1
3 2
2
7
6
5
9
4
1
32
Get help from friends
Recursion on Treesnumber of leaves
3
8
1
3 2
2
7
6
5
9
4
1
= number on left + number on right= 3 + 2 = 5
32
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 StructureCompletely 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) -i2log(n) -i
Priority Queues
Insertion Sort
Largest i values are sorted on side.Remaining values are off to side.
6,7,8,9<3
415
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 SortLargest i values are sorted on side.Remaining values arein 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 TreesEvaluate Equation Tree = ?
127
Get help from friends
Recursion on TreesEvaluate Equation Tree
127
= rootop( value on left, value on right )= rootop(12,7) = 12 + 7 = 19
Recursion on TreesEvaluate Equation Tree
7
Base Case ?
Recursion on Trees
Derivatives
Recursion on TreesPrinting 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 treewhether left or rightsting for each line
Recursion on Trees
He gives his friends:their treeswhether left or rightsting 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 recursivelyDraw here with n-1
if n=1
n=0
Recursive Images
if n=0, draw
else draw
And recursivelyDraw here with n-1
if n=2
n=1
Recursive Images
if n=0, draw
else draw
And recursivelyDraw here with n-1
if n=3
n=2
Recursive Images
if n=0, draw
else draw
And recursivelyDraw 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