Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks, Queues, and Deques

Similar presentations


Presentation on theme: "Stacks, Queues, and Deques"— Presentation transcript:

1 Stacks, Queues, and Deques
- Ed. 2, and 3.: Chapter 4 Ed. 4: Chapter 5

2 Stacks, Queues, and Deques
- Stack ADT – interface - Stack implementation Queues - Queue ADT – interface - Queue implementation Deques - Deque ADT – interface - Deque implementation Sample case study application

3 Stacks A stack is an interesting data structure with a lot of applications. In such a data structure, we are dealing with a collection of objects. Objects can be added to or removed from the collection, one at a time, as plates in a spring-loaded, cafeteria plate dispenser.

4 Stacks

5

6 The Stack Abstract Data Type
An array of integers is not a stack. However, if we enforce the LIFO principle, it becomes a stack. A stack S is an abstract data type (ADT) that supports following two fundamental methods: push(o): Insert object o at the top of the s tack Input : Object; Output : None. pop(): Remove from the stack and return the top object on the stack; an error occurs if the stack is empty. Input : None; Output : Object

7 Other supporting methods:
size(): Return the number of objects in the stack. Input: None; Output: Integer isEmpty(): Return a Boolean indicating if the stack is empty. Input: None; Output: Boolean top(): Return the top object on the stack, without removing it; an error occurs if the stack is empty. Input: None; Output: Object

8 This table shows a series of stack operations and their effects.
The stack is initially empty. 5 Operation Output S push(5) - (5) push(3) (5,3) pop() 3 push(7) (5,7) 7 top() 5 () “error” isEmpty() true push(9) (9) (9,7) (9,7,3) (9,7,3,5) size() 4 push(8) (9,7,3,8) 8 5 3 5 The stack is now empty.

9 A Stack Interface in Java

10

11 A Simple Array-Based Implementation
To implement a stack with an array, we need: 1. An array of the given size 2. An index t for the top element (Normally, t is implemented as a variable of type int. Initially, t is set to –1.) S: 1 t N - 1 … …

12 Algorithms: size(): return the number t + 1 S: 1 t N - 1 … …

13 Algorithms: isEmpty(): return true if t < 0; otherwise, false. S: 1 t N - 1 … … t + 1 = 0 or t = -1 or t < 0

14 Algorithms: top(): if the stack is empty throw a StackEmptyException else return element S[t] S: 1 t N - 1 … …

15 push(o): if stack size is N throw a StackFullException else increase the index t by 1 store the object to S[t] It can be defined in the same way for StackEmptyException. S: … … 1 t t+1 N - 1 push

16 pop(): if stack is empty throw a StackEmptyException else save the element S[t] to a variable e make the element S[t] a null object decrease the index t by 1 return e S: … … 1 t N - 1 pop

17

18

19

20 Stacks in the Java Virtual Machine

21

22 Sample Case Study Application
We want to write a program to calculate the span of the stock’s price on a given day. The span of the stock’s price on a given day: The maximum number of the consecutive days up to the current day (including the current day) such that the stock price on each of those days has been less than or equal to the price on the current day.

23 Exemplary data: Price of 7-year bonds: Spans: April 9 April 10
48.97 47.54 45.83 46.34 45.68 46.95 48.17 Spans: April 9 April 10 April 11 April 12 April 13 April 14 April 15 1 2 4 6

24 Let us use some symbols to represent the problem: Stock Price:
Stock Price: April 9 April 10 April 11 April 12 April 13 April 14 April 15 p0 p1 p2 p3 p4 p5 p6 April 9 April 10 April 11 April 12 April 13 April 14 April 15 s0 s1 s2 s3 s4 s5 s6 The problem is how to calculate the span of a given day.

25 Since price quotes start with a given day (day 0, which is April 9
Analysis – property I: Since price quotes start with a given day (day 0, which is April 9 in this example), we have: si  i + 1 Spans: April 9 April 10 April 11 April 12 April 13 April 14 April 15 1 2 4 6 si: i + 1: s0 = 1 0 + 1 s1 = 1 1 + 1 s2 = 1 2 + 1 s3 = 2 3 + 1 s4 = 1 4 + 1 s5 = 4 5 + 1 s6 = 6 6 + 1

26 Analysis – property II:
For a given day i, if the span is 1, it means pi-1 > pi or (The price of day pi-1 is higher than that of i = 0 day pi.) If the span is 2, it means: pi-1  pi and pi-2 > pi . If the span is 3, it means: pi-2  pi , pi-1  pi, and pi-3 > pi. If the span is 4, it means: pi-3  pi , pi-2  pi, pi-1  pi, and pi-4 > pi.

27 In general, if the span of pi is k, it means:
pi-k-1  pi, pi-k-2  pi, … pi-1  pi, and pi-k > pi.

28 An Algorithm Without Using a Stack
Let us consider an algorithm without using a stack. To find out the span for a given day i: 1. Assign value 0 to a variable k 2. Compare pi-k with pi. 3. If pi-k  pi, increment the value in k and go to step 2. 4. Step 2 and 3 are repeated until pi-k > pi or k > i. The span is the value in the variable k.

29 To find out the spans for all the days, the procedure has to be
repeated with i = 0, 1, …, n - 1. Therefore, the complete algorithm has two loops: One for the days and another is to find the span for a given day. The spans are stored in an array s. for i = 0 to n assign value 0 to a variable k repeat until pi-k > pi or k > i compare pi-k with pi. if pi-k  pi increment the value in k save the value of k to s[i] i p1 p2 … … pi … … pn-1 k for a certain i s[i]

30 The running time of this algorithm is proportional to the square
of n, the number of days, namely, running time = O(n2) (We say a quantity D is O(n2) if there exists a real constant c and an integer n0  1 such that D  cn2 for every integer n  n0.) Such an algorithm is called a quadratic-time algorithm. Using a stack, however, we can cut down the running time dramatically.

31 An Algorithm Using a Stack
Note that the span si on a certain day i can be easily computed if we know the closest day preceding day i, such that the price on that day is higher than the price on day i. If such a preceding day exists for a day i, let us denote it with h(i), and otherwise let us define h(i) = -1. Then, si = i – h(i).

32 si = i – h(i). h(0) -1 h(1) h(2) 1 h(3) 1 h(4) 3 h(5) 1 h(6) s0 s1 s2 s3 s4 s5 s6 1 1 1 2 1 4 6

33 The problem is how to compute h(i) efficiently?
Step 1: p0 = h(0) = -1, s0 = 0 - h(0) = 0 – (-1) = 1 Day 0. It is possible that h(1) = 0. Step 2: p1 = Pop days with prices less than or equal to p1. At this point of time, we have only one element in the stack. It is 0 and p0 > p1. So h(1) = 0, s1 = 1 - h(1) = 1 – 0 = 1. Day 1. It is possible that h(2) = 1. 1

34 p2 = 45.83. Pop days with prices less than or equal to p2.
Step 3: p2 = Pop days with prices less than or equal to p2. At this point of time, we have two elements in the stack. The top one is 1 and p1 > p2. So h(2) = 1, s2 = 2 - h(2) = 2 – 1 = 1. Day 2. It is possible that h(3) = 2. 2 1 Step 4: p3 = Pop days with prices less than or equal to p3. The top one will be taken out since p3 > p2. The second one is 1 and p1 > p3. So h(3) = 1, s3 = 3 - h(3) = 3 – 1 = 2. Day 3. It is possible that h(4) = 3. 1 3

35 p4 = 45.68. Pop days with prices less than or equal to p4.
Step 5: p4 = Pop days with prices less than or equal to p4. The top one is 3 and p3 > p4. So h(4) = 3, s4 = 4 - h(3) = 4 – 3 = 1. Day 4. It is possible that h(5) = 4. 4 3 1 Step 6: p5 = Pop days with prices less than or equal to p3. The top two will be taken out since p5 > p4 and p5 > p3. The third one is 1 and p1 > p5. So h(5) = 1, s5 = 5 - h(5) = 5 – 1 = 4. Day 5. It is possible that h(6) = 5. 1 5

36 p6 = 48.17. Pop days with prices less than or equal to p3.
Step 7: p6 = Pop days with prices less than or equal to p3. The top two will be taken out since p6 > p5 and p6 > p1. The third one is 0 and p0 > p6. So h(6) = 0, s6 = 6 - h(6) = 6 – 0 = 6. Day 6. The price on day 6. The process stops. 6

37 Java Implementation h(0) h(i)

38

39 public class DailyHighSpan { … …
public static void main(String args[]) { Quote Q[] = new Quote[10]; System.out.println("Input:" + "\n"); for (int i = 0; i < 10; i++) { Q[i] = new Quote(i,(int)(Math.random()*100)); System.out.print(Q[i].getPrice(); ); System.out.print('\t'); //System.out.print("-"); } System.out.println(); try{ computeDailyHighSpan(Q); catch(EmptyStackException e) {}


Download ppt "Stacks, Queues, and Deques"

Similar presentations


Ads by Google