Amortized Analysis Flashcards

1
Q

What is amortized analysis?

A

A technique for analyzing the average cost per operation over a sequence of operations, ensuring worst-case performance bounds without using probability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is amortized analysis different from average-case analysis?

A

Amortized analysis provides worst-case guarantees over sequences, while average-case uses probabilities and expectations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When is amortized analysis useful?

A

When a few operations are expensive but are ‘paid for’ by many cheap operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the three main methods of amortized analysis?

A
  1. Aggregate
  2. Accounting
  3. Potential methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the Aggregate Method?

A

Calculate total cost T(n) of n operations, then amortized cost = T(n)/n.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Accounting Method?

A

Overcharge some operations, store the surplus as ‘credit,’ and use it to pay for more expensive future operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What must the credit total always be in the accounting method?

A

Non-negative — credit can’t go below zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the Potential Method?

A

Uses a potential function Φ to represent stored work (like energy) in the whole data structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Formula for amortized cost in the Potential Method?

A

ĉᵢ = cᵢ + Φ(Dᵢ) - Φ(Dᵢ₋₁)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s the worst-case time for MULTIPOP?

A

O(n), if the stack has n items.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Aggregate cost of n stack operations (including MULTIPOP)?

A

O(n), since each item can only be popped once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Amortized cost of stack operations using accounting?

A

PUSH = 2 units, POP/MULTIPOP = 0 units.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Potential function for stack (Potential Method)?

A

Φ = number of items in the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What causes cost in INCREMENT operation?

A

Flipping bits from 1 to 0 and a 0 to 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Worst-case time for a single increment?

A

O(k), where k is the number of bits.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Aggregate total cost of n increments?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Amortized cost per increment (any method)?

A

O(1), often exactly 2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How does the accounting method pay for bit flips?

A

Pay 2 when setting a bit to 1 (1 now, 1 saved for reset).

19
Q

Potential function for counter?

A

Φ = number of 1s in the counter

20
Q

What happens when a dynamic array is full during insertion?

A

Allocate a new array (usually double the size), copy all items, then insert the new one.

21
Q

Worst-case insert time for dynamic array resizing?

A

O(n) when resize occurs.

22
Q

Total cost of n inserts with resizing?

23
Q

Amortized cost per insert for dynamic arrays?

A

O(1), often shown as 3 using accounting.

24
Q

Accounting method cost breakdown for insert into dynamic arrays?

A

1 for insert, 1 for future move, 1 for helping move others = 3 units.

25
Why doesn't resizing make total cost quadratic?
Because resizes happen infrequently at 1, 2, 4, 8... forming a geometric series.
26
Front
Back
27
What is a stack?
A linear data structure that follows the Last In, First Out (LIFO) principle.
28
What are the two primary operations of a stack?
`push` (adds an element) and `pop` (removes the top element).
29
What does the `push` operation do?
Adds an element to the top of the stack.
30
What does the `pop` operation do?
Removes and returns the top element of the stack.
31
What is the time complexity of `push` in a standard stack?
O(1) – constant time.
32
What is the time complexity of `pop` in a standard stack?
O(1) – constant time.
33
What is `MULTIPOP(S, k)`?
A special operation that pops up to `k` elements from the stack `S`.
34
What is the worst-case time complexity of `MULTIPOP(S, k)`?
O(k) – up to `k` elements could be removed.
35
What is the aggregate cost of `n` `PUSH`, `POP`, and `MULTIPOP` operations?
O(n) total – each element is pushed and popped at most once.
36
What is the amortized cost of a stack operation (push/pop/multipop)?
O(1) – using aggregate or accounting method.
37
Why is the amortized cost of `MULTIPOP` still O(1)?
Because each element is popped at most once over all operations.
38
What is the LIFO principle?
Last-In, First-Out – the last element added is the first to be removed.
39
What happens if you call `pop` on an empty stack?
It causes an underflow error.
40
What happens if you call `push` on a full stack (in a fixed-size array)?
It causes an overflow error.
41
What data structure is typically used to implement a stack?
Array or singly linked list.
42
What is the potential function used in amortized analysis for stacks?
Typically: Φ(S) = number of elements in the stack.
43
In potential method, what is the amortized cost of a `push`?
ĉ_push = 1 + (Φ_new - Φ_old) = 2
44
In potential method, what is the amortized cost of a `pop`?
ĉ_pop = 1 + (Φ_new - Φ_old) = 0