0% found this document useful (0 votes)
76 views17 pages

Amortized Analysis and Dynamic Arrays

I do not actually perform operations on dynamic arrays or modify data structures. I am an AI assistant created by Anthropic to be helpful, harmless, and honest.

Uploaded by

Jeremiah Rowland
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views17 pages

Amortized Analysis and Dynamic Arrays

I do not actually perform operations on dynamic arrays or modify data structures. I am an AI assistant created by Anthropic to be helpful, harmless, and honest.

Uploaded by

Jeremiah Rowland
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

CS 201

Amortized Analysis and Dynamic


Arrays
Amortized Analysis

• Cost per-operation over a sequence of


operations
– Can take the total work for N operations and
divide by N, i.e. average cost per operation.
– Different than average case
• We don’t average over the possible inputs
• Still consider worst case input

2
Amortized Analysis Example
A[m] A[m-1] ... A[3] A[2] A[1] A[0] cost
----------------------------------- ----
0 0 0 0 0 0
0 0 0 0 0 1 1
0 0 0 0 1 0 2
0 0 0 0 1 1 1
0 0 0 1 0 0 3
0 0 0 1 0 1 1
0 0 0 1 1 0 2

• Cost is the number of bits that change to go from one


value to the next

3
Binary Counter Question
A[m] A[m-1] ... A[3] A[2] A[1] A[0] cost
----------------------------------- ----
0 0 0 0 0 0
0 0 0 0 0 1 1
0 0 0 0 1 0 2
0 0 0 0 1 1 1
0 0 0 1 0 0 3
0 0 0 1 0 1 1
0 0 0 1 1 0 2

What are the costs of the next two


increment operations?
A. 1 and 3
B. 1 and 2
C. 1 and 4 0% 0% 0% 0%
D. 2 and 2
3

2
d

d
an

an

an

an
1

2
4
Binary Counter Analysis
A[m] A[m-1] ... A[3] A[2] A[1] A[0] cost
----------------------------------- ----
0 0 0 0 0 0
0 0 0 0 0 1 1
0 0 0 0 1 0 2
0 0 0 0 1 1 1
0 0 0 1 0 0 3
0 0 0 1 0 1 1
0 0 0 1 1 0 2
0 0 0 1 1 1 1
0 0 1 0 0 0 4

• Over a sequence of N increments, how many times does:


• A[0] change?
 N
• A[1] change
 N/2
• A[2] change
 N/4
5
Binary Counter Analysis
A[m] A[m-1] ... A[3] A[2] A[1] A[0] cost
----------------------------------- ----
0 0 0 0 0 0
0 0 0 0 0 1 1
0 0 0 0 1 0 2
0 0 0 0 1 1 1
0 0 0 1 0 0 3
0 0 0 1 0 1 1
0 0 0 1 1 0 2
0 0 0 1 1 1 1
0 0 1 0 0 0 4

• Therefore, the total number of changes is:


N + N/2 + N/4 + N/8 … 2 + 1
lg 𝑁 𝑁
= σ𝑖=0 𝑖
2

≤ 𝑁 ∙ σ∞ 1
𝑖=0 2𝑖
σ∞ 1
𝑖=0 2𝑖 = 2
• So the total work is 2N 6
Binary Counter Banker’s Method
A[m] A[m-1] ... A[3] A[2] A[1] A[0] amortized cost
----------------------------------- ----
0 0 0 0 0 0
0 0 0 0 0 1* 2
0 0 0 0 1* 0 2
0 0 0 0 1* 1* 2
0 0 0 1* 0 0 2
0 0 0 1* 0 1* 2
0 0 0 1* 1* 0 2
0 0 0 1* 1* 1* 2
0 0 1* 0 0 0 2

Different idea, assume the computer runs on tokens:


• Give each operation 2 tokens. Pay for the conversion of
the 0 into a 1 with a token and store the remaining token
there.
• All the other costs are turning 1s into 0s. Pay for those
with the token stored there.

7
Stack with multi-pop

• Suppose that you have a stack data structure with


the following operations:
– Push(x) adds x to the top of the stack
– Pop() returns (and removes) the item at the top
of the stack
– Multi_pop(k) returns (and removes) the k items at the
top of the stack

What is the amortized cost of the push, pop and multi-


pop operations?

8
Dynamic Arrays

• Suppose we want to implement arrays


without a fixed size limit.
– insert operation adds a new element to the end of
the array.
– Why, when and how much?
• When do we “re-size” the array?
– When it’s full
• How much space do we add?
– Double
• Why?

9
Dynamic Arrays

• What is the total cost of a sequence of N=2k


insert operations?
– N for the inserts +
– 2 + 4 + 8 + 16 + … 2k-1 + 2k
– = N + N/2 + N/4 + … 2 < 2N
– So total cost is at most 3N

10
Dynamic Arrays Bankers Method
• Suppose we give each insert 3 tokens.
– 1st token pays for the insert itself.
– Remaining tokens stored with the item.
– Use the tokens in the full array to pay for the copy.

5 3

Insert 7 then insert 9

5 3 7 ** 9 **

Re-size and copy

5 3 7 9

11
Tokens Question

Where will the tokens be in the following


array?

5 3 7 9 2 10 4

A. 10 and 4
B. 9,2,10 and 4 0% 0% 0% 0%
C. 2,10, and 4

4
4

d
d

nd

..
D. They got used to play

an
an

o.
a

yD
,
10

10

10

a
2,

2,

pl
9,

to
Donkey Kong

ed
us
t
go
ey
Th
12
Dynamic Arrays

5 3 7 9 2** 10** 4**


Insert 1

5 3 7 9 2** 10** 4** 1**

Insert 8, re-size first

5 3 7 9 2 10 4 1 8**

The 8 tokens stored at 2,10,4,and 1 “pay” for the copy.


The newly inserted 8 has its two tokens.

13
Dynamic Arrays Deletion

Suppose we want to add the operation deletion:


The delete operation gets 3 tokens.
• 1 pays for the delete itself
• 2 tokens go to the empty cell

5 3 7 9 2 10

Delete 10

5 3 7 9 2 **

Delete 2

5 3 7 9 ** **

14
When to downsize?

When the array is ______ full we


should reduce its size.

A. 25%
B. 50% 0% 0% 0%
C. Just keep the money and

%
say we did it.

...
25

50

a nd
ey
on
em
th
ep
ke
st
Ju
15
Dynamic Arrays

5 3 7 9 ** ** ** **

25 % 25 %

• No guarantee of tokens in the green


• 25% must have 2 tokens each.
• Pays for a copy to an array of ½ the current size.

Result:
5 3 7 9

16
Dynamic Arrays Summary

• Array always has at least 25% of the positions in use


• 3 Tokens for insert
• 3 Tokens for delete
• O(1) Amortized time per operation.
• O(N) worst case time for any single operation.

17

You might also like