Expanding Stacks & Queues. CS16: Introduction to Data Structures & Algorithms Seny Kamara - Spring 2017

Similar documents
Data Structures and Algorithms Stacks and Queues

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

DATA STRUCTURE - STACK

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Analysis of a Search Algorithm

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

STACKS,QUEUES, AND LINKED LISTS

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

CmpSci 187: Programming with Data Structures Spring 2015

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Programming with Data Structures

DATA STRUCTURE - QUEUE

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Sequential Data Structures

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Common Data Structures

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Data Structures in the Java API

Data Structures and Algorithms Lists

Introduction to Stacks

Chapter 3: Restricted Structures Page 1

7.1 Our Current Model

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

St S a t ck a ck nd Qu Q eue 1

Linked Lists Linked Lists, Queues, and Stacks

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Cpt S 223. School of EECS, WSU

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Algorithms and Data Structures

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Introduction to Data Structures and Algorithms

Universidad Carlos III de Madrid

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Data Structures Fibonacci Heaps, Amortized Analysis

Stacks. Linear data structures

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Monitors, Java, Threads and Processes

Abstract Data Types. Chapter 2

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Data Structures and Algorithms V Otávio Braga

Data Structures and Algorithms

Chapter 8: Bags and Sets

Stack Allocation. Run-Time Data Structures. Static Structures

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Ordered Lists and Binary Trees

PES Institute of Technology-BSC QUESTION BANK

Data Structures and Data Manipulation

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Algorithms and Abstract Data Types

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Data Structures. Level 6 C Module Descriptor

COMPUTER SCIENCE. Paper 1 (THEORY)

Unordered Linked Lists

Algorithms and Data Structures

DATA STRUCTURES USING C

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

ADTs,, Arrays, Linked Lists

Module 2 Stacks and Queues: Abstract Data Types

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

Algorithms and Data Structures Written Exam Proposed SOLUTION

CompSci-61B, Data Structures Final Exam

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Introduction to Data Structures

Data Structures and Algorithms

D06 PROGRAMMING with JAVA

Software Engineering Techniques

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

&DUORV'HOJDGR.ORRV,QJHQLHUtD7HOHPiWLFD 8QLY&DUORV,,,GH0DGULG. Copyright 2001 Java: Pilas y Colas / 1

14 Stacks, Queues, And Linked Lists

7. Object-Oriented Programming

recursion, O(n), linked lists 6/14

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

10CS35: Data Structures Using C

PARALLELIZED SUDOKU SOLVING ALGORITHM USING OpenMP

Stacks. Data Structures and Data Types. Collections

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Collaboration policy. Where to get help (but no code in ) Office hours Lab TAs in Friend 008/009 Bounce ideas (but not code) off classmates

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Basic Data Structures and Algorithms

Scheduling Programming Activities and Johnson's Algorithm

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Habanero Extreme Scale Software Research Project

Transcription:

Expanding Stacks & Queues CS16: Introduction to Data Structures & Algorithms Seny Kamara - Spring 2017

This is Hard! You will understand it

Outline Abstract data types Stacks Capped-capacity Expandable Amortized analysis Queues Expandable queues

Abstract Data Types Abstraction of a data structure Specifies functionality type of data stored operations it can perform Like a Java interface Specifies name & purpose of methods But not implementations 5

Stacks Stores arbitrary objects Operations Push: adds object Pop: returns last object LIFO: last-in first-out Implemented Linked list, array,

Stack ADT push(object): inserts object object pop( ): returns and removes last inserted object int size( ): returns number objects in stack boolean isempty( ): returns TRUE if empty; FALSE otherwise 7

Capped-capacity Stack Array-based Stack Store objects in array keep pointer to last inserted object Problem? Size of stack bounded by size of array :-( 8

Capped-capacity Stack Stack( ): data array of size 20 count 0 function push(object):????? function isempty( ): return count 0 function size( ):????? function pop( ):????? Activity #1

Capped-capacity Stack Stack( ): data array of size 20 count 0 function isempty( ): return count 0 function size( ):????? function push(object):????? 2 minactivity function pop( ):????? #1

Capped-capacity Stack Stack( ): data array of size 20 count 0 function isempty( ): return count 0 function size( ):????? function push(object):????? 1 minactivity function pop( ):????? #1

Capped-capacity Stack Stack( ): data array of size 20 count 0 function isempty( ): return count 0 function size( ):????? function push(object):????? 0 minactivity function pop( ):????? #1

Capped-capacity Stack Stack( ): data array of size 20 count 0 function push(object): if count < 20: data[count] object count++ else: error( overfull ) O(1) function size( ): return count O(1) function isempty( ): return count 0 O(1) function pop( ): if count 0: error( empty stack ) else: count-- return data[count] O(1)

Expandable Stack Capped-capacity stack is fast but not useful in practice How can we design an uncapped Stack? Strategy #1: Incremental increase size of array by constant c when full Strategy #2: Doubling double size of array when full Arrays can t be resized! Can only be copied 14

Expandable Stack Stack( ): data array of size 20 count 0 capacity 20 run time when not expanding? when does it expand? function push(object): data[count] object count++ if count capacity new_capacity capacity + c /* incremental */ capacity * 2 /* doubling */ new_data array of size new_capacity for i 0 to capacity - 1 new_data[i] data[i] capacity new_capacity data new_data

Expandable Stack function push(object): data[count] object count++ if count capacity new_capacity capacity + c /* incremental */ capacity * 2 /* doubling */ new_data array of size new_capacity for i 0 to capacity - 1 new_data[i] data[i] capacity new_capacity data new_data Run time when not expanding: O(1) When does it expand? after n pushes, where n is capacity of array 16

Incremental & Doubling Incremental (5) Doubling 50 50 38 38 Cost 25 Cost 25 13 13 0 0 10 20 30 40 O(1) O(1) O(n) O(1) O(1) Push number 17 0 0 10 20 30 40 O(1) O(n) Push number

Incremental & Doubling What is the running time of incremental? O(1) or O(n)? What is the running time of doubling? O(1) or O(n)? It depends 18

What s going on?

Expandable Stack Stack( ): data array of size 20 count 20 capacity 20 Run time depends on state/history function push(object): data[count] object count++ if count capacity new_capacity capacity + c /* incremental */ capacity * 2 /* doubling */ new_data array of size new_capacity for i 0 to capacity - 1 new_data[i] data[i] capacity new_capacity data new_data

Incremental & Doubling What is the running time of incremental? O(1) or O(n)? What is the running time of doubling? O(1) or O(n)? It depends Measure cost on sequence of inputs not a single input! 21

Towards Amortized Analysis For certain algorithms better to measure total running time on sequence of operations instead of running time on single operation T(n): total cost on sequence of n operations Not running time on a single input Usually the case for data structure operations ex: Stack T(n): cost push #1 + cost push #2 + + cost push #n 22

Amortized Analysis Instead of reporting total cost of sequence report cost of sequence per operation n 23

Amortized Analysis of Incremental Stack with capacity 5 Expands by c 5 5th push brings to capacity Objects copied to new array of size 5+c 10 Total cost per push over 5 pushes? 24

Amortized Analysis of Incremental Stack with capacity 5 Expands by c 5 n : 5+c 5 5+5 5 2 Is each push O(1)? Cost of 5 pushes 25 Cost of expansion

Amortized Analysis of Incremental What if we push 5 more objects? O(1) until 10th push brings to capacity then all 10 objects copied to new array of size 10+c 15 n : 10 + c +2c 10 10 + 5 + 10 10 2.5 Cost of 2nd expansion Cost of 10 pushes Cost of 1st expansion 26

Amortized Analysis of Incremental n : T (10) 10 10 + c +2c 10 10 + 5 + 10 10 2.5 n : T (15) 15 15 + c +2c +3c 15 15 + 5 + 10 + 15 15 3 n : T (20) 20? Activity #2 27

Amortized Analysis of Incremental n : T (10) 10 10 + c +2c 10 10 + 5 + 10 10 2.5 n : T (15) 15 15 + c +2c +3c 15 15 + 5 + 10 + 15 15 3 n : T (20) 20 1 min 28? Activity #2

Amortized Analysis of Incremental n : T (10) 10 10 + c +2c 10 10 + 5 + 10 10 2.5 n : T (15) 15 15 + c +2c +3c 15 15 + 5 + 10 + 15 15 3 n : T (20) 20 0 min 29? Activity #2

Amortized Analysis of Incremental n : T (10) 10 10 + c +2c 10 10 + 5 + 10 10 2.5 n : T (15) 15 15 + c +2c +3c 15 15 + 5 + 10 + 15 15 3 n : T (20) 20 20 + c +2c +3c +4c 20 20 + 5 + 10 + 15 + 20 20 3.5 So on and so forth Looks linear 30

Amortized Analysis of Incremental n pushes w/o exp. cost of exp. # n/c n + c +2c +3c + + n c c n + c 1+2+ + n c n + c 1 n n 2 c c +1 n + n2 /c + n O(n 2 ) 2 31 distributing & simplifying: n factoring out c using: (1 + 2 + + k) O(n) k (k + 1) 2

Amortized Analysis of Incremental Summary Total cost of n pushes: T(n) O(n 2 ) Amortized cost of n pushes: T(n)/n O(n) 32

Amortized Analysis of Doubling ex: doubling stack with initial capacity 5? pushes are O(1) until 5th push cost of pushes then O(n) n : T (10) 10 n : T (20) 20 n : T (5) 5 5+5 5 10 + 5 + 10 10 20 + 5 + 10 + 20 20 2 2.5 2.75 cost of exp cost of exp #2 cost of exp #3 33

Amortized Analysis of Doubling cost of n pushes cost of last exp cost of second to last exp n + n + n 2 + n 4 + + n 2 k 1 n + n 1+ 1 2 + 1 4 + + 1 2 k 1 cost of exp #1 <n+ n 2 3n using: lim k!1 kx i0 1 2 i 2 n O(1) 34

Amortized Analysis Summary for Incremental Total cost of n pushes: T(n) O(n 2 ) Amortized cost of n pushes: T(n)/n O(n) Summary for Doubling Total cost of n pushes: T(n) O(n) Amortized cost of n pushes: T(n)/n O(1) 35

Way to Think about Amortized Each fast operation adds some credit Need enough credits to execute slow operation 36

Queue ADT enqueue(object): object dequeue( ): returns and removes first inserted object int size( ): inserts object returns number objects in queue boolean isempty( ): returns TRUE if empty; FALSE otherwise 37

Expandable Queue head tail Can be implemented with expandable array need to keep track of head and tail What happens when tail reaches end? Is the queue full? So when should we expand array? 38

Expandable Queue tail head Wrap around until array is completely full When expanding re-order objects properly 39

Expandable Queue function enqueue(object): if size capacity double array and copy contents reset head and tail pointers data[tail] object tail (tail + 1) % capacity size++ function dequeue( ): if size 0 error( queue empty ) element data[head] head (head + 1) % capacity size-- return element n O(1) 40