Upcoming SlideShare
×

# 4 recursion

253 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
253
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
5
0
Likes
0
Embeds 0
No embeds

No notes for slide
• 78yhy77uuuuuuuuuuu7
• 78yhy77uuuuuuuuuuu7
• ### 4 recursion

1. 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. 2. The Goal is to UNDERSTAND Recursive Algorithms Fundamentally to their core
3. 3. Representation: Understand the relationship between different representations of the same information or idea 1 2 3 Rudich www.discretemath.com
4. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 45. Friends & Strong Induction Recursive Algorithm: •Assume you have an algorithm that works. •Use it to write an algorithm that works.
46. 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. 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. 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. 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. 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. 51. Friends & Strong Induction Carefully write the specifications for the problem. Preconditions: Set of legal instances (inputs) Why? Postconditions: Required output
52. 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. 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. 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. 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. 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
57. 57. n=1 X n=2 Y Output
58. 58. n=1 X n=2 n=3 Y A ? B ? C Output
59. 59. n=1 X n=2 n=3 Y A Y B X C Output
60. 60. n=1 X n=2 n=3 Y AYBXC Output
61. 61. n=1 X n=2 n=3 n=4 Y AYBXC A ? B ? C Output
62. 62. n=1 X n=2 n=3 n=4 Y AYBXC A AYBXC B Y C Output
63. 63. n=1 X n=2 n=3 n=4 Y AYBXC AAYBXCBYC Output
64. 64. n=1 X n=2 n=3 n=4 n=5 Y AYBXC AAYBXCBYC A ? B ? C Output
65. 65. n=1 X n=2 n=3 n=4 n=5 Y AYBXC AAYBXCBYC A AAYBXCBYC B AYBXC C Output
66. 66. n=1 X n=2 n=3 n=4 n=5 Y AYBXC AAYBXCBYC AAAYBXCBYCBAYBXCC Output
67. 67. Towers of Hanoi
68. 68. Towers of Hanoi
69. 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. 70. Towers of Hanoi Time: T(1)=1, T(n) = 2T(n-1) + 1 = Θ(2n )
71. 71. Time: T(1) = ≈ 2T(n-1) +3 T(n-1) + T(n-2) + 3T(n) = 11 T(2) = = 2Θ(n) defined?
72. 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. 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. 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
75. 75. Merge Sort 88 14 9825 62 52 79 30 23 31 Divide and Conquer
76. 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. 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. 78. Merge Sort Sort Time: T(n) = = Θ(n log(n)) 2T(n/2) + Θ(n)
79. 79. Quick Sort 88 14 9825 62 52 79 30 23 31 Divide and Conquer
80. 80. Quick Sort 88 14 9825 62 52 79 30 23 31 Partition set into two using randomly chosen pivot 14 25 30 2331 88 98 62 79 ≤ 52 ≤
81. 81. Quick Sort 14 25 30 2331 88 98 62 79 ≤ 52 ≤ 14,23,25,30,31 Get one friend to sort the first half. 62,79,98,88 Get one friend to sort the second half.
82. 82. Quick Sort 14,23,25,30,31 62,79,98,88 52 Glue pieces together. (No real work) 14,23,25,30,31,52,62,79,88,98
83. 83. Quick Sort 88 14 9825 62 52 79 30 23 31 Let pivot be the first element in the list? 14 25 30 23 88 98 62 79 ≤ 31 ≤ 52
84. 84. Quick Sort ≤ 14 ≤ 14,23,25,30,31,52,62,79,88,98 23,25,30,31,52,62,79,88,98 If the list is already sorted, then the slit is worst case unbalanced.
85. 85. Quick Sort Best Time: Worst Time: Expected Time: T(n) = 2T(n/2) + Θ(n) = Θ(n log(n))
86. 86. 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)
87. 87. 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))
88. 88. 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)
89. 89. Kth Element Problem Specification • Precondition: The input is a list of n values and an integer k. • Post condition: The kth smallest in the list. (or the first k smallest) 88 14 9825 62 52 79 30 23 31 14,23,25,30,31,52,62,79,88,98 k=3 Output: 25
90. 90. 88 14 9825 62 52 79 30 23 31 Partition set into two using randomly chosen pivot 14 25 30 2331 88 98 62 79 ≤ 52 ≤ Kth Element Problem Specification
91. 91. 14 25 30 2331 88 98 62 79 ≤ 52 ≤ Kth Element Problem Specification k=3 r=5 elements on left 14,23,25,30,31 Get one friend to find k=3rd element in first half. Output: 25
92. 92. 14 25 30 2331 88 98 62 79 ≤ 52 ≤ Kth Element Problem Specification k=8 r=5 elements on left Get one friend to find k=8-5=3rd element in second half. Output: 79 52,62,79,98,88
93. 93. T(n) = Kth Element Problem Specification = Θ(n) 1 T(n/2) + Θ(n)Best Time: Expected Time: Worst Time: n log a /log b # of base cases = = n0 = 1n log 1 /log 2=
94. 94. T(n) = = Θ(n) 1 T(n/2) + Θ(n) Expected Time: Kth Element Problem Specification T(n) = 1 T(n-1) + Θ(n)Worst Time: = Θ(n2 ) Best Time:
95. 95. 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:
96. 96. Recursion on Trees 3 8 1 3 2 2 7 6 5 9 4 1
97. 97. 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
98. 98. 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
99. 99. 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
100. 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 tree node
101. 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 because
102. 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 node tree
103. 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 1tree because empty
104. 104. Recursion on Trees number of nodes = ? 3 8 1 3 2 2 7 6 5 9 4 1 6 5 Get help from friends
105. 105. 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
106. 106. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 number of nodes 1 Are we done?
107. 107. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 number of nodes ?
108. 108. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 number of nodes 0 Better base case!
109. 109. 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?
110. 110. 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!
111. 111. 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.
112. 112. Recursion on Trees
113. 113. 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
114. 114. Time: T(n) = = Θ(n) T(left) + T(right) + Θ(1) Recursion on Trees One stack frame for each node in the tree
115. 115. Recursion on Trees sum of nodes = ? 3 8 1 3 2 2 7 6 5 9 4 1 23 25 Get help from friends
116. 116. 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
117. 117. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 sum of nodes = ? 0
118. 118. Recursion on Trees
119. 119. 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
120. 120. Recursion on Trees max of nodes = ? 3 8 1 3 2 2 7 6 5 9 4 1 8 9 Get help from friends
121. 121. 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
122. 122. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 max of nodes = ? ?
123. 123. 3+4+2+8+… Sum so far is 17 Sum so far is 0 Recursion on Trees NewSum = OldSum + nextElement = 0 + 3 = 3
124. 124. 3*4*2*3*… Product so far is 72 Product so far is 1 Recursion on Trees NewProd = OldProd × nextElement = 1 × 3 = 3
125. 125. 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
126. 126. Recursion on Trees
127. 127. Recursion on Trees height of tree = ? 3 8 1 3 2 2 7 6 5 9 4 1 3 4 Get help from friends
128. 128. 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
129. 129. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 height of tree ?
130. 130. 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 ?
131. 131. 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
132. 132. 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
133. 133. Recursion on Trees
134. 134. Recursion on Trees number of leaves = ? 3 8 1 3 2 2 7 6 5 9 4 1 3 2 Get help from friends
135. 135. 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
136. 136. Recursion on Trees Base Case ? 3 8 1 3 2 2 7 6 5 9 4 1 number of leaves 0
137. 137. 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
138. 138. Recursion on Trees
139. 139. Recursion on Trees
140. 140. Recursion on Trees
141. 141. Recursion on Trees ? Drops the tree.
142. 142. 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).
143. 143. 38 25 17 4 21 31 28 35 51 42 40 49 63 55 71 Binary Search Tree Its left children ≤ Any node ≤ Its right children ≤ ≤
144. 144. Define Loop Invariant • Maintain a sub-tree. • If the key is contained in the original tree, then the key is contained in the sub-tree. key 17 38 25 17 4 21 31 28 35 51 42 40 49 63 55 71
145. 145. Define Step • Cut sub-tree in half. • Determine which half the key would be in. • Keep that half. key 17 38 25 17 4 21 31 28 35 51 42 40 49 63 55 71 If key < root, then key is in left half. If key > root, then key is in right half. If key = root, then key is found
146. 146. Recursion on Trees
147. 147. Recursion on Trees
148. 148. Time: T(n) = = Θ(n log(n)) 2T(n/2) + Θ( Recursion on Trees Computing max is too much work. n)
149. 149. Recursion on Trees Extra info from belowExtra info from below
150. 150. Time: T(n) = = Θ(n log(n)) 2T(n/2) + Θ( Recursion on Trees Computing max is too much work. n) 1 n
151. 151. Recursion on Trees Extra info from above -∞, ∞
152. 152. Heaps, Heap Sort, & Priority Queues
153. 153. Heap Definition •Completely Balanced Binary Tree •The value of each node ≥ each of the node's children. •Left or right child could be larger. Where can 1 go? Maximum is at root. Where can 8 go? Where can 9 go?
154. 154. Heap Data Structure Completely Balanced Binary Tree Implemented by an Array
155. 155. Make Heap Get help from friends
156. 156. Heapify ? Maximum needs to be at root. Where is the maximum?
157. 157. Find the maximum. Put it in place ? Repeat Heapify
158. 158. Heap Running Time: Heapify
159. 159. Make Heap Get help from friends T(n) = 2T(n/2) + log(n) Running time: = Θ(n) Heapify Heap
160. 160. Heaps Heap ? Another algorithm
161. 161. ? Heap
162. 162. ? Heap
163. 163. ?
164. 164. Heap
165. 165. Running Time: i log(n) -i 2log(n) -i
166. 166. Priority Queues
167. 167. Insertion Sort Largest i values are sorted on side. Remaining values are off to side. 6,7,8,9 < 3 4 1 5 2 Exit 79 km 75 km Exit Max is easier to find if a heap. Insertion
168. 168. Heap Sort Largest i values are sorted on side. Remaining values are in a heap. Exit 79 km 75 km Exit
169. 169. Heap Data Structure Heap Array 5 3 4 2 1 98 76 Heap Array
170. 170. 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 ?
171. 171. Heap Sort
172. 172. Heap Sort ? ? ? ? ? ? ? Sorted
173. 173. Heap Sort Running Time:
174. 174. Recursion on Trees Evaluate Equation Tree = ? 12 7 Get help from friends
175. 175. Recursion on Trees Evaluate Equation Tree 12 7 = rootop( value on left, value on right ) = rootop(12,7) = 12 + 7 = 19
176. 176. Recursion on Trees Evaluate Equation Tree 7 Base Case ?
177. 177. Recursion on Trees
178. 178. Derivatives
179. 179. Recursion on Trees Printing a Tree When I taught this one year, a student needed just this algorithm for his job.
180. 180. Recursion on Trees Typing line by line? Get help from friends
181. 181. Recursion on Trees One stack frame prints: His input gives: his tree whether left or right sting for each line
182. 182. Recursion on Trees He gives his friends: their trees whether left or right sting for each line
183. 183. Parsing (1×x-x×1/(x×x)×x-(x/x)×1)/(x×x) Input: Output:
184. 184. Parsing
185. 185. Parsing
186. 186. Parsing
187. 187. Parsing
188. 188. Parsing
189. 189. Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=1 n=0
190. 190. Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=2 n=1
191. 191. Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=3 n=2
192. 192. Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=30
193. 193. Recursive Images if n=1if n=2if n=3if n=4if n=5 if n=0
194. 194. Recursive Images if n=1 if n=0 if n=2if n=3if n=4if n=5
195. 195. Recursive Images L(n) = 4 /3 L(n-1) ⇒ ∞= (4 /3)n
196. 196. Please feel free to ask questions! Please give me feedback so that I can better serve you. Thanks for the feedback that you have given me already.
197. 197. It is too hard. Feed Back • Yes it is. • Are you reading? • Are you thinking about it on your own? • Are you talking to friends? • I have taught both harder and easier. • Then students work harder and less, • learn more material or less material. • And say it is hard the same.
198. 198. Other classes I can just show up without working between classes and follow. This should be like that. Feed Back No. I facilitate learning. Not spoon feed.
199. 199. It is too abstract. . = •I hope that you will see the simplicity and the beauty in it. Feed Back •Yes it is. •My goal is to teach you to think abstractly.
200. 200. Could you trace out the algorithms on examples? Feed Back •We don’t really have time. •This would be good for you to do on your own. •I do try to, when I think it will help.
201. 201. Could you give more details of the code? Feed Back •It is not a coding course. •I want to give you the big picture not of one algorithm but algorithms in general. •It’s a trade off. One can get lost in the details.
202. 202. I can’t follow the lectures. •Are you reading before class. Feed Back •Ask questions. I am so lost I don’t know what to ask. •Tell me where you first got lost. •Ask me to slow down.
203. 203. Assignments are too hard. Feed Back Yes. I want you to think about new and hard things on your own. •Sometimes this is a mistake. •I want you to have thought about material before we teach it so that you appreciate it. Assignments need material not covered in class.
204. 204. Feed Back Thanks for the feedback that you have given me.
205. 205. End