CSE 548: (Design And) Analysis of Algorithms
CSE 548: (Design And) Analysis of Algorithms
CSE 548: (Design And) Analysis of Algorithms
R. Sekar
1 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Motivation
Amortized Analysis
Amortization
The spreading out of capital expenses for intangible assets over a
specific period of time (usually over the asset’s useful life) for
accounting and tax purposes.
2 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Motivation
Topics
3 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets
Some operations have high worst-case cost, but we can show that
the worst case does not occur every time.
In this case, we can average the costs to obtain a better bound
Summation
Let T (n) be the worst-case running time for executing a sequence of
n operations. Then the amortized time for each operation is T (n)/n.
4 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets
5 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets
Charging Method
Certain operations charge more than their cost so as to pay for other
operations. This allows total cost to be calculated while ignoring the
second category of operations.
Stack Example
Potential Method
8 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets
Stack:
Each push costs 2 units because a push increases potential
energy by 1.
Pops can use the energy released by reduction in stack size!
Counter:
Define potential as the number one 1-bits
Changing a 0 to 1 costs 2 units, one for the operation and one
to pay for increase in potential
Changes of 1 to 0 can now be paid by released potential.
9 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Amortized Rehashing Vector and String Resizing
Hash Tables
10 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Amortized Rehashing Vector and String Resizing
Amortized Rehashing
11 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Amortized Rehashing Vector and String Resizing
Are we done?
Consider total cost after 2K, 3K, and 4K operations:
12 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Amortized Rehashing Vector and String Resizing
16 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Amortized Rehashing Vector and String Resizing
gure 5.5 A directed-tree representation of two sets {B, E} and {A, C, D, F, G, H}.
E H
B C D F
G A
18 / 33
et, arranged in no particular order, and each has parent pointers that eve
Intro Aggregate Charging Potential Table resizing Disjoint sets Inverted Trees Union by Depth Threaded Trees Path compression
A0 B
0 0
C D
0
E
0
F0 G
0
D1 E1 F1 G0
A0 B
0
C0
D2 F1 21 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Inverted Trees Union by Depth Threaded Trees Path compression
0 0
Disjoint Sets withA Union by
0
B
DepthC (3)
After union(C, G), union(E, A):
D2 F1
E1
A0 C
0 0
G
B0
D2
E1 F1
A0
B0 C0 G
0
22 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Inverted Trees Union by Depth Threaded Trees Path compression
23 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Inverted Trees Union by Depth Threaded Trees Path compression
24 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Inverted Trees Union by Depth Threaded Trees Path compression
a p a
b c d q r p q r b c d
Merging two sets stored as threaded trees.
Problem:
old arrows pointWorst-case complexity
to leaders; lighter of union
arrows form becomes
the threads. ShadedO(n)
nodes have a new lead
Solution:
UNION(x, y):
Merge smaller set with larger set x ← FIND(x)
Amortize cost of union over other operations
y ← FIND( y)
y←y 25 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Inverted Trees Union by Depth Threaded Trees Path compression
26 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Inverted Trees Union by Depth Threaded Trees Path compression
Further improvement
Can we combine the best elements of the two approaches?
Threaded trees employ an eager approach to union while the original
approach used a lazy approach
Eager approach is better for find, while being lazy is better for union.
So, why not use lazy approach for union and eager approach for find?
1
B0 C E2 B0 C1 E2 F
1
I
0
D0 F1 G1 H
0
−→ D0 G1 H0 J
0
0
I0 J
0
K K0
A3
−→ B0 C
1
E2 F
1
I0 K0 G1
D0 H0 J0
28 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Inverted Trees Union by Depth Threaded Trees Path compression
29 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Inverted Trees Union by Depth Threaded Trees Path compression
32 / 33
Intro Aggregate Charging Potential Table resizing Disjoint sets Inverted Trees Union by Depth Threaded Trees Path compression