CS294-1 On-line Computation & Net w ork Algorithms Spring - - PDF document

cs294 1 on line computation net w ork algorithms spring
SMART_READER_LITE
LIVE PREVIEW

CS294-1 On-line Computation & Net w ork Algorithms Spring - - PDF document

CS294-1 On-line Computation & Net w ork Algorithms Spring 1997 Lecture 2: Jan uary 23 L e ctur er: Y air Bartal Scrib e: Keith V anderve en This lecture in tro duces amortized analysis tec hniques and


slide-1
SLIDE 1 CS294-1 On-line Computation & Net w
  • rk
Algorithms Spring 1997 Lecture 2: Jan uary 23 L e ctur er: Y air Bartal Scrib e: Keith V anderve en This lecture in tro duces amortized analysis tec hniques and demonstrates their use in analyzing the cost
  • f
t w
  • simple
data structures problems. 2.1 Amorti ze d Anlysis Amortized analysis [T arjan85 , CLR90] is
  • rginated
in the study
  • f
dynamic data structures. Eac h
  • p
eration is regarded as starting with some initial amoun t
  • f
\money ." If the
  • p
eration costs less than this amoun t
  • f
money , the excess is stored as credit to w ard pa ying the costs
  • f
  • p
erations whic h cost more than the initial amoun t
  • f
money . In Data Structures: Supp
  • se
y
  • u
carry
  • ut
a sequence
  • f
  • p
erations
  • 1
;
  • 2
; :::;
  • n
. Then w
  • rst-case
cost = max i c(o i ): total cost = X i c(o i ): amortized cost = total cost n
  • ~
c F
  • r
example, in dynamic hashing the w
  • rst-case
searc h cost is (log n) whereas the amortized cost is O (1). 2.2 Amorti zatri
  • n
T ec hniques Through Examples 2.2.1 The Binary Coun ter Initially , all the bits are set to 0. T
  • incremen
t the coun ter b y 1,
  • ne
go es righ t to left ipping bits un til a bit is ipp ed from to 1. The cost
  • f
the ith up date, c i , is the n um b er
  • f
bits whic h are ipp ed. w
  • rst-case
cost = k : 2-1
slide-2
SLIDE 2 2-2 Lecture 2: Jan uary 23
  • b

b b b b

k-1 k-2 k-3 1

Figure 2.1: k-bit binary coun ter total cost = c(n) = n X i c i = O (nk ): amortized cost ~ c = c(n) n : Claim 2.1 ~ c
  • 2.
Pro
  • f:
3 tec hniques to pro v e this:
  • The
Engineer's view
  • The
Bank er's view
  • The
Ph ysicist's view 2.2.1.1 The Engineer's view Idea: brute force calculation! Observ e: b ips ev ery up date b 1 ips ev ery 2 up dates . . . b i ips ev ery 2 i up dates c(n) = blog nc X i=0 b n 2 i c < 2n: ~ c < 2:
slide-3
SLIDE 3 Lecture 2: Jan uary 23 2-3 2.2.1.2 The Bank er's View: (The Credit-Debit Metho d) Idea: Eac h
  • p
eration comes in with ~ c $. During execution, if the cost
  • f
an
  • p
eration is less than ~ c, store the excess ( ~ c
  • cost)
as credit. If the cost
  • f
an
  • p
eration is greater than ~ c , withdra w previously stored credit to complete pa ymen t
  • f
(cost ( ~ c). Theorem 2.2 If system has non-ne gative cr e dit at al l times, then ~ c is a b
  • und
  • n
the amotize d c
  • st.
Bac k to the Binary Coun ter problem, w e set ~ c = 2 and asso ciate credits with bit p
  • sitions.
Case 1: W e ip a bit in to 1, pa y 1 unit and store 1 unit at that bit's p
  • sition.
Case 2: W e ip a bit in to 0, withdra w 1 unit from credit at that bit's p
  • sition.
  • 1

b b b b b

k-1 k-2 k-3 1

1

Figure 2.2: after rst up date: 1 credit
  • n
bit
  • 1

1

b b b b b

k-1 k-2 k-3 1

Figure 2.3: after 2nd up date: 1 credit
  • n
bit 1 (paid to ip bit with credit for that bit) It can b e sho wn b y an induction argumen t that ev ery time a bit i is ipp ed to 1, it will ha v e a credit
  • f
1 dep
  • sited
at its p
  • sition.
This is b ecause all
  • f
the bits to its righ t m ust ha v e b een 1 after the previous up date, hence they had credit to pa y for their ips, and w e got t w
  • units
  • f
credit for the up date,
  • ne
to ip the bit i and the
  • ther
to store as credit at i's p
  • sition.
2.2.1.3 The Ph ysicist's View: (The P
  • ten
tial F unction Metho d) Idea: Store pre-paid credit as a "p
  • ten
tial energy"
  • f
the en tire system. Use p
  • ten
tial to pa y for
  • p
erations whic h cost more energy than they bring in.
slide-4
SLIDE 4 2-4 Lecture 2: Jan uary 23 F
  • rmally
, let
  • i
denote the p
  • ten
tial at time i. Clearly , ~ c = c i + , where
  • =
  • i
  • i1
, th us assume ~ c i = b, the total amortized cost is b
  • n
= n X i=0 ~ c i = n X i=1 (c i +
  • i
  • i1
) = n X i=1 c i +
  • n
  • Theorem
2.3 If
  • n
  • (usual
ly
  • =
0), then ~ c (n) = c(n) n
  • b.
Imp
  • rtan
t: W an t
  • i
  • for
all i
  • n.
T
  • pro
v e ~ c i b
  • unded,
w e can sho w that c i
  • ~
c i
  • .
The p
  • ten
tial metho d is applied to the Binary Coun ter problem as follo ws. Let
  • i
= n um b er
  • f
bits set to 1 at time i. Let
  • =
0. Then 8i; i
  • 0.
Consider the ith up date. There exists a j suc h that bit j is ipp ed in to a 1 and all bits less than j are ipp ed to 0. Therefore,
  • i
  • i1
= 1
  • j:
W e w an t to sho w ~ c = 2. c i = j + 1 = 2
  • (1
  • j
) = ~ c i
  • 2.2.2
A Queue F rom Tw
  • Stac
ks W e ma y use t w
  • stac
ks A and B and need to implemen t a queue. The Queue
  • p
erations include: insert item x (I (x)) and delete item x (D (x)). An insert places a new item at the tail
  • f
the queue, and a delete remo v es an item from the head
  • f
the queue. W e are giv en a sequence
  • f
insert and delete
  • p
erations. Consider the follo wing algorithm. Newly inserted items are pushed in to stac k A. When a delete
  • ccurs
w e rst c hec k if there are items in stac k B. If so w e p
  • p
the item at the top
  • f
stac k B. Otherwise all items in stac k A are p
  • pp
ed
  • the
stac k and all except the last
  • ne
are pushed in to stac k B. It is easy to c hec k the correctness
  • f
this algorithm. W e pro v e the follo wing:
slide-5
SLIDE 5 Lecture 2: Jan uary 23 2-5 Claim 2.4 The 2-Stacks algorithm has amortize d c
  • st
at most 3. Pro
  • f:
W e use the Credit-Debit metho d. Let eac h insert
  • p
eration come with 3 units and eac h delete
  • p
eration come with 1 unit. Then inserting an item in to stac k A tak es 1 unit, and p
  • pping
the item and inserting it in to stac k 2 tak es 2 units. Deleting the item from stac k 2 then tak es 1 unit. Then the cost (whic h is the total n um b er
  • f
pushes and p
  • ps)
  • b
eys total cost
  • 3
  • #
  • f
Inserts + #
  • f
Deletes
  • 3n;
where n is the
  • v
erall n um b er
  • f
  • p
erations. References [CLR90] T.H. Cormen, C.E. Leiserson, and R.L. Riv est. Intr
  • duction
to A lgorithms. The MIT Press, 1990. [T arjan85] R.E. T arjan. Amortized Computational Complexit y . SIAM Journal
  • n
A lgebr aic and Discr ete Metho ds, 6(2):306-318, 1985.