11/11/2014 CMPS 2200 Intro. to Algorithms 1
CMPS 2200 – Fall 2014
Amortized Analysis
Carola Wenk
Slides courtesy of Charles Leiserson with changes by Carola Wenk
Amortized Analysis Carola Wenk Slides courtesy of Charles Leiserson - - PowerPoint PPT Presentation
CMPS 2200 Fall 2014 Amortized Analysis Carola Wenk Slides courtesy of Charles Leiserson with changes by Carola Wenk CMPS 2200 Intro. to Algorithms 11/11/2014 1 Dynamic tables Task: Store a dynamic set in a table/array. Elements can only
11/11/2014 CMPS 2200 Intro. to Algorithms 1
Slides courtesy of Charles Leiserson with changes by Carola Wenk
11/11/2014 CMPS 2200 Intro. to Algorithms 2
11/11/2014 CMPS 2200 Intro. to Algorithms 3
1
11/11/2014 CMPS 2200 Intro. to Algorithms 4
1
11/11/2014 CMPS 2200 Intro. to Algorithms 5
1 2
11/11/2014 CMPS 2200 Intro. to Algorithms 6
1 2
11/11/2014 CMPS 2200 Intro. to Algorithms 7
2 1
11/11/2014 CMPS 2200 Intro. to Algorithms 8
2 1
11/11/2014 CMPS 2200 Intro. to Algorithms 9
4 3 2 1
11/11/2014 CMPS 2200 Intro. to Algorithms 10
4 3 2 1
11/11/2014 CMPS 2200 Intro. to Algorithms 11
4 3 2 1
11/11/2014 CMPS 2200 Intro. to Algorithms 12
4 3 2 1
11/11/2014 CMPS 2200 Intro. to Algorithms 13
6
5 4 3 2 1 7
11/11/2014 CMPS 2200 Intro. to Algorithms 14
11/11/2014 CMPS 2200 Intro. to Algorithms 15
11/11/2014 CMPS 2200 Intro. to Algorithms 16
11/11/2014 CMPS 2200 Intro. to Algorithms 17
11/11/2014 CMPS 2200 Intro. to Algorithms 18
11/11/2014 CMPS 2200 Intro. to Algorithms 19
) 1 log( 1
n j j n i i
11/11/2014 CMPS 2200 Intro. to Algorithms 20
11/11/2014 CMPS 2200 Intro. to Algorithms 21
11/11/2014 CMPS 2200 Intro. to Algorithms 22
11/11/2014 CMPS 2200 Intro. to Algorithms 23
n i i n i i
1 1
11/11/2014 CMPS 2200 Intro. to Algorithms 24
11/11/2014 CMPS 2200 Intro. to Algorithms 25
11/11/2014 CMPS 2200 Intro. to Algorithms 26
11/11/2014 CMPS 2200 Intro. to Algorithms 27
11/11/2014 CMPS 2200 Intro. to Algorithms 28
0,0,…,0. The counter supports the following INCREMENT
i 0 while i<length(A) and A[i]=1 do A[i] 0 i++ if i<length(A) then A[i] 1
the amortized runtime of one INCREMENT operation?
11/11/2014 CMPS 2200 Intro. to Algorithms 29
Initial counter 0 0 0 0 0 0 0 0 After 1 increment 0 0 0 0 0 0 0 1 After 2 increments 0 0 0 0 0 0 1 0 After 3 increments 0 0 0 0 0 0 1 1 After 4 increments 0 0 0 0 0 1 0 0 After 5 increments 0 0 0 0 0 1 0 1 After 6 increments 0 0 0 0 0 1 1 0 After 7 increments 0 0 0 0 0 1 1 1 After 8 increments 0 0 0 0 1 0 0 0 After 9 increments 0 0 0 0 1 0 0 1 Example for k=8 and n=9:
10 flip 01 flip
$1 $1 $1 $1 $2 $1 $1 $1 $1 $1 $1 $3 $1
11/11/2014 CMPS 2200 Intro. to Algorithms 30
flipped back to 0
pay for this flip
its own cost, and each 10 flip is prepaid by the $1 credit on it.
11/11/2014 CMPS 2200 Intro. to Algorithms 31
Initial counter 0 0 0 0 0 0 0 0 After 1 increment 0 0 0 0 0 0 0 1 After 2 increments 0 0 0 0 0 0 1 0 After 3 increments 0 0 0 0 0 0 1 1 After 4 increments 0 0 0 0 0 1 0 0 After 5 increments 0 0 0 0 0 1 0 1 After 6 increments 0 0 0 0 0 1 1 0 After 7 increments 0 0 0 0 0 1 1 1 After 8 increments 0 0 0 0 1 0 0 0 After 9 increments 0 0 0 0 1 0 0 1 Example for k=8 and n=9:
10 flip 01 flip
$1 $1 $1 $1 $2 $1 $1 $1 $1 $1 $1 $3 $1
10 flip 01 flip
$2 $2 $0 $2 $0 $2 $2 $2 $0 $2 $2 $0 $2
Actual cost Amortized cost
11/11/2014 CMPS 2200 Intro. to Algorithms 32
11/11/2014 CMPS 2200 Intro. to Algorithms 33