dynamic arrays and stacks data structures and algorithms cs 244 brent m. dingle, ph.d. department of...

106
Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content from Data Structures Using C++ (D.S. Malik)

Upload: rosa-moody

Post on 06-Jan-2018

224 views

Category:

Documents


1 download

DESCRIPTION

Points of Note Assignment 7 is posted Due soon: Wednesday, April 2 Assignment 8 is posted Due Thursday, April 3 Assignment 9 is posted Due Thursday, April 10

TRANSCRIPT

Page 1: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Dynamic Arrays and Stacks

Data Structures and AlgorithmsCS 244

Brent M Dingle PhDDepartment of Mathematics Statistics and Computer ScienceUniversity of Wisconsin ndash Stout

Based on the book Data Structures and Algorithms in C++ (Goodrich Tamassia Mount)Some content from Data Structures Using C++ (DS Malik)

Points of Note

bull Assignment 7 is postedbull Due soon Wednesday April 2

bull Assignment 8 is postedbull Due Thursday April 3

bull Assignment 9 is postedbull Due Thursday April 10

Previously

bull Talked about Dynamic Arraysbull With a Pitcher Example

Today

bull Stacks ndash Chapter 5bull What are theybull How are the used

bull How can we implement thembull Static Arraybull Dynamic Array

bull Amortized Timebull Linked Lists

Marker Slidebull Questions

bull Next upbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Stack Introbull We are now to chapter 5 of the book

bull Unit 2 on D2L

bull You are now all assumed to be proficient at C++ and familiar with the processes needed to analyze algorithms using Big-Oh notationbull We may still review from time to time

bull We will be discussing Stacks todaybull As an ADT ndash what are theybull Using Static Arrays to implement the Stack ADTbull Using Dynamic Arrays to implement the Stack ADTbull Using Linked Lists to implement the Stack ADT

Stacks

Stacksbull Stacks store arbitrary objects

(Pez in this case)

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 2: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Points of Note

bull Assignment 7 is postedbull Due soon Wednesday April 2

bull Assignment 8 is postedbull Due Thursday April 3

bull Assignment 9 is postedbull Due Thursday April 10

Previously

bull Talked about Dynamic Arraysbull With a Pitcher Example

Today

bull Stacks ndash Chapter 5bull What are theybull How are the used

bull How can we implement thembull Static Arraybull Dynamic Array

bull Amortized Timebull Linked Lists

Marker Slidebull Questions

bull Next upbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Stack Introbull We are now to chapter 5 of the book

bull Unit 2 on D2L

bull You are now all assumed to be proficient at C++ and familiar with the processes needed to analyze algorithms using Big-Oh notationbull We may still review from time to time

bull We will be discussing Stacks todaybull As an ADT ndash what are theybull Using Static Arrays to implement the Stack ADTbull Using Dynamic Arrays to implement the Stack ADTbull Using Linked Lists to implement the Stack ADT

Stacks

Stacksbull Stacks store arbitrary objects

(Pez in this case)

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 3: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Previously

bull Talked about Dynamic Arraysbull With a Pitcher Example

Today

bull Stacks ndash Chapter 5bull What are theybull How are the used

bull How can we implement thembull Static Arraybull Dynamic Array

bull Amortized Timebull Linked Lists

Marker Slidebull Questions

bull Next upbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Stack Introbull We are now to chapter 5 of the book

bull Unit 2 on D2L

bull You are now all assumed to be proficient at C++ and familiar with the processes needed to analyze algorithms using Big-Oh notationbull We may still review from time to time

bull We will be discussing Stacks todaybull As an ADT ndash what are theybull Using Static Arrays to implement the Stack ADTbull Using Dynamic Arrays to implement the Stack ADTbull Using Linked Lists to implement the Stack ADT

Stacks

Stacksbull Stacks store arbitrary objects

(Pez in this case)

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 4: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Today

bull Stacks ndash Chapter 5bull What are theybull How are the used

bull How can we implement thembull Static Arraybull Dynamic Array

bull Amortized Timebull Linked Lists

Marker Slidebull Questions

bull Next upbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Stack Introbull We are now to chapter 5 of the book

bull Unit 2 on D2L

bull You are now all assumed to be proficient at C++ and familiar with the processes needed to analyze algorithms using Big-Oh notationbull We may still review from time to time

bull We will be discussing Stacks todaybull As an ADT ndash what are theybull Using Static Arrays to implement the Stack ADTbull Using Dynamic Arrays to implement the Stack ADTbull Using Linked Lists to implement the Stack ADT

Stacks

Stacksbull Stacks store arbitrary objects

(Pez in this case)

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 5: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Marker Slidebull Questions

bull Next upbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Stack Introbull We are now to chapter 5 of the book

bull Unit 2 on D2L

bull You are now all assumed to be proficient at C++ and familiar with the processes needed to analyze algorithms using Big-Oh notationbull We may still review from time to time

bull We will be discussing Stacks todaybull As an ADT ndash what are theybull Using Static Arrays to implement the Stack ADTbull Using Dynamic Arrays to implement the Stack ADTbull Using Linked Lists to implement the Stack ADT

Stacks

Stacksbull Stacks store arbitrary objects

(Pez in this case)

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 6: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack Introbull We are now to chapter 5 of the book

bull Unit 2 on D2L

bull You are now all assumed to be proficient at C++ and familiar with the processes needed to analyze algorithms using Big-Oh notationbull We may still review from time to time

bull We will be discussing Stacks todaybull As an ADT ndash what are theybull Using Static Arrays to implement the Stack ADTbull Using Dynamic Arrays to implement the Stack ADTbull Using Linked Lists to implement the Stack ADT

Stacks

Stacksbull Stacks store arbitrary objects

(Pez in this case)

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 7: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stacks

Stacksbull Stacks store arbitrary objects

(Pez in this case)

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 8: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stacksbull Stacks store arbitrary objects

(Pez in this case)

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 9: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 10: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 11: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 12: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 13: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 14: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 15: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stacksbull Stacks store arbitrary objects

(Pez in this case)bull Operations

ndash push(e) inserts an element to the top of the stack

ndash pop() removes and returns the top element of the stack

ndash top() returns a reference to the top element of the stack but doesnrsquot remove it

bull Optional operationsndash size() returns the number of

elements in the stackndash empty() returns a bool indicating if

the stack contains any objects

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 16: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack Exceptionsbull Attempting to execute an operation of ADT may

cause an error condition called an exceptionbull Exceptions are said to be ldquothrownrdquo by an

operation that cannot be executedbull In the Stack ADT pop and top cannot be

performed if the stack is emptybull Attempting to execute pop or top on an empty

stack throws an EmptyStackException

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 17: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Class Exercise Stacksbull Describe the output and final structure of the stack after the

following operationsndash Push(8)ndash Push(3)ndash Pop()ndash Push(2)ndash Push(5)ndash Pop()ndash Pop()ndash Push(9)ndash Push(1)

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 18: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Marker Slidebull Questions on

bull Stacksbull Description

bull Next upbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 19: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

So far Stacks

bull A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack)bull Common place examples

bull pancakes plates trays

bull Items in a stack must be removed in the reverse order that they were added to the stackbull This is referred to as a Last-InFirst Out (LIFO)

structure

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 20: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Other Applications of Stacks

bull Direct Applicationsbull Visited Page history of a web-browserbull Undo sequence in a text editorbull Saving local variables when one function calls another and

that one calls yet another andhellip

bull Indirect Applicationsbull Auxiliary data structure for algorithmsbull Component of other data structures

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 21: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 22: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 23: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

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

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 24: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

foo PC = 3 j = 5 k = 6

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 25: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

C++ Run-time Stack

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

bull When a function is called the run-time system pushes on the stack a frame containingndash Local variables and return valuendash Program counter keeping track of

the statement being executed bull When a function returns its

frame is popped from the stack and control is passed back to the function that called it

main() int i

i = 5foo(i)

foo(int j) int kk = j+1bar(k)

bar(int m) hellip

main PC = 2 i = 5

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 26: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Application

bull Next upbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 27: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 28: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 29: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

(static) Array-based Stack

bull A simple way of implementing the Stack ADT uses an array

bull We add elements from left to right

bull A variable keeps track of the index of the top element

S0 1 2 t

hellip

Algorithm size()return t + 1

Algorithm empty() return size () == 0

Algorithm pop()if empty() then

throw EmptyStackException

else t t 1return S[t + 1]

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 30: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

(static) Array-based Stack

bull The array storing the stack elements may become full

bull A push operation will then throw a FullStackExceptionndash Limitation of the array-

based implementationndash Not intrinsic to the Stack

ADT

S0 1 2 t

hellip

Algorithm push(e)if t = Slength 1 then

throw FullStackException

else t t + 1S[t] e

>

SoundJaycom Sound Effects

track 1

2010

10128

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 31: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Performance and Limitations (array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 32: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

General Stack Interface in C++

bull Requires the definition of a classbull EmptyStackException

bull Most similar in STL to stdvector

template lttypename Typegtclass Stack public int size() bool isEmpty() Typeamp top()

throw(EmptyStackException) void push(Type e) Type pop()

throw(EmptyStackException)

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 33: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

template ltclass Typegtclass ArrayStackprivate int capacity stack capacity Type S stack array int top top of stack

public ArrayStack(int c) capacity(c) S = new Type [capacity] top = -1

bool isEmpty() return top lt 0

Type pop() throw(EmptyStackException) if ( isEmpty() ) throw EmptyStackException(Popping from empty stack) return S[ top-- ] hellip (other functions omitted)

Array-based Stack in C++

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 34: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stacks ndash Fun Application

bull Word Reversalbull LOVE becomes EVOL

bull Useful for finding palindromesbull Radar becomes

radaR

bull Step on no Pets becomes steP on no petS

Side

track

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 35: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Math Check ndash Application

bull Stacks are often used for evaluating math formulasbull For example checking for matching

parenthesesbull ( ( x + y ( z + 7 ) ) (a + b) )

bull Processing the line left to rightbull Each open paren ( equates to a pushbull Each closed paren ) is a popbull If matched the stack is empty at the end

Side

track

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 36: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Performance and Limitations (Static Array Implementation of Stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori

and cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Back

on

Trac

k

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 37: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

End Static ndash Begin Dynamic

bull Static arrays can be used to implement stacksbull But have limitations (previous slide)

bull Perhaps Dynamic Arrays will be better

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 38: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Based

bull Next upbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 39: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Dynamic (growable) Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant cndash doubling strategy double the

size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] oDid we see these

options before

With c = 2

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 40: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

So which will be better

bull Incremental Strategybull Increasing the array size by a constant c

bull Doubling Strategybull Doubling the array size

bull The answer is found using amortized time

Did we see these options before

With c = 2

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 41: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Next upbull Amortization

bull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 42: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Amortization (common use)

bull Amortization (definition)

bull Any guesses at this

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 43: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Amortization (common use)

bull Amortization (definition)bull The process of decreasing an amount over time

bull This shows up in several places in ldquoreal liferdquobull Such as

bull Home Loansbull Business Payments

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 44: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life

of a loan

bull A portion of the payment is applied towards principle and a portion is applied toward interest

bull The ldquocostrdquo is stretched out over timebull Each payment is paying a small amount of

what would be a large payment if paid all at once

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 45: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Amortization (common use)bull Amortization (definition)

bull The process of decreasing an amount over time

bull Home Loansbull Amortization is the process by which loan principle decreases over the life of a

loanbull A portion of the payment is applied towards principle and a portion is applied

toward interestbull The ldquocostrdquo is stretched out over time

bull Each payment is paying a small amount of what would be a large payment if paid all at once

bull Businessbull Amortization allocates a lump sum (payment) amount to different time periods

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 46: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Amortization (CS concept)bull Back in Computer Science world

bull Certain operations may be extremely costly

bull BUT

bull They cannot occur frequently enough to slow down the entire programbull The less costly operations far outnumber the costly onebull Thus over the long-term they are ldquopaying backrdquo the program over a

number of iterations

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 47: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Amortized Analysis

bull Requires knowledge about the entire series of operationsbull Usually where a state persists between operations

bull Like the capacity of memory allocated

bull The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a ldquolongrdquo timebull thus amortizing its cost

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 48: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Applying Amortization Analysis(aka Aggregate Analysis)

bull Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operationsbull T(n) is what we have been calculating previously

for our Big-Oh stuff

bull Then the amortized cost isbull T(n) nbull because we make ldquosmall paymentsrdquo for the worst

operation across each operation

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 49: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Marker Slidebull Questions on

bull Stacksbull Descriptionbull Applicationbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Description

bull Next upbull Amortization

bull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 50: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Dynamic Array-based Stack

bull In a push operation when the array is full instead of throwing an exception we can replace the array with a larger one

bull How large should the new array bendash incremental strategy increase

the size by a constant c (say c = 2)

ndash doubling strategy double the size

Algorithm push(e)if t =

Slength 1 thenA

new array of

size hellipfor i

0 to t do

A[i] S[i]S A

t t + 1S[t] o

Recall

Recall we used

c = 2

for the Pitcher class

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 51: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Apply to +2(incremental) vs double

bull 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

bull Assume we start with an empty stack represented by an array of size 1

bull We call the amortized time of a push operationbull the average time taken by a push over the series of operations bull ie T(n) n

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 52: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 53: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 54: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

How do we know it will replace the array k = nc times

Think how many ldquogroups of size crdquo are in a set of n things nc

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 55: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 56: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 57: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 2 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 58: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 59: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need 2 + 4 + 6 + 8 + hellip + 2k units of time total time = n + 21 + 22 + 23 + 24 +hellip+2k

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 60: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 22 + 23 + 24 +hellip+2k

but we were using c = 2 for thathellip now put the c back in

total time = n + c + c2 + c3 + c4 +hellip+ ck

Next we simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 61: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O( )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 62: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) =

n stays nc(k2 + k)2 = (c2)k2 + k2 =gt k2

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 63: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2)

Substitute innc for kand simplify

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 64: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

total time = n + c + c2 + c3 + c4 +hellip+ ck= n + c(1 + 2 + 3 + 4 +hellip + k)

= n + c = n + c

So hellip T(n) is O(n + k2) = O(n + n2) = O( n2 )

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 65: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Incremental Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = nc times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull bull

bull

So hellip T(n) is O(n + k2) = O(n + n2)

And the Amortized Time is T(n)n

= O( n2 )

= O( n )

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 66: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Summary So Far

bull Amortized Analysis tells usbull Incremental Increase Method is

bull O(n)

bull Next we do similar for the Doubling Method

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 67: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizing

bull Next upbull Amortization

bull Applied to Doubling Increase for Dynamic Array Resizing

bull Stack Implementation Analysisbull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 68: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 69: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Think on How do we know it replaces the array k = lg n times lg n is the number of times n can be divided by 2hellip

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 70: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 71: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 72: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 73: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

We start with an array of capacity 2 and size 0 (empty)

Assume a call to push() takes time 1 unit we will push n things one at a time so need n time

Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip

Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip

We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 74: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 75: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n + Put into Summation Notation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 76: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)Simplify the Summation

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 77: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

Take a 2 out

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 78: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)Substitute lg n in for k

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 79: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k

= n +

= n + (2k+1 ndash 1)

= n + (2 2k ndash 1)

= n + (22lg n ndash 1)

= n + (2n ndash 1) = 3n - 1

Simplify

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 80: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Doubling Analysisbull Say our array grows to a final size of n

bull Then this strategy replaces the array k = log2 n times

bull The total time T(n) of a series of n push operations is proportional tobull

bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n

bull The amortized time is O(n)

total time = n + 2 + 4 + 8 + 16 + hellip + 2k = 3n ndash 1

So T(n) is O(n)

and the amortized time T(n) n = O(n) n = O( 1 )

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 81: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizing

bull Next upbull Stack Implementation Analysis

bull Static Array versus Dynamic Array

bull Linked List Refresher

bull Stack Implemented as a Linked List

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 82: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Conclusions of Analysis

bull So what did we learn

bull If we use a dynamic array the amortized time for a push operation is O(1)

bull Why do we care hellip Recall next slide

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 83: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 84: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Dynamic Arraysclearly fix thishellipBUThellip

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 85: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

Seemed to fail on this point

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 86: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Performance and Limitations (static array-based implementation of stack ADT)

bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)

runs in time O(1)

bull Limitationsndash The maximum size of the stack must be defined a priori and

cannot be changedndash Trying to push a new element onto a full stack causes an

implementation-specific exception

Recall

But dynamic arrays are good here toohellipper the amortized analysis of doubling the capacity

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 87: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

ConclusionImplementing Stack Using Dynamic Array

bull Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack

bull Doing so does NOT limit the stack sizebull like a static array

bull Amortization Analysis is required to see how it is also an efficient way to implement a Stack

bull Intuitively it is not necessarily obvious

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 88: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Next upbull Linked List Refresher head towards Stacks again

bull Stack Implemented as a Linked List

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 89: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 90: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 91: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Singly Linked Listbull A singly linked list is a structure

consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

next

elem node

Leonard Sheldon Howard Raj

head tail

Revie

w

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 92: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Singly Linked List Node

next

elem node

template lttypename Typegtclass SLinkedListNode public Type elem SLinkedListNodeltTypegt next

Leonard Sheldon Howard Raj

Revie

w

bull A singly linked list is a structure consisting of a sequence of nodes

bull A singly linked list stores a pointer to the first node (head) and last (tail)

bull Each node storesndash elementndash link to the next node

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 93: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Singly Linked List

bull A singly linked list is a structure consisting of a sequence of nodes

bull Operationsndash insertFront(e) inserts an element on the front of

the listndash removeFront() returns and removes the element at

the front of the listndash insertBack(e) inserts an element on the back of

the listndash removeBack() returns and removes the element at

the end of the list

Revie

w

Details of each of these operationswas given in previously

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 94: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Marker Slidebull Questions on

bull Stacksbull STATIC Array Basedbull DYNAMIC Array Based

bull Amortizationbull Descriptionbull Applied to Incremental Increase for Dynamic Array Resizingbull Applied to Doubling Increase for Dynamic Array Resizingbull Static Array versus Dynamic Array

bull Linked List Refresher head towards Stacks again

bull Next upbull Stack Implemented as a Linked List

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 95: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

So far

bull Stacks implemented usingbull Static Arraysbull Dynamic Arrays (also in the MiniStack homework)

bull Nextbull Linked Lists

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 96: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack with a Singly Linked Listbull CLAIM

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) and each operation of the Stack ADT takes

O(1) timebull Demonstration of how follows

t

nodes

elements

top

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 97: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Reca

ll

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 98: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Top is the First Node

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 99: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 100: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 101: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

ndash insertBack(e) inserts an element on the back of the list

ndash removeBack() returns and removes the element at the end of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 102: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

top() would require a minoralteration or addition to LinkedListvery similar to removeFront()

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 103: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack and Singly Linked Listbull Singly linked list Operations

ndash insertFront(e) inserts an element on the front of the list

ndash removeFront() returns and removes the element at the front of the list

bull Stack Operationsbull push(e) inserts an element to the

top of the stackbull pop() removes and returns the top

element of the stack

bull top() returns a reference to the top element of the stack but doesnrsquot remove it

bull size() returns the number of elements in the stack

bull empty() returns a bool indicating if the stack contains any objects

size() and isEmpty() would requirethe addition of a counter that incrementseach time push() is called anddecrements when pop() is called

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 104: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack with a Singly Linked Listbull CONCLUSION

ndash We can implement a stack with a singly linked listndash The top element of the stack is the first node of the listndash The space used is O(n) ndash and each operation of the Stack ADT takes O(1) time

bull push pop top size empty each are O(1) time

t

nodes

elements

top

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 105: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

Stack Summarybull Stack Operation Complexity for Different Implementations

Array Fixed-Size

ArrayDynamic (doubling strategy)

SinglyLinkedList

Pop() O(1) O(1) O(1)

Push(o) O(1) O(n) Worst CaseO(1) Best CaseO(1) Average Case

O(1)

Top() O(1) O(1) O(1)

Size() isEmpty() O(1) O(1) O(1)

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End
Page 106: Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University

The End

bull For next timebull Read Chapter 5

bull Stacks and Queues

  • Dynamic Arrays and Stacks
  • Points of Note
  • Previously
  • Today
  • Marker Slide (3)
  • Stack Intro
  • Stacks
  • Stacks (2)
  • Stacks (3)
  • Stacks (4)
  • Stacks (5)
  • Stacks (6)
  • Stacks (7)
  • Stacks (8)
  • Stacks (9)
  • Stack Exceptions
  • Class Exercise Stacks
  • Marker Slide (4)
  • So far Stacks
  • Other Applications of Stacks
  • C++ Run-time Stack
  • C++ Run-time Stack (2)
  • C++ Run-time Stack (3)
  • C++ Run-time Stack (4)
  • C++ Run-time Stack (5)
  • Marker Slide (5)
  • (static) Array-based Stack
  • (static) Array-based Stack (2)
  • (static) Array-based Stack (3)
  • (static) Array-based Stack (4)
  • Performance and Limitations (array-based implementation of sta
  • General Stack Interface in C++
  • Array-based Stack in C++
  • Stacks ndash Fun Application
  • Math Check ndash Application
  • Performance and Limitations (Static Array Implementation of St
  • End Static ndash Begin Dynamic
  • Marker Slide (6)
  • Dynamic (growable) Array-based Stack
  • So which will be better
  • Marker Slide (7)
  • Amortization (common use)
  • Amortization (common use) (2)
  • Amortization (common use) (3)
  • Amortization (common use) (4)
  • Amortization (CS concept)
  • Amortized Analysis
  • Applying Amortization Analysis (aka Aggregate Analysis)
  • Marker Slide (8)
  • Dynamic Array-based Stack
  • Apply to +2(incremental) vs double
  • Incremental Analysis
  • Incremental Analysis (2)
  • Incremental Analysis (3)
  • Incremental Analysis (4)
  • Incremental Analysis (5)
  • Incremental Analysis (6)
  • Incremental Analysis (7)
  • Incremental Analysis (8)
  • Incremental Analysis (9)
  • Incremental Analysis (10)
  • Incremental Analysis (11)
  • Incremental Analysis (12)
  • Incremental Analysis (13)
  • Incremental Analysis (14)
  • Summary So Far
  • Marker Slide (9)
  • Doubling Analysis
  • Doubling Analysis (2)
  • Doubling Analysis (3)
  • Doubling Analysis (4)
  • Doubling Analysis (5)
  • Doubling Analysis (6)
  • Doubling Analysis (7)
  • Doubling Analysis (8)
  • Doubling Analysis (9)
  • Doubling Analysis (10)
  • Doubling Analysis (11)
  • Doubling Analysis (12)
  • Doubling Analysis (13)
  • Marker Slide (10)
  • Conclusions of Analysis
  • Performance and Limitations (static array-based implementation
  • Performance and Limitations (static array-based implementation (2)
  • Performance and Limitations (static array-based implementation (3)
  • Performance and Limitations (static array-based implementation (4)
  • Conclusion Implementing Stack Using Dynamic Array
  • Marker Slide (11)
  • Singly Linked List
  • Singly Linked List (2)
  • Singly Linked List (3)
  • Singly Linked List Node
  • Singly Linked List (4)
  • Marker Slide (12)
  • So far
  • Stack with a Singly Linked List
  • Stack and Singly Linked List
  • Stack and Singly Linked List (2)
  • Stack and Singly Linked List (3)
  • Stack and Singly Linked List (4)
  • Stack and Singly Linked List (5)
  • Stack and Singly Linked List (6)
  • Stack and Singly Linked List (7)
  • Stack with a Singly Linked List (2)
  • Stack Summary
  • The End