Download presentation
Presentation is loading. Please wait.
Published byPaul Hall Modified over 8 years ago
2
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Content derived from: Data Structures Using C++ (D.S. Malik) and Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount)
3
Points of Note Assignment 6 is posted Due: November 4
4
Previously Stacks in general from the book Dynamic Array (review)
5
Next Up for Today Stacks – Chapter 7 What are they? How are the used? How can we implement them Static Array Dynamic Array Amortized Time Linked Lists
6
Marker Slide Questions? Next up Description Application STATIC Array Based DYNAMIC Array Based Amortization Description Applied to Incremental Increase for Dynamic Array Resizing Applied to Doubling Increase for Dynamic Array Resizing Stack Implementation Analysis Static Array versus Dynamic Array Linked List Refresher Stack Implemented as a Linked List
7
Recall: Stack Intro We are now to chapter 7 of the book Unit 2 on D2L You are now all assumed to be proficient at C++ and familiar with the processes needed to analyze algorithms using Big-Oh notation We may still review from time to time We will be discussing Stacks today As an ADT – what are they? Using Static Arrays to implement the Stack ADT Using Dynamic Arrays to implement the Stack ADT Using Linked Lists to implement the Stack ADT
8
Stacks
9
Stacks store arbitrary objects (Pez in this case)
10
Stacks Stacks store arbitrary objects (Pez in this case) Operations – push(e): inserts an element to the top of the stack
11
Stacks Stacks store arbitrary objects (Pez in this case) Operations – push(e): inserts an element to the top of the stack
12
Stacks Stacks store arbitrary objects (Pez in this case) Operations – push(e): inserts an element to the top of the stack – pop(): removes and returns the top element of the stack
13
Stacks Stacks store arbitrary objects (Pez in this case) Operations – push(e): inserts an element to the top of the stack – pop(): removes and returns the top element of the stack
14
Stacks Stacks store arbitrary objects (Pez in this case) Operations – push(e): inserts an element to the top of the stack – pop(): removes and returns the top element of the stack
15
Stacks Stacks store arbitrary objects (Pez in this case) Operations – push(e): inserts an element to the top of the stack – pop(): removes and returns the top element of the stack – top(): returns a reference to the top element of the stack, but doesn’t remove it
16
Stacks Stacks store arbitrary objects (Pez in this case) Operations – push(e): inserts an element to the top of the stack – pop(): removes and returns the top element of the stack – top(): returns a reference to the top element of the stack, but doesn’t remove it Optional operations – size(): returns the number of elements in the stack – empty(): returns a bool indicating if the stack contains any objects
17
Stack Exceptions Attempting to execute an operation of ADT may cause an error condition called an exception Exceptions are said to be “thrown” by an operation that cannot be executed In the Stack ADT, pop and top cannot be performed if the stack is empty Attempting to execute pop or top on an empty stack throws an EmptyStackException
18
Class Exercise: Stacks Describe the output and final structure of the stack after the following operations: Push(8) Push(3) Pop() Push(2) Push(5) Pop() Push(9) Push(1)
19
Marker Slide Questions on: Stacks Description Next up Application STATIC Array Based DYNAMIC Array Based Amortization Description Applied to Incremental Increase for Dynamic Array Resizing Applied to Doubling Increase for Dynamic Array Resizing Stack Implementation Analysis Static Array versus Dynamic Array Linked List Refresher Stack Implemented as a Linked List
20
So far Stacks A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack) Common place examples: pancakes, plates, trays
21
So far Stacks A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack) Common place examples: pancakes, plates, trays Items in a stack must be removed in the reverse order that they were added to the stack This is referred to as a ------------ data structure PAUSE FOR CLASS PARTICPATION
22
So far Stacks A stack is an ordered collection of entries that can be accessed only at one end (the top of the stack) Common place examples: pancakes, plates, trays Items in a stack must be removed in the reverse order that they were added to the stack This is referred to as a Last-In/First Out (LIFO) structure
23
Other Applications of Stacks Direct Applications Visited Page history of a web-browser Undo sequence in a text editor Saving local variables when one function calls another, and that one calls yet another, and… Indirect Applications Auxiliary data structure for algorithms Component of other data structures Let’s take a closer look at this one Good for Computer Engineers
24
C++ Run-time Stack The C++ run-time system keeps track of the chain of active functions with a stack When a function is called, the run-time system pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed 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 = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … }
25
C++ Run-time Stack The C++ run-time system keeps track of the chain of active functions with a stack When a function is called, the run-time system pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed 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 = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } main PC = 2 i = 5
26
C++ Run-time Stack The C++ run-time system keeps track of the chain of active functions with a stack When a function is called, the run-time system pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed 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 = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } main PC = 2 i = 5 foo PC = 3 j = 5 k = 6
27
C++ Run-time Stack The C++ run-time system keeps track of the chain of active functions with a stack When a function is called, the run-time system pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed 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 = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } main PC = 2 i = 5 foo PC = 3 j = 5 k = 6
28
C++ Run-time Stack The C++ run-time system keeps track of the chain of active functions with a stack When a function is called, the run-time system pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed 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 = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } main PC = 2 i = 5
29
Marker Slide Questions on: Stacks Description Application Next up STATIC Array Based DYNAMIC Array Based Amortization Description Applied to Incremental Increase for Dynamic Array Resizing Applied to Doubling Increase for Dynamic Array Resizing Stack Implementation Analysis Static Array versus Dynamic Array Linked List Refresher Stack Implemented as a Linked List
30
(static) Array-based Stack A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element S 012 t … Algorithm size() return t + 1 Algorithm empty() return size () == 0 Algorithm pop() if empty() then throw EmptyStackException else t t 1 return S[t + 1]
31
(static) Array-based Stack A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element S 012 t … Algorithm size() return t + 1 Algorithm empty() return size () == 0 Algorithm pop() if empty() then throw EmptyStackException else t t 1 return S[t + 1]
32
(static) Array-based Stack A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element S 012 t … Algorithm size() return t + 1 Algorithm empty() return size () == 0 Algorithm pop() if empty() then throw EmptyStackException else t t 1 return S[t + 1]
33
(static) Array-based Stack The array storing the stack elements may become full A push operation will then throw a FullStackException Limitation of the array-based implementation Not intrinsic to the Stack ADT S 012 t … Algorithm push(e) if t = S.length 1 then throw FullStackException else t t + 1 S[t] e
34
Performance and Limitations (array-based implementation of stack ADT) Performance – Let n be the number of elements in the stack – The space used is O(n) – Each operation (push, pop, top, size, empty) runs in time O(1) Limitations – The maximum size of the stack must be defined a priori, and cannot be changed – Trying to push a new element onto a full stack causes an implementation-specific exception
35
General Stack Interface in C++ Requires the definition of a class EmptyStackException Most similar in STL to std::vector template class Stack { public: int size(); bool isEmpty(); Type& top() throw(EmptyStackException); void push(Type e); Type pop() throw(EmptyStackException); };
36
template class ArrayStack { private: 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 < 0; } Type pop() throw(EmptyStackException) { if ( isEmpty() ) throw EmptyStackException("Popping from empty stack"); return S[ top-- ]; } //… (other functions omitted) Array-based Stack in C++
37
Stacks – Fun Application Word Reversal LOVE becomes EVOL Useful for finding palindromes "Radar" becomes "radaR" "Step on no Pets" becomes "steP on no petS" Sidetrack
38
Math Check – Application Stacks are often used for evaluating math formulas For example checking for matching parentheses ( ( x + y * ( z + 7 ) ) * (a + b) ) Processing the line left to right Each open paren, (, equates to a push Each closed paren, ), is a pop If matched the stack is empty at the end Sidetrack
39
Performance and Limitations (Static Array Implementation of Stack ADT) Performance – Let n be the number of elements in the stack – The space used is O(n) – Each operation (push, pop, top, size, empty) runs in time O(1) Limitations – The maximum size of the stack must be defined a priori, and cannot be changed – Trying to push a new element onto a full stack causes an implementation-specific exception Back on Track
40
End Static – Begin Dynamic Static arrays can be used to implement stacks But have limitations (previous slide) Perhaps Dynamic Arrays will be better
41
Marker Slide Questions on: Stacks Description Application STATIC Array Based Next up DYNAMIC Array Based Amortization Description Applied to Incremental Increase for Dynamic Array Resizing Applied to Doubling Increase for Dynamic Array Resizing Stack Implementation Analysis Static Array versus Dynamic Array Linked List Refresher Stack Implemented as a Linked List
42
Dynamic (growable) Array-based Stack In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be? incremental strategy: increase the size by a constant c doubling strategy: double the size Algorithm push(e) if t = S.length 1 then A new array of size … for i 0 to t do A[i] S[i] S A t t + 1 S[t] o Did we see these options before? With c = 2
43
So which will be better? Incremental Strategy Increasing the array size by a constant c Doubling Strategy Doubling the array size The answer is found using amortized time Let c = 2 if that is easier to think about See also the Pitcher example
44
Marker Slide Questions on: Stacks Description Application STATIC Array Based DYNAMIC Array Based Next up Amortization Description Applied to Incremental Increase for Dynamic Array Resizing Applied to Doubling Increase for Dynamic Array Resizing Stack Implementation Analysis Static Array versus Dynamic Array Linked List Refresher Stack Implemented as a Linked List
45
Amortization (common use) Amortization (definition) Any guesses at this?
46
Amortization (common use) Amortization (definition) The process of decreasing an amount over time This shows up in several places in “real life” Such as Home Loans Business Payments
47
Amortization (common use) Amortization (definition) The process of decreasing an amount over time Home Loans Amortization is the process by which loan principle decreases over the life of a loan A portion of the payment is applied towards principle and a portion is applied toward interest The “cost” is stretched out over time Each payment is paying a small amount of what would be a large payment if paid all at once
48
Amortization (common use) Amortization (definition) The process of decreasing an amount over time Home Loans Amortization is the process by which loan principle decreases over the life of a loan A portion of the payment is applied towards principle and a portion is applied toward interest The “cost” is stretched out over time Each payment is paying a small amount of what would be a large payment if paid all at once Business Amortization allocates a lump sum (payment) amount to different time periods
49
Amortization (CS concept) Back in Computer Science world Certain operations may be extremely costly BUT They cannot occur frequently enough to slow down the entire program The less costly operations far outnumber the costly one Thus over the long-term they are “paying back” the program over a number of iterations
50
Amortized Analysis Requires knowledge about the entire series of operations Usually where a state persists between operations Like the capacity of memory allocated The idea is the worst case operation can alter the state in such a way that the worst case cannot occur again for a “long” time thus amortizing its cost
51
Applying Amortization Analysis (aka Aggregate Analysis) Aggregate analysis determines the upper bound T(n) of the total cost of a sequence of n operations T(n) is what we have been calculating previously for our Big-Oh stuff Then the amortized cost is T(n) / n because we make “small payments” for the worst operation across each operation
52
Marker Slide Questions on: Stacks Description Application STATIC Array Based DYNAMIC Array Based Amortization Description Next up Amortization Applied to Incremental Increase for Dynamic Array Resizing Applied to Doubling Increase for Dynamic Array Resizing Stack Implementation Analysis Static Array versus Dynamic Array Linked List Refresher Stack Implemented as a Linked List
53
Dynamic Array-based Stack In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be? incremental strategy: increase the size by a constant c (say c = 2) doubling strategy: double the size Algorithm push(e) if t = S.length 1 then A new array of size … for i 0 to t do A[i] S[i] S A t t + 1 S[t] o Recall: Recall we used c = 2 for the Pitcher class
54
Apply to +2(incremental) vs. double 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. Assume we start with an empty stack represented by an array of size 1 We call the amortized time of a push operation: the average time taken by a push over the series of operations i.e. T(n) / n
55
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = n/2 times) we will increase capacity by c = 2 And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 6 items the third, 8 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 6 + 8 + … + 2*k units of time total time = n + 2 + 2*2 + 2*3 + 2*4 +…+2*k How do we know it will replace the array k = n/c times? Think: how many “groups of size c” are in a set of n things? n/c
56
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = n/2 times) we will increase capacity by c = 2 And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 6 items the third, 8 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 6 + 8 + … + 2*k units of time total time = n + 2 + 2*2 + 2*3 + 2*4 +…+2*k How do we know it will replace the array k = n/c times? Think: how many “groups of size c” are in a set of n things? n/c
57
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = n/2 times) we will increase capacity by c = 2 And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 6 items the third, 8 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 6 + 8 + … + 2*k units of time total time = n + 2 + 2*2 + 2*3 + 2*4 +…+2*k How do we know it will replace the array k = n/c times? Think: how many “groups of size c” are in a set of n things? n/c
58
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = n/2 times) we will increase capacity by c = 2 And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 6 items the third, 8 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 6 + 8 + … + 2*k units of time total time = n + 2 + 2*2 + 2*3 + 2*4 +…+2*k
59
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = n/2 times) we will increase capacity by c = 2 And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 6 items the third, 8 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 6 + 8 + … + 2*k units of time total time = n + 2 + 2*2 + 2*3 + 2*4 +…+2*k
60
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = n/2 times) we will increase capacity by c = 2 And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 6 items the third, 8 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need 2 + 4 + 6 + 8 + … + 2*k units of time total time = n + 2 + 2*2 + 2*3 + 2*4 +…+2*k
61
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = n/2 times) we will increase capacity by c = 2 And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 6 items the third, 8 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need 2 + 4 + 6 + 8 + … + 2*k units of time total time = n + 2*1 + 2*2 + 2*3 + 2*4 +…+2*k
62
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = n/2 times) we will increase capacity by c = 2 And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 6 items the third, 8 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need 2 + 4 + 6 + 8 + … + 2*k units of time total time = n + 2*1 + 2*2 + 2*3 + 2*4 +…+2*k
63
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) total time = n + 2 + 2*2 + 2*3 + 2*4 +…+2*k but we were using c = 2 for that… now put the c back in total time = n + c + c*2 + c*3 + c*4 +…+ c*k Next we simplify
64
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc.
65
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc. n stays n, c*(k 2 + k)/2 = (c/2)*k 2 + k/2 => k 2
66
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc. Substitute in n/c for k and simplify
67
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc. = O( n 2 )
68
Incremental Analysis Say our array grows to a final size of n Then this strategy replaces the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc. So … T(n) is O(n + k 2 ) = O(n + n 2 ) And the Amortized Time is T(n)/n = O( n 2 ) = O( n )
69
Summary So Far Amortized Analysis tells us Incremental Increase Method is O(n) Next we do similar for the Doubling Method
70
Marker Slide Questions on: Stacks STATIC Array Based DYNAMIC Array Based Amortization Description Applied to Incremental Increase for Dynamic Array Resizing Next up Amortization Applied to Doubling Increase for Dynamic Array Resizing Stack Implementation Analysis Static Array versus Dynamic Array Linked List Refresher Stack Implemented as a Linked List
71
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = log 2 n times) we will double capacity And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 8 items the third, 16 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 8 + 16 + … + 2 k units of time total time = n + 2 + 4 + 8 + 16 + … + 2 k Think on: How do we know it replaces the array k = lg n times ?
72
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = log 2 n times) we will double capacity And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 8 items the third, 16 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 8 + 16 + … + 2 k units of time total time = n + 2 + 4 + 8 + 16 + … + 2 k 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 2…
73
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = log 2 n times) we will double capacity And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 8 items the third, 16 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 8 + 16 + … + 2 k units of time total time = n + 2 + 4 + 8 + 16 + … + 2 k
74
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = log 2 n times) we will double capacity And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 8 items the third, 16 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 8 + 16 + … + 2 k units of time total time = n + 2 + 4 + 8 + 16 + … + 2 k
75
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = log 2 n times) we will double capacity And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 8 items the third, 16 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 8 + 16 + … + 2 k units of time total time = n + 2 + 4 + 8 + 16 + … + 2 k
76
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) We start with an array of capacity 2 and size 0 (empty) Assume a call to push() takes time 1 unit we will push n things one at a time so need n time Each time we go past our capacity (k = log 2 n times) we will double capacity And we will have to copy the stuff already in the array into the new array So 2 items the first time, 4 items the second, 8 items the third, 16 items the fourth … Assuming each item we copy requires time 1 unit So 2 units of time for 2 items, 4 units of times for 4 items, 6 units for 6 items, … We then have the need for 2 + 4 + 8 + 16 + … + 2 k units of time total time = n + 2 + 4 + 8 + 16 + … + 2 k
77
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) total time = n + 2 + 4 + 8 + 16 + … + 2 k
78
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) Put into Summation Notation
79
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) Simplify the Summation
80
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) Take a 2 out
81
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) Substitute lg n in for k
82
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) Simplify
83
Doubling Analysis Say our array grows to a final size of n Then this strategy replaces the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to. Since c is a constant T(n) is O(n + k 2 ) = O(n 2 ) Divide by T(n) by n The amortized time is O(n) total time = n + 2 + 4 + 8 + 16 + … + 2 k = 3n – 1 So T(n) is O(n) and the amortized time T(n) / n = O(n) / n = O( 1 )
84
Marker Slide Questions on: Stacks STATIC Array Based DYNAMIC Array Based Amortization Description Applied to Incremental Increase for Dynamic Array Resizing Applied to Doubling Increase for Dynamic Array Resizing Next up Stack Implementation Analysis Static Array versus Dynamic Array Linked List Refresher Stack Implemented as a Linked List
85
Conclusions of Analysis So what did we learn? If we use a dynamic array the amortized time for a push operation is O(1) Why do we care? … Recall next slide
86
Performance and Limitations (static array-based implementation of stack ADT) Performance – Let n be the number of elements in the stack – The space used is O(n) – Each operation (push, pop, top, size, empty) runs in time O(1) Limitations – The maximum size of the stack must be defined a priori, and cannot be changed – Trying to push a new element onto a full stack causes an implementation-specific exception Recall:
87
Performance and Limitations (static array-based implementation of stack ADT) Performance – Let n be the number of elements in the stack – The space used is O(n) – Each operation (push, pop, top, size, empty) runs in time O(1) Limitations – The maximum size of the stack must be defined a priori, and cannot be changed – Trying to push a new element onto a full stack causes an implementation-specific exception Recall: Dynamic Arrays clearly fix this … BUT…
88
Performance and Limitations (static array-based implementation of stack ADT) Performance – Let n be the number of elements in the stack – The space used is O(n) – Each operation (push, pop, top, size, empty) runs in time O(1) Limitations – The maximum size of the stack must be defined a priori, and cannot be changed – Trying to push a new element onto a full stack causes an implementation-specific exception Recall: Seemed to fail on this point
89
Performance and Limitations (static array-based implementation of stack ADT) Performance – Let n be the number of elements in the stack – The space used is O(n) – Each operation (push, pop, top, size, empty) runs in time O(1) Limitations – The maximum size of the stack must be defined a priori, and cannot be changed – Trying to push a new element onto a full stack causes an implementation-specific exception Recall: But dynamic arrays are good here too … per the amortized analysis of doubling the capacity
90
Conclusion: Implementing Stack Using Dynamic Array Using a Dynamic array to implement a stack meets the ADT specification requirements for a Stack Doing so does NOT limit the stack size like a static array Amortization Analysis is required to see how it is also an efficient way to implement a Stack Intuitively it is not necessarily obvious
91
Marker Slide Questions on: Stacks STATIC Array Based DYNAMIC Array Based Amortization Description Applied to Incremental Increase for Dynamic Array Resizing Applied to Doubling Increase for Dynamic Array Resizing Static Array versus Dynamic Array Next up Linked List Refresher head towards Stacks again Stack Implemented as a Linked List
92
Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node LeonardSheldonHowardRaj head tail Review
93
Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node LeonardSheldonHowardRaj head tail Review
94
Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node next elem node LeonardSheldonHowardRaj head tail Review
95
Singly Linked List Node next elem node template class SLinkedListNode { public: Type elem; SLinkedListNode *next; }; LeonardSheldonHowardRaj Review A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node
96
Singly Linked List A singly linked list is a structure consisting of a sequence of nodes Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list Review Details of each of these operations was given in previously
97
Marker Slide Questions on: Stacks STATIC Array Based DYNAMIC Array Based Amortization Description Applied to Incremental Increase for Dynamic Array Resizing Applied to Doubling Increase for Dynamic Array Resizing Static Array versus Dynamic Array Linked List Refresher head towards Stacks again Next up Stack Implemented as a Linked List
98
So far Stacks implemented using Static Arrays Dynamic Arrays (also in the MiniStack homework) Next Linked Lists
99
Stack with a Singly Linked List CLAIM: We can implement a stack with a singly linked list The top element of the stack is the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time Demonstration of how follows t nodes elements top
100
Stack and Singly Linked List Singly linked list Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list Stack Operations push(e): inserts an element to the top of the stack pop(): removes and returns the top element of the stack top(): returns a reference to the top element of the stack, but doesn’t remove it size(): returns the number of elements in the stack empty(): returns a bool indicating if the stack contains any objects Recall
101
Stack and Singly Linked List Singly linked list Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list Stack Operations push(e): inserts an element to the top of the stack pop(): removes and returns the top element of the stack top(): returns a reference to the top element of the stack, but doesn’t remove it size(): returns the number of elements in the stack empty(): returns a bool indicating if the stack contains any objects Top is the First Node
102
Stack and Singly Linked List Singly linked list Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list Stack Operations push(e): inserts an element to the top of the stack pop(): removes and returns the top element of the stack top(): returns a reference to the top element of the stack, but doesn’t remove it size(): returns the number of elements in the stack empty(): returns a bool indicating if the stack contains any objects
103
Stack and Singly Linked List Singly linked list Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list Stack Operations push(e): inserts an element to the top of the stack pop(): removes and returns the top element of the stack top(): returns a reference to the top element of the stack, but doesn’t remove it size(): returns the number of elements in the stack empty(): returns a bool indicating if the stack contains any objects
104
Stack and Singly Linked List Singly linked list Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list Stack Operations push(e): inserts an element to the top of the stack pop(): removes and returns the top element of the stack top(): returns a reference to the top element of the stack, but doesn’t remove it size(): returns the number of elements in the stack empty(): returns a bool indicating if the stack contains any objects
105
Stack and Singly Linked List Singly linked list Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list Stack Operations push(e): inserts an element to the top of the stack pop(): removes and returns the top element of the stack top(): returns a reference to the top element of the stack, but doesn’t remove it size(): returns the number of elements in the stack empty(): returns a bool indicating if the stack contains any objects top() would require a minor alteration or addition to LinkedList very similar to removeFront()
106
Stack and Singly Linked List Singly linked list Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list Stack Operations push(e): inserts an element to the top of the stack pop(): removes and returns the top element of the stack top(): returns a reference to the top element of the stack, but doesn’t remove it size(): returns the number of elements in the stack empty(): returns a bool indicating if the stack contains any objects size() and isEmpty() would require the addition of a counter that increments each time push() is called and decrements when pop() is called
107
Stack with a Singly Linked List CONCLUSION: We can implement a stack with a singly linked list The top element of the stack is the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time push, pop, top, size, empty each are O(1) time t nodes elements top
108
Stack Summary Stack Operation Complexity for Different Implementations Array Fixed-Size Array Dynamic (doubling strategy) Singly Linked List Pop() O(1) Push(o)O(1)O(n) Worst Case O(1) Best Case O(1) Average Case O(1) Top()O(1) Size(), isEmpty()O(1)
109
The End For next time Read Chapters 7 and 8 (if not already done) Stacks and Queues
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.