View
224
Download
1
Category
Preview:
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
Citation preview
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
(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
(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
(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
(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
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
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
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
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
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
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
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
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 (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
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
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
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
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
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
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
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
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
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
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 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
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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = n2 times) we will increase capacity by c = 2And we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 6 items the third 8 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The 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
Incremental Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = nc times
bull The 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
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
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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items 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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items 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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip
We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip
We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip
We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip
We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip
We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip
We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip
We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip
We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip
We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) by n
bull The amortized time is O(n)
We start with an array of capacity 2 and size 0 (empty)
Assume a call to push() takes time 1 unit we will push n things one at a time so need n time
Each time we go past our capacity (k = log2n times) we will double capacityAnd we will have to copy the stuff already in the array into the new arraySo 2 items the first time 4 items the second 8 items the third 16 items the fourth hellip
Assuming each item we copy requires time 1 unitSo 2 units of time for 2 items 4 units of times for 4 items 6 units for 6 items hellip
We then have the need for2 + 4 + 8 + 16 + hellip + 2k units of time total time = n + 2 + 4 + 8 + 16 + hellip + 2k
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) 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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) 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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) 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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this strategy replaces the array k = log2 n times
bull The total time T(n) of a series of n push operations is proportional tobull n + c + 2c + 3c + 4c + hellip + kc
bull Since c is a constant T(n) is O(n + k2) = O(n2)bull Divide by T(n) 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
Doubling Analysisbull Say our array grows to a final size of n
bull Then this 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
Marker Slidebull Questions on
bull Stacksbull 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
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
Performance and Limitations (static array-based implementation of stack ADT)
bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)
runs in time O(1)
bull Limitationsndash The maximum size of the stack must 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
Performance and Limitations (static array-based implementation of stack ADT)
bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)
runs in time O(1)
bull Limitationsndash The maximum size of the stack must 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
Performance and Limitations (static array-based implementation of stack ADT)
bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)
runs in time O(1)
bull Limitationsndash The maximum size of the stack must 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
Performance and Limitations (static array-based implementation of stack ADT)
bull Performancendash Let n be the number of elements in the stackndash The space used is O(n)ndash Each operation (push pop top size empty)
runs in time O(1)
bull Limitationsndash The maximum size of the stack must 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
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
Marker Slidebull Questions on
bull Stacksbull 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
Singly Linked Listbull A singly linked list is a structure
consisting of a sequence of nodes
bull A 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
Singly Linked Listbull A singly linked list is a structure
consisting of a sequence of nodes
bull A 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
Singly Linked Listbull A singly linked list is a structure
consisting of a sequence of nodes
bull A 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
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
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
Marker Slidebull Questions on
bull Stacksbull 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
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
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
Stack and Singly Linked Listbull Singly linked list Operations
ndash insertFront(e) inserts an element on the front of the list
ndash removeFront() returns and removes the element at the front of the list
ndash insertBack(e) inserts an element on the back of the list
ndash removeBack() returns and removes the element at the end of the list
bull Stack Operationsbull push(e) inserts an element to the
top of the stackbull pop() removes and returns the top
element of the stack
bull top() returns a reference to the top element of the stack but doesnrsquot 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
Stack and Singly Linked Listbull Singly linked list Operations
ndash insertFront(e) inserts an element on the front of the list
ndash removeFront() returns and removes the element at the front of the list
ndash insertBack(e) inserts an element on the back of the list
ndash removeBack() returns and removes the element at the end of the list
bull Stack Operationsbull push(e) inserts an element to the
top of the stackbull pop() removes and returns the top
element of the stack
bull top() returns a reference to the top element of the stack but doesnrsquot 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
Stack and Singly Linked Listbull Singly linked list Operations
ndash insertFront(e) inserts an element on the front of the list
ndash removeFront() returns and removes the element at the front of the list
ndash insertBack(e) inserts an element on the back of the list
ndash removeBack() returns and removes the element at the end of the list
bull Stack Operationsbull push(e) inserts an element to the
top of the stackbull pop() removes and returns the top
element of the stack
bull top() returns a reference to the top element of the stack but doesnrsquot remove it
bull size() returns the number of elements in the stack
bull empty() returns a bool indicating if the stack contains any objects
Stack and Singly Linked Listbull Singly linked list Operations
ndash insertFront(e) inserts an element on the front of the list
ndash removeFront() returns and removes the element at the front of the list
ndash insertBack(e) inserts an element on the back of the list
ndash removeBack() returns and removes the element at the end of the list
bull Stack Operationsbull push(e) inserts an element to the
top of the stackbull pop() removes and returns the top
element of the stack
bull top() returns a reference to the top element of the stack but doesnrsquot remove it
bull size() returns the number of elements in the stack
bull empty() returns a bool indicating if the stack contains any objects
Stack and Singly Linked Listbull Singly linked list Operations
ndash insertFront(e) inserts an element on the front of the list
ndash removeFront() returns and removes the element at the front of the list
ndash insertBack(e) inserts an element on the back of the list
ndash removeBack() returns and removes the element at the end of the list
bull Stack Operationsbull push(e) inserts an element to the
top of the stackbull pop() removes and returns the top
element of the stack
bull top() returns a reference to the top element of the stack but doesnrsquot remove it
bull size() returns the number of elements in the stack
bull empty() returns a bool indicating if the stack contains any objects
Stack and Singly Linked Listbull Singly linked list Operations
ndash 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
Stack and Singly Linked Listbull Singly linked list Operations
ndash insertFront(e) inserts an element on the front of the list
ndash removeFront() returns and removes the element at the front of the list
ndash insertBack(e) inserts an element on the back of the list
ndash removeBack() returns and removes the element at the end of the list
bull Stack Operationsbull push(e) inserts an element to the
top of the stackbull pop() removes and returns the top
element of the stack
bull top() returns a reference to the top element of the stack but doesnrsquot remove it
bull size() returns the number of elements in the stack
bull empty() returns a bool indicating if the stack contains any objects
Stack and Singly Linked Listbull Singly linked list Operations
ndash insertFront(e) inserts an element on the front of the list
ndash removeFront() returns and removes the element at the front of the list
ndash insertBack(e) inserts an element on the back of the list
ndash removeBack() returns and removes the element at the end of the list
bull Stack Operationsbull push(e) inserts an element to the
top of the stackbull pop() removes and returns the top
element of the stack
bull top() returns a reference to the top element of the stack but doesnrsquot remove it
bull size() returns the number of elements in the stack
bull empty() returns a bool indicating if the stack contains any objects
Stack and Singly Linked Listbull Singly linked list Operations
ndash 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
Stack and Singly Linked Listbull Singly linked list Operations
ndash insertFront(e) inserts an element on the front of the list
ndash removeFront() returns and removes the element at the front of the list
ndash insertBack(e) inserts an element on the back of the list
ndash removeBack() returns and removes the element at the end of the list
bull Stack Operationsbull push(e) inserts an element to the
top of the stackbull pop() removes and returns the top
element of the stack
bull top() returns a reference to the top element of the stack but doesnrsquot remove it
bull size() returns the number of elements in the stack
bull empty() returns a bool indicating if the stack contains any objects
Stack and Singly Linked Listbull Singly linked list Operations
ndash 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
Stack and Singly Linked Listbull Singly linked list Operations
ndash 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
Stack and Singly Linked Listbull Singly linked list Operations
ndash 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
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
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
The End
bull For next timebull Read Chapter 5
bull Stacks and Queues
Recommended