ch 5 : stacks, queues, and deques acknowledgement: the slides are prepared from slides provided with...

35
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO AND JORY DENNY 1

Upload: tyrone-bridges

Post on 18-Jan-2018

241 views

Category:

Documents


1 download

DESCRIPTION

STACKS  A stack is a container with visibility and access through one end known as top element.  LIFO – Last In First Out Principle  Practical Applications:  Web browser history of pages : Back button  Editors: Undo button  Runtime Stack: Nested Function calls and storage of parameters, variables, return address, etc.  Indirect applications  Auxiliary data structure for algorithms  Component of other data structures 3

TRANSCRIPT

Page 1: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

1

CH 5 : STACKS, QUEUES, AND DEQUESACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO AND JORY DENNY

Page 2: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

2

STACK - OUTLINE

The Stack ADT (Ch. 5.1.1) Array-based implementation (Ch. 5.1.4) Growable array-based stack Singly list implementation (Ch 5.1.5)

Page 3: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

3

STACKS

A stack is a container with visibility and access through one end known as top element. LIFO – Last In First Out Principle Practical Applications:

Web browser history of pages : Back button Editors: Undo button Runtime Stack: Nested Function calls and storage of parameters, variables, return address, etc.

Indirect applications Auxiliary data structure for algorithms Component of other data structures

Page 4: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

4

STACK ADT Stores arbitrary objects Insertions, deletion, modification and

access is allowed through the top element. Main Operations:

push(e) – inserts element e at the top of the stack

pop() – removes the top element from the stack

top() – returns the top element without removing it

Auxiliary operations: size() – Number of elements in the stack empty() – True if the stack contains no

elements Removing or accessing an element in an

empty stack returns an exception called EmptyStackException.

Page 5: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

5

EXERCISE

Show the stack after each of these operations: Push(5) Push(3) Pop() Push(2) Push(8) Pop() Pop() Pop() Push(1)

Page 6: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

6

C++ RUNTIME STACK

The C++ run-time system keeps track of the chain of active functions with a stack

When a function is called, the system pushes on the stack a frame containing Local variables and return value Program counter, keeping track of

the statement being executed When the function ends, its frame is

popped from the stack and control is passed to the function on top of the stack

main() { int i; i = 5; foo(i);}foo(int j) { int k; k = j+1; bar(k);}bar(int m) { …}

bar PC = 1 m = 6

foo PC = 3 j = 5 k = 6

main PC = 2 i = 5

Page 7: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

7

ARRAY BASED STACK IMPLEMENTATION

Array based implementation is the simplest

Add elements from left to right of the array

Keep track of the index of the top element in a variable, t

Do you observe any problems?

S0 1 2 t

Algorithm pop()if empty() then

throw StackEmptyException

else t t 1return S[t + 1]

Algorithm push(e)t t + 1S[t] e

Page 8: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

8

ARRAY BASED STACK IMPLEMENTATION

The array storing the stack elements may become full

A push operation will then throw a StackFullException Limitation of the array-based

implementation Not intrinsic to the Stack ADT

S0 1 2 t

Algorithm push(e)if size() = S.length

thenthrow

StackFullException else

t t + 1S[t] e

Algorithm size()return t

+ 1

Page 9: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

9

ALGORITHMIC COMPLEXITY ANALYSIS

Computer Scientists are concerned with describing how long and how much memory an algorithm (computation) takes, known as time and space complexity of the algorithm Described through functions which show how time or space grows

as function of input, note that there are no constants! – Constant time - Logarithmic time – Linear time – Quadratic time

1 2 3 4 5 6 7 8 9 10

Constant LogarithmicLinear Quadratic

Input Size

Tim

e

Page 10: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

10

STACK PERFORMANCE

Array Fixed Capacity

pop()

push(e)

top()

size(), empty()

Array Fixed Capacity

pop()

push(e)

top()

size(), empty()

Page 11: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

11

PERFORMANCE AND LIMITATIONS

Performance Let n be the number of elements in the stack The space used is O(n) Each operation runs in time O(1)

Limitations The maximum size of the stack must be defined a priori and cannot be changed Trying to push a new element into a full stack causes an implementation-specific

exception

Page 12: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

12

GROWABLE ARRAY STACK

Instead of throwing an exception while pushing to a filled stack, replace the array with a larger array.

How much to increase to? incremental strategy: increase the

size by a constant ,

doubling strategy: double the size,

Algorithm if then new array of length, for to do

Page 13: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

13

COMPARISON OF THE STRATEGIES

We compare the incremental strategy and the doubling strategy by analyzing the total time needed to perform a series of push operations

We assume that we start with an empty stack represented We call amortized time of a push operation the average time taken by a push over the

series of operations, i.e.,

Page 14: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

14

INCREMENTAL STRATEGY ANALYSIS

Let be the constant increase and be the number of push operations We replace the array times The total time of a series of push operations is proportional to

is so the amortized time of a push is

Note:

Page 15: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

15

DOUBLING STRATEGY ANALYSIS

We replace the array times The total time of a series of n push

operations is proportional to

is so the amortized time of a push is

1

2

1

4

8

Page 16: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

16

STACK PERFORMANCE

Array Fixed Capacity

Growable Array (Doubling)

pop()push(e) Worst Case

Best CaseAverage Case

top()size(), empty()

Array Fixed Capacity

Growable Array (Doubling)

pop()push(e) Worst Case

Best CaseAverage Case

top()size(), empty()

Page 17: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

17

SINGLY LINKED LIST STACK IMPLEMENTATION

We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is and each operation of the Stack ADT takes time

nodes

elements

top

Page 18: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

18

EXERCISE

Describe how to implement a stack using a singly-linked list Stack operations: , , , For each operation, give the running time

Page 19: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

19

STACK SUMMARY

Array Fixed Capacity

Growable Array (Doubling)

Singly Linked List

pop()push(e) Worst Case

Best CaseAverage Case

top()size(), empty()

Page 20: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

20

QUEUES - OUTLINE

The Queue ADT (Ch. 5.2.1) Implementation with a circular array (Ch. 5.2.4)

Growable array-based queue List-based queue

Page 21: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

21

QUEUES

Container storing arbitrary objects such that insertions allowed at one end called back and removal from other end called front.

FIFO – First In First Out scheme Practical applications:

Waiting lines Scheduling – Task, shared resources (printer) access Multiprogramming

Indirect applications Auxiliary data structure for algorithms Component of other data structures

Page 22: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

22

QUEUE ADT

Insertions and deletions follow the first-in first-out scheme

Insertions are at the rear of the queue and removals are at the front of the queue

Main operations: enqueue(e): inserts an element e at the

end of the queue dequeue(): removes the element at the

front of the queue

Auxiliary queue operations: front(): returns the element at the front

without removing it size(): returns the number of elements

stored empty(): returns a Boolean value

indicating whether no elements are stored

Application of dequeue() on an empty queue throws EmptyQueueException

Page 23: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

23

EXERCISE

Show the queue after each of the following operations: enqueue(5) enqueue(3) dequeue() enqueue(2) enqueue(8) dequeue() dequeue() dequeue() enqueue(9)

Page 24: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

24

ARRAY BASED QUEUE IMPLEMENTATION

Use an array of size in a circular fashion Two variables keep track of the front and rear

: index of the front element : index immediately past the rear element

Array location is kept empty

Q0 1 2 rf

normal configuration

Q0 1 2 fr

wrapped-around configuration

Page 25: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

25

QUEUE OPERATIONS

Use modulo operator (finds the remainder of a division)

Algorithm return Algorithm return

Q0 1 2 rf

Q0 1 2 fr

Page 26: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

26

QUEUE OPERATIONS

Operation enqueue throws an exception if the array is full This exception is implementation-dependent

Q0 1 2 rf

Q0 1 2 fr

Algorithm if then throw

Page 27: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

27

QUEUE OPERATIONS

Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT

Q0 1 2 rf

Q0 1 2 fr

Algorithm if then throw

return

Page 28: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

28

PERFORMANCE AND LIMITATIONS – ARRAY BASED QUEUE

Performance Let be the number of elements in the queue The space used is Each operation runs in time

Limitations The maximum size of the stack must be defined a priori, and cannot be changed Trying to push a new element into a full stack causes an implementation-specific

exception

Page 29: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

29

GROWABLE ARRAY BASED QUEUE

In enqueue(e), if the queue is full, similar to growable array-based stack, instead of throwing an exception, we can replace the array with a larger one

has amortized running time with the incremental strategy with the doubling strategy

Page 30: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

30

EXERCISE

Describe how to implement a queue using a singly-linked list Queue operations: , , , For each operation, give the running time

Page 31: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

31

QUEUE SUMMARY

Array with fixed capacity

Growable array (doubling)

Singly linked list

dequeue()enqueue(e) Worst Case

Best CaseAverage Case

front()size(), empty()

Page 32: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

32

DEQUE ADT (CH 5.3)

The Double-Ended Queue, or Deque, ADT stores arbitrary objects. (Pronounced ‘deck’)

Supports insertions and deletions at both ends: front and back.

Main deque operations: : inserts element at the beginning of the

deque : inserts element at the end of the deque : removes and returns the element at the front

of the queue : removes and returns the element at the end

of the queue

Auxiliary queue operations: : returns the element at the front without

removing it : returns the element at the front without

removing it : returns the number of elements stored : returns a Boolean value indicating

whether no elements are stored Exceptions

Attempting the execution of dequeue or front on an empty queue throws an EmptyDequeException

Page 33: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

33

DOUBLY LINKED LIST BASED DEQUE

The front element is stored at the first node The rear element is stored at the last node The space used is and each operation of the Deque ADT takes time

backfront

elements

Page 34: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

34

PERFORMANCE AND LIMITATIONS – DOUBLY LINKED LIST BASED DEQUE

Performance Let be the number of elements in the stack The space used is Each operation runs in time

Limitations NOTE: we do not have the limitation of the array based implementation on the size of

the stack b/c the size of the linked list is not fixed, i.e., the deque is NEVER full.

Page 35: CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

35

DEQUE SUMMARY

Array Fixed capacity

Growable array (doubling)

Singly linked list Doubly linked list

, for one at list tail, for other

, Worst CaseBest CaseAverage Case

,

,