Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to Think about Algorithms

Similar presentations


Presentation on theme: "How to Think about Algorithms"— Presentation transcript:

1 How to Think about Algorithms
Recursion Code Stack of Stack Frames Tree of Stack Frames Friends and Strong Induction Towers of Hanoi Check List Merge & Quick Sort Simple Recursion on Trees Generalizing the Problem Things not to do Heap Sort & Priority Queues Trees Representing Equations Pretty Print Parsing Recursive Images

2 Divide And Conquer (an approach to faster algorithms)
DIVIDE my instance to the problem into smaller instances to the same problem. Have a friend (recursively) solve them. Do not worry about it yourself. GLUE the answers together so as to obtain the answer to your larger instance.

3 Different Representations of Recursive Algorithms
Views Pros 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.

4 Code Representation of an Algorithm
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 Pros and Cons?

5 Code Representation of an Algorithm
Pros: Cons: Runs on computers Precise and succinct Perception that being able to code is the only thing needed to get a job. (false) I am not a computer I need a higher level of intuition. Prone to bugs Language dependent

6 Different Representations of Recursive Algorithms
Views Pros 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.

7 Stack of Stack Frames 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 X = 2133 Y = 2312 ac = bd = (a+b)(c+d) = XY = Stack Frame: A particular execution of one routine on one particular input instance.

8 Stack of Stack Frames 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 X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY =

9 Stack of Stack Frames 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 X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = bd = (a+b)(c+d) = XY =

10 Stack of Stack Frames 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 X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = ? bd = (a+b)(c+d) = XY =

11 Stack of Stack Frames 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 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=

12 Stack of Stack Frames 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 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

13 Stack of Stack Frames 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 X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 bd = (a+b)(c+d) = XY =

14 Stack of Stack Frames 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 X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 bd = ? (a+b)(c+d) = XY =

15 Stack of Stack Frames 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 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

16 Stack of Stack Frames 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 X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 bd = 3 (a+b)(c+d) = XY =

17 Stack of Stack Frames 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 X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 bd = 3 (a+b)(c+d) = ? XY =

18 Stack of Stack Frames 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 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

19 Stack of Stack Frames 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 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 = ?

20 Stack of Stack Frames 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 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

21 Stack of Stack Frames X = 2133 Y = 2312 ac = 483 bd = (a+b)(c+d) =
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 X = 2133 Y = 2312 ac = 483 bd = (a+b)(c+d) = XY =

22 Stack of Stack Frames X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) =
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 X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY =

23 Stack of Stack Frames X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) =
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 X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY = X = 33 Y = 12 ac = ? bd = (a+b)(c+d) = XY =

24 Stack of Stack Frames X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) =
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 X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY = X = 33 Y = 12 ac = ? bd = (a+b)(c+d) = XY = X = 3 Y = 1 XY = 3

25 Stack of Stack Frames X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) =
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 X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY = X = 33 Y = 12 ac = 3 bd = ? (a+b)(c+d) = XY =

26 Stack of Stack Frames X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) =
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 X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY = X = 33 Y = 12 ac = 3 bd = ? (a+b)(c+d) = XY = X = 3 Y = 2 XY = 6

27 Stack of Stack Frames An so on …. X = 2133 Y = 2312 ac = 483 bd = ?
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 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 = An so on ….

28 Stack of Stack Frames Representation of an Algorithm
Pros: Cons: 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.

29 Different Representations of Recursive Algorithms
Views Pros 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.

30 Tree of Stack Frames X = 2133 Y = 2312 ac = 483 bd = 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 X = 2133 Y = 2312 ac = 483 bd = 396 (a+b)(c+d) = 1890 XY = 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

31 Stack of Stack Frames Representation of an Algorithm
Pros: Cons: 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.

32 Different Representations of Recursive Algorithms
Views Pros 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.

33 Friends & Strong Induction
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 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 = 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

34 Friends & Strong Induction
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 Worry about one step at a time. Imagine that you are one specific friend. X = 2133 Y = 2312 ac = 483 bd = 396 (a+b)(c+d) = 1890 XY = 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

35 Friends & Strong Induction
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 Consider your input instance X = 33 Y = 12 ac = bd = (a+b)(c+d) = XY =

36 Friends & Strong Induction
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 Consider your input instance Allocate work Construct one or more sub-instances X = 33 Y = 12 ac = ? bd = ? (a+b)(c+d) = XY = X = 3 Y = 1 XY = ? X = 3 Y = 2 XY = ? X = 6 Y = 3 XY = ?

37 Friends & Strong Induction
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 Consider your input instance Allocate work Construct one or more sub-instances Assume by magic your friends give you the answer for these. X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = X = 3 Y = 1 XY = 3 X = 3 Y = 2 XY = 6 X = 6 Y = 3 XY = 18

38 Friends & Strong Induction
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 Consider your input instance Allocate work Construct one or more sub-instances Assume by magic your friends give you the answer for these. Use this help to solve your own instance. X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 396 X = 3 Y = 1 XY = 3 X = 3 Y = 2 XY = 6 X = 6 Y = 3 XY = 18

39 Friends & Strong Induction
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 Consider your input instance Allocate work Construct one or more sub-instances 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 = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 396 X = 3 Y = 1 XY = 3 X = 3 Y = 2 XY = 6 X = 6 Y = 3 XY = 18

40 Friends & Strong Induction
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 This technique is often referred to as Divide and Conquer X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 396 X = 3 Y = 1 XY = 3 X = 3 Y = 2 XY = 6 X = 6 Y = 3 XY = 18

41 Friends & Strong Induction
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 Consider generic instances. 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. ac bd (a+b)(c+d)

42 Friends & Strong Induction
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 This solves the problem for every possible instance. X = 2133 Y = 2312 ac = 483 bd = 396 (a+b)(c+d) = 1890 XY = 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

43 Friends & Strong Induction
Recursive Algorithm: Assume you have an algorithm that works. Use it to write an algorithm that works.

44 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!

45 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

46 Friends & Strong Induction
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 Allocate work Construct one or more sub-instances X = 33 Y = 12 ac = ? bd = ? (a+b)(c+d) = XY = Each sub-instance must be a smaller instance to the same problem. X = 3 Y = 1 XY = ? X = 3 Y = 2 XY = ? X = 6 Y = 3 XY = ?

47 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.

48 Friends & Strong Induction
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 MULT(X,Y): If |X| = |Y| = 1 then RETURN XY Use brute force to solve the base case instances.

49 Friends & Strong Induction
Carefully write the specifications for the problem. <preCond>: Set of legal instances (inputs) Why? <postCond>: Required output 78yhy77uuuuuuuuuuu7

50 Friends & Strong Induction
Carefully write the specifications for the problem. <preCond>: 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. <postCond>: Required output what is expected of us. what we can expect from our friend. 78yhy77uuuuuuuuuuu7 Related to Loop Invariants

51 Induction Strong Induction

52 Friends & Strong Induction
Induction Hypothesis: ``The recursive algorithm works for every instance of size n.'' Base case ``The algorithm works for every instance of any size.'' size i

53 Friends & Strong Induction Representation of an Algorithm
Pros: Cons: 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. You expect too much from your friends. Each sub-instance must be a smaller instance to the same problem. You expect too much from your boss. You only know your instance. You do not know your boss’s instance.

54 Try explaining what this algorithm does by tracing it out.
Output Try explaining what this algorithm does by tracing it out. It is much easier to TAKE ONE STEP AT A TIME

55 Output n=1 X n=2 Y

56 Output n=1 X n=2 Y n=3 A ? B ? C

57 Output n=1 X n=2 Y n=3 A Y B X C

58 Output n=1 X n=2 Y n=3 AYBXC

59 Output n=1 X n=2 Y n=3 AYBXC n=4 A ? B ? C

60 Output n=1 X n=2 Y n=3 AYBXC n=4 A AYBXC B Y C

61 Output n=1 X n=2 Y n=3 AYBXC n=4 AAYBXCBYC

62 Output n=1 X n=2 Y n=3 AYBXC n=4 AAYBXCBYC n=5 A ? B ? C

63 Output n=1 X n=2 Y n=3 AYBXC n=4 AAYBXCBYC n=5 AAAYBXCBYCBAYBXCC

64 Output n=1 X n=2 Y n=3 AYBXC n=4 AAYBXCBYC n=5 AAAYBXCBYCBAYBXCC

65 Time: T(1) = T(2) = 1 1 T(n) = T(n-1) + T(n-2) + 3  2T(n-1) +3 = 2Q(n) defined?

66 This job of mine is a bit daunting.
Towers of Hanoi This job of mine is a bit daunting. Where do I start? And I am lazy.

67 At some point, the biggest disk moves.
Towers of Hanoi At some point, the biggest disk moves. I will do that job.

68 To do this, the other disks must be in the middle.
Towers of Hanoi To do this, the other disks must be in the middle.

69 Towers of Hanoi How will these move? I will get a friend to do it.
And another to move these. I only move the big disk.

70 Towers of Hanoi

71 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.

72 Towers of Hanoi Time: T(1) = 1, T(n) = ≈ 2(2T(n-2)) ≈ 4(2T(n-3))
≈ 2i T(n-i) ≈ 2n

73 Evaluating: T(n) = aT(n-b)+f(n)
2 1 Level Instance size Work in stack frame # stack frames Work in Level n T(0) f(n-ib) f(n-2b) f(n-b) f(n) a ai a2 1 n/b a · T(0) ai · f(n-ib) a2 · f(n-2b) a · f(n-b) 1 · f(n) n/b n-b n-hb n-ib n-2b h = ? h = n/b Exponential Likely dominated by base cases |base case| = 0 = n-hb h = n/b

74 Evaluating: T(n) = 1T(n-b)+f(n)
2 1 Level Instance size Work in stack frame # stack frames Work in Level n-b n n-hb n-ib n-2b T(0) f(n-ib) f(n-2b) f(n-b) f(n) h = n/b 1 f(0) f(n-ib) f(n-2b) f(n-b) f(n) h = ? Total Work T(n) = ∑i=0..h f(b·i) = θ(f(n)) or θ(n·f(n))

75 Evaluating: T(n) = aT(n/b)+f(n)

76 Check Lists for Recursive Programs
This is the format of “all” recursive programs. Don’t deviate from this. Or else!

77 Check Lists for Recursive Programs
This is input instance is your mission. You must return this output. (An x, y, & z in every computation path.) Focus on your mission.

78 Check Lists for Recursive Programs
The <preCond> & <postCond> must document these variables and what you must do.

79 Check Lists for Recursive Programs
To get help, you construct an instance for each friend It must be valid and smaller. And pass them to them.

80 Check Lists for Recursive Programs
Each friend returns a solution his sub-instance. Trust him to do this. But don’t expect him to do or know anything else.

81 Check Lists for Recursive Programs
Combine their solutions to construct a solution to your instance. Return your solution.

82 Check Lists for Recursive Programs
If your solution is too small to get solved by the general technique solve it yourself. Be sure you handle every legal instance.

83 Check Lists for Recursive Programs
You rarely need to change the values of these variable beyond their initial setting (this is not an iterative algorithm.) Your rarely need additional input, output, or local variable. But it you do document them.

84 Check Lists for Recursive Programs
Have NO global variables. If you change the value of a local variable n, neither your friends nor your boss get that value.

85 Sorting Problem Specification
<preCond>: The input is a list of n values with the same value possibly repeated. <postCond>: The output is a list consisting of the same n values in non-decreasing order. 88 14 98 25 62 52 79 30 23 31 14,23,25,30,31,52,62,79,88,98

86 Recursive Sorts Given list of objects to be sorted
Split the list into two sub-lists. Recursively have a friend sort the two sub-lists. Combine the two sorted sub-lists into one entirely sorted list.

87 Four Recursive Sorts Size of Sub-lists n-1,1 n/2,n/2 Merge Sort
Insertion Sort Quick Sort Selection Sort Minimal effort splitting Lots of effort recombining Lots of effort splitting Minimal effort recombining

88 Merge Sort 88 14 98 25 62 52 79 30 23 31 Divide and Conquer

89 Merge Sort (no real work) Split Set into Two
88 14 98 25 62 52 79 30 23 31 (no real work) Split Set into Two 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.

90 Merge Sort Merge two sorted lists into one 25,31,52,88,98
14,23,25,30,31,52,62,79,88,98 14,23,30,62,79

91

92 Total work at any level = Q(n)
Merge Sort Sort Time: T(n) = 2T(n/2) + Q(n) = Q(n log(n)) Total work at any level = Q(n) # levels = Q(log(n))

93 Evaluating: T(n) = aT(n/b)+f(n)
2T(n/2) + Q(n) Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame # stack frames Work in Level n f(n) 1 1 · f(n) n/b f(n/b) a a · f(n/b) 2 n/b2 f(n/b2) a2 a2 · f(n/b2) i n/bi f(n/bi) ai ai · f(n/bi) h = log n/log b n/bh T(1) 2i·Q(n/2i) = Q(n) n log a/log b n · T(1) log a/log b All levels basically the same. Total Work T(n) = ∑i=0..h ai×f(n/bi) = Q(n) · log(n)

94 Quick Sort 88 14 98 25 62 52 79 30 23 31 Divide and Conquer

95 Quick Sort Partition set into two using randomly chosen pivot 88 14 98
25 62 52 79 30 23 31 14 25 30 23 31 88 98 62 79 ≤ 52 ≤

96 Quick Sort Get one friend to sort the first half.
14 25 30 23 31 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.

97 Quick Sort Glue pieces together. (No real work) 14,23,25,30,31 52
62,79,98,88 Glue pieces together. (No real work) 14,23,25,30,31,52,62,79,88,98

98

99 Quick Sort Let pivot be the first element in the list? 88 14 98 25 62
52 79 30 23 31 14 88 98 30 ≤ 31 ≤ 62 23 25 52 79

100 Quick Sort 14,23,25,30,31,52,62,79,88,98 23,25,30,31,52,62,79,88,98 ≤ 14 ≤ If the list is already sorted, then the slit is worst case unbalanced.

101 Quick Sort T(n) = 2T(n/2) + Q(n) = Q(n log(n)) Best Time: Worst Time:
Expected Time:

102 Quick Sort T(n) = T(0) + T(n-1) + Q(n) Best Time:
T(n) = 2T(n/2) + Q(n) = Q(n log(n)) Worst Time: = Q(n2) Expected Time:

103 Quick Sort Best Time: T(n) = 2T(n/2) + Q(n) = Q(n log(n)) Worst Time:
T(n) = T(0) + T(n-1) + Q(n) = Q(n2) Expected Time: T(n) = T(1/3n) + T(2/3n) + Q(n) = Q(n log(n))

104 Quick Sort Expected Time: T(n) = T(1/3n) + T(2/3n) + Q(n)
= Q(n log(n)) Top work Q(n) 2nd level Q(n) Q(n) 3rd level 1/3 2/3 3/2 log (n) # levels = = Q(log(n))

105 Kth Element Problem Specification
<preCond>: The input is a list of n values and an integer k. <postCond>: The kth smallest in the list. k=3 88 14 98 25 62 52 79 30 23 31 Output: 25 14,23,25,30,31,52,62,79,88,98

106 Kth Element Problem Specification
Partition set into two using randomly chosen pivot 88 14 98 25 62 52 79 30 23 31 14 25 30 23 31 88 98 62 79 ≤ 52 ≤

107 Kth Element Problem Specification
14 25 30 23 31 88 98 ≤ 52 ≤ 62 79 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

108 Kth Element Problem Specification
14 25 30 23 31 88 98 ≤ 52 ≤ 62 79 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

109

110 Kth Element Problem Specification
Best Time: T(n) = 1 T(n/2) + Q(n) = Q(n) n log a/log b # of base cases = Worst Time: n log 1/log 2 = = n0 = 1 n +n/2 +n/4 +n/8 +1 Expected Time:

111 Kth Element Problem Specification
Best Time: T(n) = 1 T(n/2) + Q(n) = Q(n) Worst Time: T(n) = 1 T(n-1) + Q(n) ≈ n+T(n-1) ≈ n+(n-1)+T(n-2) ≈ n+(n-1)+(n-2)+T(n-3) ≈ n+(n-1)+(n-2)+(n-3)+…+1 ≈ Q(n2) Expected Time:

112 Kth Element Problem Specification
Best Time: T(n) = 1 T(n/2) + Q(n) = Q(n) Worst Time: T(n) = 1 T(n-1) + Q(n) = Q(n2) Expected Time: T(n) = 1 T(2/3n) + Q(n) = Q(n)

113 T(N) = 2T(N/2) + 1 = (N) Size = log(b) + log(N) = 2(n) b7 = b3 × b4 N=7 b4 b3 N=4 N=3 N=2 N=1

114 T(N) = 1T(N/2) + 1 = (log(N)) Size = log(b) + log(N) = (n) b7 = (b3)2 × b N=7 b3 N=3 N=1

115 Recursion on Trees 3 8 1 2 7 6 5 9 4

116 Recursion on Trees A binary tree is: - the empty tree
- a node with a right and a left sub-tree. tree because 3 8 1 2 7 6 5 9 4

117 Recursion on Trees A binary tree is: - the empty tree
- a node with a right and a left sub-tree. node tree 3 8 1 2 7 6 5 9 4 tree

118 Recursion on Trees A binary tree is: - the empty tree
- a node with a right and a left sub-tree. 3 8 1 2 7 6 5 9 4 tree because

119 Recursion on Trees A binary tree is: - the empty tree
- a node with a right and a left sub-tree. 3 8 1 2 7 6 5 9 4 node tree tree

120 Recursion on Trees A binary tree is: - the empty tree
- a node with a right and a left sub-tree. 3 8 1 2 7 6 5 9 4 tree because

121 Recursion on Trees A binary tree is: - the empty tree
- a node with a right and a left sub-tree. 3 8 1 2 7 6 5 9 4 node tree tree

122 Recursion on Trees A binary tree is: - the empty tree
- a node with a right and a left sub-tree. 3 8 1 2 7 6 5 9 4 tree because empty

123 Recursion on Trees number of nodes = ? Get help from friends 5 6 3 8 1
2 7 6 5 9 4

124 Recursion on Trees number of nodes
= number on left + number on right + 1 = = 12 5 3 8 1 2 7 6 5 9 4 6

125 Recursion on Trees number of nodes Base Case ? 1 Are we done? 3 8 1 2
7 6 5 9 4 1 Are we done?

126 Recursion on Trees number of nodes Base Case ? 3 8 1 2 7 6 5 9 4 ?

127 Recursion on Trees number of nodes Base Case ? Better base case! 3 8 1
2 7 6 5 9 4 Better base case!

128 Recursion on Trees number of nodes Base Case ? 1
3 8 1 2 7 6 5 9 4 1 Does this still need to be a base case?

129 Recursion on Trees number of nodes
= number on left + number on right + 1 = = 1 3 8 1 2 7 6 5 9 4 No!

130 Recursion on Trees Most programmers don’t use the empty tree as a base case. This causes a lot more work. 3 8 1 2 7 6 5 9 4

131 Recursion on Trees

132 Recursion on Trees Designing Program/Test Cases Try same code n1 n2
n1 n1 + 1 3 3 generic generic generic Same code works! Try same code 0+0+1=1 3 Same code works!

133 Recursion on Trees Time: T(n) = T(left) + T(right) + Q(1) = Q(n)
One stack frame for each node in the tree

134 Recursion on Trees sum of nodes = ? Get help from friends 25 23 3 8 1
7 6 5 9 4

135 Recursion on Trees sum of nodes
= sum on left + sum on right + value at root = = 51 25 3 8 1 2 7 6 5 9 4 23

136 Recursion on Trees sum of nodes = ? Base Case ? 3 8 1 2 7 6 5 9 4

137 Recursion on Trees

138 Recursion on Trees Designing Program/Test Cases s1 s2 s1 + s2 + 3 s1
s1 s1 + 3 3 3 generic generic generic =3 3

139 Recursion on Trees max of nodes = ? Get help from friends 9 8 3 8 1 2
7 6 5 9 4

140 Recursion on Trees max of nodes
= max( max on left, max on right, value at root) = max( 8, 9, 3) = 9 9 3 8 1 2 7 6 5 9 4 8

141 Recursion on Trees max of nodes = ? Base Case ? 3 8 1 2 7 6 5 9 4 ?

142 Recursion on Trees 3+4+2+8+… Sum so far is 17 Sum so far is
NewSum = OldSum + nextElement = =

143 Recursion on Trees 3*4*2*3*… Product so far is 72 Product so far is 1
NewProd = OldProd × nextElement = × =

144 Recursion on Trees Conclusion so far is True
True and True and False and … Conclusion so far is True Conclusion so far is True NewProd = OldProd and nextElement = True and True = True

145 Recursion on Trees -¥ Max(3,4,2,3,… Max so far is 4 Max so far is
NewMax = max(OldMax, next Element) = max( -¥ , ) =

146 Recursion on Trees

147 Recursion on Trees height of tree = ? Get help from friends 4 3 3 8 1
2 7 6 5 9 4

148 Recursion on Trees height of tree
= max(height of left,height on right) + 1 = max(3,4) + 1 = 5 4 3 8 1 2 7 6 5 9 4 3

149 Recursion on Trees height of tree Base Case ? 3 8 1 2 7 6 5 9 4 ?

150 Recursion on Trees height of tree? 2 nodes or 1 edge Base Case ?
0 nodes ? edges Base Case ? 3 8 1 2 7 6 5 9 4

151 Recursion on Trees Work it out backwards height = 0 edge
= max(height of left,height on right) + 1 = max(?,?) + 1 ? 3 8 1 2 7 6 5 9 4 ? edges

152 Recursion on Trees Work it out backwards height = 0 edge
= max(height of left,height on right) + 1 = max(-1,-1) + 1 3 8 1 2 7 6 5 9 4 -1 edges -1 -1

153 Recursion on Trees

154 Recursion on Trees number of leaves = ? Get help from friends 2 3 3 8
1 2 7 6 5 9 4

155 Recursion on Trees number of leaves = number on left + number on right
= = 5 2 3 8 1 2 7 6 5 9 4 3

156 Recursion on Trees number of leaves Base Case ? 3 8 1 2 7 6 5 9 4

157 Recursion on Trees Designing Program/Test Cases L1 L2 L1 + L2 L1
L1 3 3 generic generic generic 1 No! 0+0 = 0 3 Needs to be another base case.

158 Recursion on Trees

159 Recursion on Trees

160 Recursion on Trees

161 Recursion on Trees ? Drops the tree.

162 Define Problem: Binary Search Tree
<preCond> Key A binary search tree. <postCond> Find key in BST (if there). 38 25 17 4 21 31 28 35 51 42 40 49 63 55 71

163 Its left children ≤ Any node ≤ Its right children
Binary Search Tree Its left children ≤ Any node ≤ Its right children 38 25 51 17 31 42 63 4 21 28 35 40 49 55 71

164 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. 38 25 17 4 21 31 28 35 51 42 40 49 63 55 71 key 17

165 Define Step Cut sub-tree in half.
Determine which half the key would be in. Keep that half. 38 25 17 4 21 31 28 35 51 42 40 49 63 55 71 key 17 If key < root, then key is in left half. If key = root, then key is found If key > root, then key is in right half.

166 Recursion on Trees

167 Recursion on Trees

168 Recursion on Trees Time: T(n) = 2T(n/2) + Q( n) = Q(n log(n))
If tree very unbalanced? T(n) = 1T(n-1) + (n) = (n2) Computing max is too much work.

169 Recursion on Trees Extra info from below

170 Recursion on Trees 1 Time: T(n) = 2T(n/2) + Q( n) = Q(n log(n)) n
Computing max is too much work.

171 Recursion on Trees Extra info from above min   max min   max min 
38 min   max min   root root   max -¥, ¥

172 Things to Remember Things to Do and Things NOT TO DO

173 Friends - Strong Induction View of Recursion.
I am obsessed with the Friends - Strong Induction View of Recursion. Trust your friends to solve sub-instances. The sub-instance given must be smaller and must be an instance to the same problem. Combine solution given by friend to construct your own solution for your instance. Focus on one step. Do not talk of their friends friends friends. Solve small instances on your own. Do it!

174 Don't have inputs or outputs that are not explained!
Typical Test Answer Define pre & post conditions Don't have inputs or outputs that are not explained!

175 Typical Test Answer Call recursively on the correct types of input
k,num,v

176 returning the correct types of output
Typical Test Answer Call recursively returning the correct types of output Save the results (or don't bother calling)

177 Combine solutions given by friends to construct your own solution.
Typical Test Answer Combine solutions given by friends to construct your own solution.

178 Return things of the correct types.
Typical Test Answer Return things of the correct types. Not an element the root? In every path through code

179 Sub-instances need to be smaller.
Typical Test Answer Sub-instances need to be smaller. “Size” is size of the tree Wrong base case

180 What is the value of n here?
Typical Test Answer No Global Variables. What is the value of n here? Still zero. The friend has his own variable n.

181 Maybe they mean to pass the new n in and out.
Typical Test Answer No Global Variables. Maybe they mean to pass the new n in and out. Please don’t use these to pass out values when I am marking.

182 Looks like an iterative algorithm
Typical Test Answer Looks like an iterative algorithm Looks like an recursive algorithm Which is it?

183 Hard to write an iterative program
to traverse on a recursive structure tree 3 8 1 2 7 6 5 9 4 Need pointers from each node to its parent.

184 Hard to write a recursive program that implements an iterative algorithm.

185 Writing a Recursive Program
Call recursively on the correct types of input

186 on sub-instances which will give useful information.
Writing a Recursive Program Call recursively on sub-instances which will give useful information. ? node in sub-tree 3 8 1 2 7 6 5 9 4

187 on sub-instances which will give useful information.
Writing a Recursive Program Call recursively on sub-instances which will give useful information. 3rd node in sub-tree node in whole tree ? 3 8 1 2 7 6 5 9 4

188 on sub-instances which will give useful information.
Writing a Recursive Program Call recursively on sub-instances which will give useful information. 3rd node in sub-tree nl node in whole tree +1 +3rd 3 8 1 2 7 6 5 9 4

189 on sub-instances which will give useful information.
Writing a Recursive Program Call recursively on sub-instances which will give useful information. 3rd node in sub-tree nl node in whole tree +1 +3rd 3 8 1 2 7 6 5 9 4

190 Writing a Recursive Program
But who will count nodes in left sub-tree? 3 8 1 2 7 6 5 9 4 nl nl My friend

191 Writing a Recursive Program
But who will count nodes in left sub-tree? 3 8 1 2 7 6 5 9 4 nl nl My friend

192 returning the correct types of output
Writing a Recursive Program Call recursively returning the correct types of output Save the results (or don't bother calling)

193 Combine solutions given by friends to construct your own solution.
Writing a Recursive Program Combine solutions given by friends to construct your own solution.

194 Writing a Recursive Program
Sub-instances need to be smaller. “Size” is # nodes in the tree

195 Writing a Recursive Program
Sub-instances need to be smaller. When the instance sufficiently small solve on your own.

196 Return things of the correct types.
Writing a Recursive Program Return things of the correct types.

197 Not possible because must count # nodes
Writing a Recursive Program Running Time T(n) = 2T(n/2) + (1) = (n) Faster? Not possible because must count # nodes in left tree.

198 Writing a Recursive Program
Running Time T(n) = 2T(n/2) + (n) = (n logn) If tree very unbalanced? T(n) = 1T(n-1) + (n) = (n2)

199 Heaps, Heap Sort, & Priority Queues

200 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 9 go? Maximum is at root. Where can 1 go? Where can 8 go?

201 Completely Balanced Binary Tree Implemented by an Array
Heap Data Structure Completely Balanced Binary Tree Implemented by an Array

202 Make Heap Get help from friends

203 Maximum needs to be at root.
Heapify Where is the maximum? Maximum needs to be at root. ?

204 Heapify Find the maximum. Put it in place ? Repeat

205 Heapify Heap Running Time:

206

207

208 Make Heap Get help from friends Heapify Running time:
T(n) = 2T(n/2) + log(n) = Q(n)

209 Another algorithm ? Heap Heaps

210 ? Heap

211 ? Heap

212 ?

213 Heap

214 Running Time: i log(n) -i 2log(n) -i

215 Priority Queues

216 Selection 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 Max is easier to find if a heap.

217 Heap Sort Largest i values are sorted on side.
Remaining values are in a heap. 79 km 75 km Exit Exit

218 Heap Data Structure 9 8 7 6 Heap Array Heap Array 5 3 4 2 1

219 Heap Sort ? Largest i values are sorted on side. Remaining values are
in a heap. 79 km 75 km Exit ? Put next value where it belongs. Heap Exit

220 Heap Sort

221 Heap Sort ? ? ? ? ? ? ? Sorted

222 Heap Sort Running Time:

223 Recursion on Trees Evaluate Equation Tree = ? Get help from friends 7
12 7 Get help from friends

224 Recursion on Trees Evaluate Equation Tree
= rootop( value on left, value on right ) = rootop(12,7) = = 19 7 12

225 Recursion on Trees Evaluate Equation Tree Base Case ? 7

226

227 Derivatives Input: a function f. Output: Its derivative df/dx.

228 Derivatives

229 Derivatives Input: a function f. Output: Its derivative df/dx.

230 Derivatives Input: a function f. Output: Its derivative df/dx.

231 Derivatives Output: Its derivative df/dx. Input: a function f. g

232 Simplify Input: a function f. Output: f simplified.

233 Simplify

234 Recursion on Trees Printing a Tree
When I taught this one year, a student needed just this algorithm for his job.

235 Recursion on Trees Get help from friends Typing line by line?

236 Recursion on Trees One stack frame prints: His input gives: his tree
sting for each line whether left or right

237 Recursion on Trees He gives his friends: their trees
string for each line whether left or right

238

239

240

241

242 Parsing Input: s=6*8+((2+42)*(5+12)+987*7*123+15*54) Output:

243 Parsing

244 Parsing

245 Parsing Algorithm: GetExp( s, i )
Input: s is a string of tokens i is a start index Output: p is a parsing of the longest valid expression j is the end index s=6*8+((2+42)*(5+12)+987*7*123+15*54)

246 Parsing Algorithm: GetTerm( s, i )
Input: s is a string of tokens i is a start index Output: p is a parsing of the longest valid term j is the end index s=6*8+((2+42)*(5+12)+987*7*123+15*54)

247 Parsing Algorithm: GetFact( s, i )
Input: s is a string of tokens i is a start index Output: p is a parsing of the longest valid factor j is the end index s=6*8+((2+42)*(5+12)+987*7*123+15*54)

248 Algorithm: GetExp( s, i )
p<term,1> p<term,2> p<term,3>

249 Algorithm: GetExp( s, i )
p<term,1> p<term,2> p<term,k> +

250 Algorithm: GetTerm( s, i )
p<fact,1> p<fact,2>

251 Algorithm: GetTerm( s, i )
p<fact,1> p<fact,2> p<fact,k> *

252 Parsing Algorithm: GetFact( s, i )

253 Parsing Algorithm: GetFact( s, i ) Fact 42

254 Algorithm: GetFact( s, i )
p<exp>

255 Algorithm: GetFact( s, i )
p<exp> ( )

256 Parsing Stackframes  nodes in parse tree

257 Recursive Images if n=1 if n=0, draw n=0 else draw And recursively
Draw here with n-1

258 Recursive Images if n=2 if n=0, draw n=1 else draw And recursively
Draw here with n-1

259 Recursive Images if n=3 if n=0, draw n=2 else draw And recursively
Draw here with n-1

260 Recursive Images if n=30 if n=0, draw else draw And recursively
Draw here with n-1

261 Recursive Images if n=0 if n=5 if n=4 if n=3 if n=1 if n=2

262 Recursive Images if n=0 if n=5 if n=4 if n=2 if n=1 if n=3

263 Recursive Images Þ ¥ L(n) = 4/3 L(n-1) = (4/3)n

264

265

266

267

268

269 Ackermann’s Function How big is A(5,5)?

270 Ackermann’s Function

271 Ackermann’s Function

272 Ackermann’s Function 3

273 Ackermann’s Function

274 Ackermann’s Function

275 Ackermann’s Function

276 Ackermann’s Function

277 Ackermann’s Function


Download ppt "How to Think about Algorithms"

Similar presentations


Ads by Google