Today's Outline: I I I 0 I 0 K I I
Today's Outline: I I I 0 I 0 K I I
CS 362, Lecture 10
• Fractional Knapsack Wrapup
Jared Saia • Amortized Analysis
University of New Mexico
Proof Proof
2 3
Proof Proof
n n
vk
V −V0 = x0ivi
X X
xi v i − (1) • Note that the last step follows because w is positive and
k
i=1 i=1 because:
n
(xi − x0i) ∗ vi n n n
X
= (2)
(xi − x0i) ∗ wi = x0i ∗ wi
X X X
i=1
xi w i − (7)
n ! i=1 i=1 i=1
vi
(xi − x0i) ∗ wi = W − W0
X
= (3) (8)
i=1 wi
n ! ≥ 0. (9)
X
0 vk
≥ (xi − xi) ∗ wi (4) • Where W is the total weight taken by greedy and W 0 is the
i=1 ! wk
vk n total weight for the strategy B
(xi − x0i) ∗ wi
X
≥ ∗ (5) • We know that W ≥ W 0
wk i=1
≥ 0 (6)
4 5
In-Class Exercise Q1
v v
(xi − x0i) i ≥ (xi − x0i) k
Consider the inequality: wi wk
v v
(xi − x0i) i ≥ (xi − x0i) k • Q1: Show that the inequality is true for i < k
wi wk
• For i < k, (xi − x0i) ≥ 0
• Q1: Show this inequality is true for i < k • If (xi − x0i) = 0, trivially true. Otherwise, can divide both
• Q2: Show it’s true for i = k sides of the inequality by xi − x0i to get
• Q3: Show it’s true for i > k vi v
≥ k.
wi wk
• This is true since the items are sorted by profit per weight
6 7
Q2 Q3
v v
(xi − x0i) i ≥ (xi − x0i) k
wi wk
v v
(xi − x0i) i ≥ (xi − x0i) k • Q3: Show that the inequality is true for i > k
wi wk • For i < k, (xi − x0i) ≤ 0
• If (xi − x0i) = 0, trivially true. Otherwise can divide both sides
• Q2: Show that the inequality is true for i = k
of the inequality by xi − x0i to get
• When i = k, we have
vi v
v v ≤ k.
(xk − x0k ) k ≥ (xk − x0k ) k wi wk
wk wk
• This is obviously true since the items are sorted by profit per
• Which is true since the left side equals the right side
weight
• Note that the direction of the inequality changed when we
divided by (xi − x0i), since it is negative
8 9
10 11
Types of Amortized Analysis Aggregate Analysis
• Aggregate Analysis
• Accounting or Taxation Method • We get an upperbound T (n) on the total cost of a sequence
• Potential method of n operations. The average cost per operation is then
• We’ll see each method used for 1) a stack with the additional T (n)/n, which is also the amortized cost per operation
operation MULTIPOP and 2) a binary counter
12 13
14 15
Multipop Analysis The Problem
16 17
18 19
Another Example Binary Counter
20 21
Increment Analysis
Increment(A){
i = 0;
• It’s not hard to see that in the worst case, the increment
while(i<k && A[i]=1){
procedure takes time Θ(k)
A[i] = 0;
• Thus a sequence of n increments takes time O(nk) in the
i++;
worst case
}
• Note that again this bound is correct but overly pessimistic
if (i<k)
- not all bits flip each time increment is called!
A[i] = 1;
}
22 23
Aggregate Analysis Aggregate Analysis
j k
• In general, for i = 0, . . . blog nc, bit A[i] flips n/2i times in a
• In fact, we can show that a sequence of n calls to Increment sequence of n calls to Increment on an initially zero counter
has a worst case time of O(n) • For i > blog nc, bit A[i] never flips
• A[0] flips every time Increment is called, A[1] flips over every • Total number of flips in the sequence of n calls is thus
other time, A[2] flips over every fourth time, . . .
blog
Xnc n ∞
• Thus if there are n calls to increment, A[0] flips n times, A[1] X 1
< n (10)
flips bn/2c times, A[2] flips bn/4c times i=0 2i i=0 2i
= 2n (11)
24 25
26 27
Taxation Method for Multipop Taxation Method
• Instead of paying for each Push and Pop operation when they
• Like any good government (ha ha) we need to make sure
occur, let’s tax the pushes to pay for the pops
that: 1) our taxes are low and 2) we can use our taxes to
• I.e. we tax the push operation 2 dollars, and the pop and
pay for all our costs
multipop operations 0 dollars
• We already know that our taxes for n operations are no more
• Then each time we do a push, we spend one dollar of the
than 2n dollars
tax to pay for the push and then save the other dollar of the
• We now want to show that we can use the 2 dollars we collect
tax to pay for the inevitable pop or multipop of that item
for each push to pay for all the push, pop and multipop
• Note that if we do n operations, the total amount of taxes
operations
we collect is then 2n
28 29
30 31
Taxation Method for Binary Counter
Taxation Scheme
32
33
• A sequence of Pushes and Pops is performed on a stack • A sequence of Pushes and Pops is performed on a stack
whose size never exceeds k whose size never exceeds k
• After every k operations, a copy of the entire stack is made • After every k operations, a copy of the entire stack is made
for backup purposes for backup purposes
• Show that the cost of n stack operations, including copying • Show that the cost of n stack operations, including copying
the stack, is O(n) the stack, is O(n)
36 37
In Class Exercise