CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Similar documents
HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Outline and Reading. The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack.

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Data Structures Lecture 5

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Stacks Goodrich, Tamassia, Goldwasser Stacks

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Foundations of Data Structures

Analysis of Algorithms

data structures and algorithms lecture 6

Stacks Goodrich, Tamassia

Data Structures Lecture 8

Lecture 3 Linear Data Structures

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

csci 210: Data Structures Stacks and Queues

Stacks Goodrich, Tamassia Stacks

Lecture 3 Linear Data Structures

COMP9024: Data Structures and Algorithms

Elementary maths for GMT. Algorithm analysis Part I

LIFO : Last In First Out

Data Structures and Algorithms

Linear Data Structures

Algorithms and Data Structures

Arrays. Array Definition

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

infix expressions (review)

Lecture 3 Linear Data Structures

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

1 P age DS & OOPS / UNIT II

DataStruct 5. Stacks, Queues, and Deques

The java.util.list ADT

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

Ch02 Data Structures

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

Ch02 Data Structures

8/19/2014. Most algorithms transform input objects into output objects The running time of an algorithm typically grows with input size

Queue. COMP 250 Fall queues Sept. 23, 2016

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists


COS 226 Algorithms and Data Structures Fall Midterm

DS L9: Queues

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Outline and Reading. Analysis of Algorithms 1

CH 6 : VECTORS, LISTS AND SEQUENCES

CS 206 Introduction to Computer Science II

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

Stack and Queue. Stack:

CH 6 : VECTORS, LISTS AND SEQUENCES

This lecture. Abstract Data Types (ADTs) The Stack ADT ( 4.2) Stacks. Stack Interface in Java. Exceptions. Abstract data types Stacks Queues

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

CH7. LIST AND ITERATOR ADTS

CS212:Data Structure

CS/CE 2336 Computer Science II

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

COMP 250 Midterm #2 March 11 th 2013

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

182 review 1. Course Goals

Data Abstraction and Specification of ADTs

Data Structures and Algorithms

Data structure and algorithm in Python

Apply to be a Meiklejohn! tinyurl.com/meikapply

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz

CSC148H Week 3. Sadia Sharmin. May 24, /20

CE204 Data Structures and Algorithms Part 2

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CMSC 132: Object-Oriented Programming II. Stack and Queue

Queues Fall 2018 Margaret Reid-Miller

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Information Science 2

CSC 1052 Algorithms & Data Structures II: Queues

CS : Data Structures

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

STACKS AND QUEUES. Problem Solving with Computers-II

CS 216 Exam 1 Fall SOLUTION

Data Structures and Algorithms " Arrays and Linked Lists!

Your Topic Goes Here Queue

CH 6. VECTORS, LISTS, AND SEQUENCES

Week 6. Data structures

Summer Final Exam Review Session August 5, 2009

ITI Introduction to Computing II

Chapter 18: Stacks And Queues

How many times is the loop executed? middle = (left+right)/2; if (value == arr[middle]) return true;

Stacks and queues (chapters 6.6, 15.1, 15.5)

ITI Introduction to Computing II

Transcription:

CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016)

ANALYSIS OF ALGORITHMS (CH 4.2-4.3) Input Algorithm Output

PSEUDOCODE High-level description of an algorithm More structured than English prose Less detailed than a program Preferred notation for describing algorithms Hides program design issues

PSEUDOCODE DETAILS Control flow if then [else ] while do repeat until for do Indentation replaces braces Method declaration Algorithm method (arg [, arg ]) Input Output Method call method (arg [, arg ]) Return value return expression Expressions: Assignment (, not =) Equality testing (= not ==) n 2 Superscripts and other mathematical formatting allowed

RUNNING TIME Most algorithms transform input objects into output objects. The running time of an algorithm typically grows with the input size. Average case time is often difficult to determine. We focus on the worst case running time. Easier to analyze Crucial to applications such as games, finance and robotics Running Time 120 100 80 60 40 20 0 best case average case worst case 1000 2000 3000 4000 Input Size

LIMITATIONS OF EXPERIMENTS It is necessary to implement the algorithm, which may be difficult Results may not be indicative of the running time on other inputs not included in the experiment. In order to compare two algorithms, the same hardware and software environments must be used

THEORETICAL ANALYSIS Uses a high-level description of the algorithm instead of an implementation Characterizes running time as a function of the input size, n (Big-Oh notation) Takes into account all possible inputs Allows us to evaluate the speed of an algorithm independent of the hardware/software environment How Count the operations!

BIG-OH NOTATION Given functions f n and g n, we say that f n is O g n if there are positive constants c and n 0 such that f n cg n for n n 0 f n - real computation time (measured time if you will) g n - approximation function Example: 2n + 10 is O(n) 2n + 10 cn 10 c 2 n Pick c = 3 and n 0 = 10 Easy method: Strip constants, and take highest order terms only! Constants do no matter because of limits as n goes to infinity

Time SEVEN IMPORTANT FUNCTIONS Seven functions that often appear in algorithm analysis: Constant 1 Logarithmic log n Linear n N-Log-N n log n Quadratic n 2 Cubic n 3 Exponential 2 n In a log-log chart, the slope of the line corresponds to the growth rate 1 2 3 4 5 6 7 8 9 10 Input Size 1 log n n n log n n^2 n^3 2^n

ABSTRACT DATA TYPES (ADTS) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations Example: ADT modeling a simple stock trading system The data stored are buy/sell orders The operations supported are order buy(stock, shares, price) order sell(stock, shares, price) void cancel(order) Error conditions: Buy/sell a nonexistent stock Cancel a nonexistent order

STACKS (CH 6.1)

STACKS A data structure similar to a neat stack of something, basically only access to top element is allowed also reffered to as LIFO (last-in, first-out) storage Direct applications Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in the Java Virtual Machine Indirect applications Auxiliary data structure for algorithms Component of other data structures

THE STACK ADT The Stack ADT stores arbitrary objects Insertions and deletions follow the lastin first-out (LIFO) scheme Main stack operations: push(e): inserts element e at the top of the stack object pop(): removes and returns the top element of the stack (last inserted element) Auxiliary stack operations: object top(): returns reference to the top element without removing it integer size(): returns the number of elements in the stack boolean isempty(): a Boolean value indicating whether the stack is empty Attempting the execution of pop or top on an empty stack return null

EXERCISE: STACKS Describe the output of the following series of stack operations Push(8) Push(3) Pop() Push(2) Push(5) Pop() Pop() Push(9) Push(1)

EXCEPTIONS VS. RETURNING NULL Attempting the execution of an operation of an ADT may sometimes cause an error condition Java supports a general abstraction for errors, called exception An exception is said to be thrown by an operation that cannot be properly executed In our Stack ADT, we do not use exceptions Instead, we allow operations pop and top to be performed even if the stack is empty For an empty stack, pop and top simply return null

METHOD STACK IN THE JVM The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a methods is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack main() { int i = 5; foo(i); } foo(int j) { int 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

ARRAY-BASED STACK A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element size() 1.return t + 1 pop() 1.if isempty() then 2. return null 3. t t 1 4.return S t + 1 S 0 1 2 t

ARRAY-BASED STACK The array storing the stack elements may become full A push operation will then throw an IllegalStateException Limitation of the array-based implementation Not intrinsic to the Stack ADT push(o) 1. if t = S. length 1 then 2. throw IllegalStateException 3. t t + 1 4. S t o S 0 1 2 t

PERFORMANCE AND LIMITATIONS - ARRAY-BASED IMPLEMENTATION OF STACK ADT 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

GROWABLE ARRAY-BASED STACK In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be? incremental strategy: increase the size by a constant c doubling strategy: double the size push Input: Element o 1. if t = S. length 1 then 2. A new array of size 3. for i 0 to t do 4. A i S[i] 5. S A 6. S t o 7. t t + 1

COMPARISON OF THE STRATEGIES We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n 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., T(n)/n

INCREMENTAL STRATEGY ANALYSIS Let c be the constant increase and n be the number of push operations We replace the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + + kc = n + c 1 + 2 + 3 + + k k(k + 1) = n + c 2 = O n + k 2 = O n + n2 c 2 = O n 2 T(n) is O(n 2 ) so the amortized time of a push is O n2 n = O(n) Side note: 1 + 2 + + k = = k i=0 i k k + 1 2

DOUBLING STRATEGY ANALYSIS We replace the array k = log 2 n times 2 4 The total time T(n) of a series of n push operations is proportional to n + 1 + 2 + 4 + 8 + + 2 k = n + 2 k+1 1 = O n + 2 k = O n + 2 log 2 n = O n 1 1 8 T(n) is O n so the amortized time of a push is O n = O(1) n

EXERCISE Describe how to implement a stack using a singly-linked list Stack operations: push(e), pop(), size(), isempty() For each operation, give the running time

STACK WITH A SINGLY LINKED LIST 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 O(n) and each operation of the Stack ADT takes O(1) time nodes top elements

STACK SUMMARY Array Fixed-Size Array Expandable (doubling strategy) List Singly-Linked pop() O(1) O(1) O(1) push(o) O(1) O(n) Worst Case O(1) Best Case O(1) Average Case O(1) top() O(1) O(1) O(1) size(), empty() O(1) O(1) O(1)

QUEUES (CH 6.2)

APPLICATIONS OF QUEUES Direct applications Waiting lines Access to shared resources (e.g., printer) Multiprogramming Indirect applications Auxiliary data structure for algorithms Component of other data structures

THE QUEUE ADT The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out (FIFO) scheme Insertions are at the rear of the queue and removals are at the front of the queue Main queue operations: enqueue(e): inserts element e at the end of the queue object dequeue(): removes and returns the element at the front of the queue Auxiliary queue operations: object first(): returns the element at the front without removing it integer size(): returns the number of elements stored boolean isempty(): indicates whether no elements are stored Boundary cases Attempting the execution of dequeue or front on an empty queue returns null

EXERCISE: QUEUES Describe the output of the following series of queue operations enqueue(8) enqueue(3) dequeue() enqueue(2) enqueue(5) dequeue() dequeue() enqueue(9) enqueue(1)

ARRAY-BASED QUEUE Use an array of size N in a circular fashion Two variables keep track of the front and rear f index of the front element sz number of stored elements When the queue has fewer than N elements, array location r (f + sz) mod N is the first empty slot past the rear of the queue Q normal configuration 0 1 2 f r Q wrapped-around configuration 0 1 2 r f

QUEUE OPERATIONS We use the modulo operator (remainder of division) size() 1.return sz isempty() 1.return sz = 0 Q Q 0 1 2 f r 0 1 2 r f

QUEUE OPERATIONS Operation enqueue throws an exception if the array is full This exception is implementation-dependent Q enqueue(o) 1.if size() = N 1 then 2. throw IllegalStateException 3. r f + sz mod N 4. Q r o 5. sz sz + 1 0 1 2 f r Q 0 1 2 r f

QUEUE OPERATIONS Operation dequeue returns null if the queue is empty dequeue() 1.if empty() then 2. return null 3. o Q[f] 4. f f + 1 mod N 5. sz sz 1 6.return o Q Q 0 1 2 f r 0 1 2 r f

PERFORMANCE AND LIMITATIONS - ARRAY-BASED IMPLEMENTATION OF QUEUE ADT Performance Let n be the number of elements in the queue The space used is O(n) Each operation runs in time O(1) Limitations The maximum size of the queue must be defined a priori, and cannot be changed

GROWABLE ARRAY-BASED QUEUE In enqueue(e), when the array is full, instead of throwing an exception, we can replace the array with a larger one Similar to what we did for an array-based stack enqueue(e) has amortized running time O(n) with the incremental strategy O(1) with the doubling strategy

EXERCISE Describe how to implement a queue using a singly-linked list Queue operations: enqueue(e), dequeue(), size(), empty() For each operation, give the running time

QUEUE WITH A SINGLY LINKED LIST The front element is stored at the head of the list, The rear element is stored at the tail of the list The space used is O(n) and each operation of the Queue ADT takes O(1) time 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 queue is NEVER full. r nodes front f rear elements

QUEUE SUMMARY Array Fixed-Size Array Expandable (doubling strategy) List Singly-Linked dequeue() O(1) O(1) O(1) enqueue(o) O(1) O(n) Worst Case O(1) Best Case O(1) Average Case O(1) front() O(1) O(1) O(1) size(), empty() O(1) O(1) O(1)

THE DOUBLE-ENDED QUEUE ADT (CH. 6.3) The Double-Ended Queue, or Deque, ADT stores arbitrary objects. (Pronounced deck ) Richer than stack or queue ADTs. Supports insertions and deletions at both the front and the end. Main deque operations: addfirst(e): inserts element e at the beginning of the deque addlast(e): inserts element e at the end of the deque object removefirst(): removes and returns the element at the front of the queue object removelast(): removes and returns the element at the end of the queue Auxiliary queue operations: object first(): returns the element at the front without removing it object last(): returns the element at the front without removing it integer size(): returns the number of elements stored boolean isempty(): indicates whether no elements are stored

DEQUE WITH A DOUBLY LINKED LIST The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Deque ADT takes O(1) time first last elements

PERFORMANCE AND LIMITATIONS - DOUBLY LINKED LIST IMPLEMENTATION OF DEQUE ADT Performance Let n be the number of elements in the deque The space used is O(n) Each operation runs in time O(1)

DEQUE SUMMARY Array Fixed-Size Array Expandable (doubling strategy) List Singly-Linked List Doubly-Linked removefirst(), removelast() O(1) O(1) O(n) for one at list tail, O(1) for other O(1) addfirst(o), addlast(o) O(1) O(n) Worst Case O(1) Best Case O(1) Average Case first(), last() O(1) O(1) O(1) O(1) O(1) O(1) size(), isempty() O(1) O(1) O(1) O(1)

INTERVIEW QUESTION 1 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? push, pop and min should all operate in O(1) time

INTERVIEW QUESTION 2 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (i.e., each disk sits on top of an even larger one). You have the following constraints: (1) Only one disk can be moved at a time. (2) A disk is slid off the top of one tower onto the next tower. (3) A disk can only be placed on top of a larger disk. Write pseudocode to move the disks from the first tower to the last using stacks.