• 沒有找到結果。

# Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

N/A
N/A
Protected

Share "Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao"

Copied!
33
0
0

(1)

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

(2)

2

(3)

### Amortized Analysis

3

(4)

Textbook Chapter 17 – Amortized Analysis

## 4

(5)

### The same operation may have different costs

5

stack

PUSH POP

MULTIPOP

cost

operations

(6)

6

stack

PUSH POP

MULTIPOP

cost

operations worst-case

Cost of stack operations PUSH(S, x) = O(1)

POP(S) = O(1)

MULTIPOP(S, k) = O(min(|S|, k))

(7)

ULTIPOP

2

### ) time

7

Stack Operations

Suppose that we apply a sequence of n operations on a data structure. What is the time complexity of the procedure?

Can this be an over-estimate?

What if only a few operations take O(n) time and the rest of them take O(1) time?

The worst-case bound is not tight because this

expensive Multipop operation cannot occur so frequently!

(8)

### Goal: obtain an accurate worst-case bound in executing a sequence of operations on a given data structure

An upper bound for any sequence of n operations

### Comparison: types of running-time analysis

8

Type Description

Worst case Running time guarantee for any input of size n

Average case Expected running time for a random input of size n Probabilistic Expected running time of a randomized algorithm

Amortized Worst-case running time for a sequence of n operations

(9)

Aggregate method (聚集法)

• Determine an upper bound 𝑇(𝑛) on the cost over any sequence of 𝑛 operations

• The average cost per operation is then 𝑇(𝑛)/𝑛

• All operations have the same amortized cost

Accounting method (記帳法)

• Each operation is assigned an amortized cost (may differ from the actual cost)

• Each object of the data structure is associated with a credit

• Need to ensure that every object has sufficient credit at any time

Potential method (位能法)

• Similar to accounting method; each operation is assigned an amortized cost

• The data structure as a whole maintains a credit (i.e., potential)

• Need to ensure that the potential level is nonnegative at any time 9

(10)

Textbook Chapter 17.1 – Aggregate analysis

Textbook Chapter 17.2 – The accounting method Textbook Chapter 17.3 – The potential method

## 10

(11)

### Implementation with an array or a linked list

11

Operation Type Cost

PUSH(S, x): inset an element x into S POP(S): pop the top element from S

MULTIPOP(S, k): pop top k elements from S at once

stack

PUSH POP

MULTIPOP MULTIPOP(S, k)

while not STACK-EMPTY(S) and k > 0 POP(S)

k = k - 1

Stack Operations

Suppose that we apply a sequence of n operations on a data structure. What is the time complexity of the procedure?

(12)

1.

2.

3.

### All operations have the same amortized cost

12

cost

operations 𝑇(𝑛) Amortized cost of each op =

opn

op1 op2 … …

(13)

pop

multipop

### Amortized cost per operation:

13

Operation Type #Operations

PUSH(S, x): inset an element x into S npush POP(S): pop the top element from S npop MULTIPOP(S, k): pop top k elements from S at once nmultipop

n

Key idea: #pop elements ≤ #push operations/elements

(14)

### Once the push operation is taken, we prepare the additional cost for the future usage of multipop

14

Key idea: #pop elements ≤ #push operations/elements

(15)

1.

### Each operation is assigned a valid amortized cost

If amortized cost > actual cost, the difference becomes credit (存)

Credit is deposited in an object of the data structure

If amortized cost < actual cost, then withdraw (提) stored credits

2.

3.

15

(16)

### Validity check: ensure that every object has sufficient credit for any times of n operations (不能有赤字)

ci: the actual cost of the i-th operation

ĉi: the amortized cost of the i-th operation

→ For all sequences of n operations, we require

16

### Aggregate Method

Each type of operations have its actual cost

Compute amortized cost using T(n)

### Accounting Method

Each type of operations can have a different amortized cost

Assign valid amortized costs first and then compute T(n)

(17)

1.

2.

### Show that for each object s.t.

PUSH: the pushed element is deposited \$1 credit

POP and MULTIPOP: use the credit stored with the popped element

There is always enough credit to pay for each operation

3.

### Each amortized cost is O(1) → total amortized cost is O(n)

17

Operation Type Actual Cost Amortized Cost

PUSH(S, x) 1 2

POP(S) 1 0

MULTIPOP(S, k) min(|S|, k) 0

(18)

1.

2.

3.

4.

18

### Accounting Method

Each object within the data structure has its credit

### Potential Method

The data structure has credits

(19)

### Potential function Φ maps any state of the data structure to a real number

D0: the initial state of data structure

Di: the state of data structure after i-th operation

ci: the actual cost of i-th operation

ĉi: the amortized cost of i-th operation, defined as

19

(20)

### To obtain an upper bound on the actual cost

Define a potential function such that

Usually we set

20

(21)

1.

𝑖

2.

### Validity check:

The stack is initially empty →

The number of elements in the stack is always ≥ 0 → 3.

PUSH(S, X):

POP(S):

MULTIPOP(S, k):

4.

### All operations have O(1) amortized cost → total amortized cost is O(n)

21

Practice: justify why it is zero

ci: the actual cost of i-th operation ĉi: the amortized cost of i-th operation

(22)

## 22

(23)

23

### Fibonacci heap (Textbook Ch. 19)

BUILD-MIN-HEAP:

EXTRACT-MIN: (amortized)

DECREASE-KEY: (amortized)

### Total complexity:

MST-PRIM(G, w, r) // w = weights, r = root for u in G.V

u.key = ∞ u.π = NIL r.key = 0 Q = G.V

while Q ≠ empty

u = EXTRACT-MIN(Q) for v in G.adj[u]

if v ∈ Q and w(u, v) < v.key v.π = u

v.key = w(u, v) // DECREASE-KEY

(24)

### Fibonacci heap (Textbook Ch. 19)

BUILD-MIN-HEAP:

EXTRACT-MIN: (amortized)

DECREASE-KEY: (amortized)

### Total complexity:

24

DIJKSTRA(G, w, s)

INITIALIZATION(G, s) S = empty

Q = G.v // INSERT while Q ≠ empty

u = EXTRACT-MIN(Q) S = S∪{u}

RELAX(u, v, w)

INITIALIZATION(G, s) for v in G.V

v.d = ∞ v.π = NIL s.d = 0

RELAX(u, v, w)

if v.d > u.d + w(u, v) // DECREASE-KEY

v.d = u.d + w(u, v) v.π = u

(25)

Textbook Chapter 17.1 – Aggregate analysis

Textbook Chapter 17.2 – The accounting method Textbook Chapter 17.3 – The potential method

## 25

(26)

### Implementation with a k-bit array

26

INCREMENT(A) i = 0

while i < A.length and A[i] == 1 A[i] = 0

i = i + 1 if i < A.length

A[i] = 1

Binary Counter

Suppose that a counter is initially zero. We increment the counter n times. How many bits are altered throughout the process?

0 1 10 11 100 101 110 111 1000

1001 1010 1011 1100 1101 1110 1111 10000 increment

### n operations take O(n log n) time

(27)

27

Counter

Value A[3] A[2] A[1] A[0] Total Cost of First n

Operations

0 0 0 0 0 0

1 0 0 0 1 1

2 0 0 1 0 3

3 0 0 1 1 4

4 0 1 0 0 7

5 0 1 0 1 8

6 0 1 1 0 10

7 0 1 1 1 11

8 1 0 0 0 15

flip every increment flip every 2 increments

flip every 4 increments flip every 8 increments

(28)

28

(29)

1.

2.

### Validity check:

Each bit 0 to bit 1, we save additional \$1 in the bit 1

When bit 1 becomes to bit 0, we spend the saved cost 3.

### Each increment

Change many 1s to 0s → free

Change exactly a 0 to 1 → O(1)

### Each amortized cost is O(1) → total amortized cost is O(n)

29

Operation Actual Cost Amortized Cost

bit 0 → bit 1 1 2 (存\$1到bit 1)

bit 1 → bit 0 1 0 (用掉存在bit 1裡面的\$1)

increment #flipped bits 2 for setting a bit to 1

(30)

30

Counter

Value A[3] A[2] A[1] A[0] Total Cost of First n

Operations

0 0 0 0 0 0

1 0 0 0 1 1

2 0 0 1 0 3

3 0 0 1 1 4

4 0 1 0 0 7

5 0 1 0 1 8

6 0 1 1 0 10

7 0 1 1 1 11

8 1 0 0 0 15

Amortized cost per operation is O(1)

Total amortized cost of n operations is O(n)

(31)

1.

𝑖

2.

### Validity check:

The counter is initially zero →

The number of 1’s cannot be negative →

3.

### Compute amortized cost of each INCREMENT:

Let LSB0(i) be the number of continuous 1s in the suffix

For example, LSB0(01011011) = 2, and LSB0(01011111) = 5

4.

### All operations have O(1) amortized cost → total amortized cost is O(n)

31

ci: the actual cost of i-th operation ĉi: the amortized cost of i-th operation

(32)

32

Aggregate method (聚集法)

• Determine an upper bound 𝑇(𝑛) on the cost over any sequence of 𝑛 operations

• The average cost per operation is then 𝑇(𝑛)/𝑛

• All operations have the same amortized cost

Accounting method (記帳法)

• Each operation is assigned an amortized cost (may differ from the actual cost)

• Each object of the data structure is associated with a credit

• Need to ensure that every object has sufficient credit at any time

Potential method (位能法)

• Similar to accounting method; each operation is assigned an amortized cost

• The data structure as a whole maintains a credit (i.e., potential)

• Need to ensure that the potential level is nonnegative at any time

(33)

## 33

Important announcement will be sent to @ntu.edu.tw mailbox

& post to the course website

 Combine: find closet pair with one point in each region, and return the best of three

jobs

▪ Step 2: Run DFS on the transpose

Greedy-Choice Property : making locally optimal (greedy) choices leads to a globally optimal

 From a source vertex, systematically follow the edges of a graph to visit all reachable vertices of the graph.  Useful to discover the structure of

 “Greedy”: always makes the choice that looks best at the moment in the hope that this choice will lead to a globally optimal solution.  When to

In particular, we present a linear-time algorithm for the k-tuple total domination problem for graphs in which each block is a clique, a cycle or a complete bipartite graph,

✓ Express the solution of the original problem in terms of optimal solutions for subproblems. Construct an optimal solution from