Presentation is loading. Please wait.

Presentation is loading. Please wait.

22C:21 Discrete Structures

Similar presentations


Presentation on theme: "22C:21 Discrete Structures"— Presentation transcript:

1 22C:21 Discrete Structures
Problem 7.2. System Stacks on Recursion

2 Stacks – just to refresh your memory
List FIFO – push/pop There is a built-in Stack ADT in java. However, I used my own implementation… … because I can … yes, it’s THAT easy … and a little personal touch is always better

3 The myStack class // Instance variables private static int[] stack;
private static int top=0; private static int MAX=100; // Default constructor public myStack() { stack=new int[MAX]; top=-1; }

4 Push(element) public void push(int b) { if(top>=MAX)
System.out.println("Overflow!"); return; } stack[++top]=b; this.print();

5 Pop() public int pop() { if(top<0)
System.out.println("Underflow!"); return(-1); } int temp=top--; this.print(); return stack[temp];

6 Fibonacci Numbers F0 = 1; F1 = 1; Fn = Fn-1 + Fn-2 for n ≥ 2 So…
F2 = 2, F3 = 3, F4 = 5, F5 = 8 …and so on

7 Modified Fibonacci Code
public static BigInteger fibonacci(int n) { System.out.println("fibonacci("+n+") has been called..."); SysStack.push(n); if((n == 0) || (n == 1)) System.out.println("fibonacci("+n+") returning value..."); int garbage = SysStack.pop(); return new BigInteger ("1"); } else BigInteger number1 = fibonacci(n-1); BigInteger number2 = fibonacci(n-2); int moregrabage=SysStack.pop(); return number1.add(number2);

8 Output for n=3 fibonacci(3) has been called... Current Stack: -- 3
2

9 Output for n=3 contd. fibonacci(1) has been called... Current Stack:
-- 1 2 3 fibonacci(1) returning value...

10 Output for n=3 contd. fibonacci(0) has been called... Current Stack:
-- 2 3 fibonacci(0) returning value...

11 Output for n=3 contd. fibonacci(2) returning value... Current Stack:
-- 3 fibonacci(1) has been called... 1

12 … should be empty at the end
Output for n=3 contd. fibonacci(1) returning value... Current Stack: -- 3 fibonacci(3) returning value... … should be empty at the end

13 What happens for n=5? 5 Fibonacci(5) 4 3 5 System Stack Empty

14 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 4 5
System Stack

15 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3)
1 3 4 5 System Stack

16 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3)
1 2 Fibonacci(2) 3 1 4 5 System Stack

17 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3)
1 2 1 2 Fibonacci(2) 3 4 Fibonacci(1) 1 5 System Stack

18 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3)
1 2 Fibonacci(2) 3 4 5 System Stack

19 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3)
2 1 2 Fibonacci(2) 3 4 Fibonacci(0) 5 System Stack

20 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3)
1 2 Fibonacci(2) 3 4 5 System Stack

21 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3)
1 3 4 5 System Stack

22 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3)
1 1 Fibonacci(1) 3 4 5 System Stack

23 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3)
System Stack

24 What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 2 4 5
System Stack

25 What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 2 Fibonacci(2) 1
2 4 5 System Stack

26 What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 2 Fibonacci(2) 1
Fibonacci(1) 2 4 5 System Stack

27 What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 2 Fibonacci(2) 2
2 4 5 System Stack

28 What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 2 Fibonacci(2)
Fibonacci(0) 2 4 5 System Stack

29 What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 2 Fibonacci(2) 2
System Stack

30 What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 4 5 System Stack

31 What happens for n=5? 5 Fibonacci(5) 3 5 System Stack

32 What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 3 5
System Stack

33 What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 Fibonacci(2) 1
2 3 5 System Stack

34 What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 Fibonacci(2) 1
2 3 5 System Stack

35 What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 Fibonacci(2) 2
2 3 5 System Stack

36 What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 Fibonacci(2)
Fibonacci(0) 2 3 5 System Stack

37 What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 Fibonacci(2) 2
System Stack

38 What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 1 3 5 System Stack

39 What happens for n=5? Fibonacci(5) 5 Fibonacci(3) 3 1 Fibonacci(1) 1 3
System Stack

40 What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 3 5 System Stack

41 What happens for n=5? 5 Fibonacci(5) 5 System Stack

42 What happens for n=5? System Stack Empty

43 Note Actual system stack holds returned values
Just the indices has been shown Compare with FastFibonacci()

44 Now you know!

45 Quiz 7 You are given a collection with 6 items. The priorities of these items are shown below: 1. Draw all possible max-heaps (as binary trees) for this collection of items. 2. Start with the max-heap (30, 50, 9, 25, 7, 22). Perform the following sequence of operations on this max-heap: Insert(45) Insert(29) Delete() Show the max-heap after each operation. 3. Answer in a sentence or two: Given a max-heap can you find the item with 2nd highest priority in O(1) time? Explain your answer. 4. Answer in a sentence or two: Given a max-heap can you find the item with smallest priority in O(1) time? Explain your answer.


Download ppt "22C:21 Discrete Structures"

Similar presentations


Ads by Google