Amortized Analysis of Union/Find operations CPSC 320 2012W T1 1 - - PowerPoint PPT Presentation

amortized analysis of union find operations
SMART_READER_LITE
LIVE PREVIEW

Amortized Analysis of Union/Find operations CPSC 320 2012W T1 1 - - PowerPoint PPT Presentation

CPSC 320: Intermediate Algorithm Design and Analysis Amortized Analysis of Union/Find operations CPSC 320 2012W T1 1 Potential Function Relationships between a node N and its parent P: N is close if log 2 rank(N) = log 2


slide-1
SLIDE 1

CPSC 320 – 2012W T1 1

CPSC 320: Intermediate Algorithm Design and Analysis

Amortized Analysis of Union/Find

  • perations
slide-2
SLIDE 2

CPSC 320 – 2012W T1 2

Potential Function

Relationships between a node N and its parent P:

N is close if ⌊ log

2 rank(N) ⌋ = ⌊ log 2 rank(P) ⌋.

N is far if N is not close, but rank(N) and rank(P) belong to the same interval. N is really far if rank (N) and rank(P) belong to different intervals.

slide-3
SLIDE 3

CPSC 320 – 2012W T1 3

Potential Function

Potential of a node N with parent P: Observe that ϕ(N) is always an integer. The potential of D

i is

ϕ(N)={ 3rank (N) if N isaroot 3rank (N)−rank(P) if N isclose rank (N)+⌊log2rank(N)⌋−⌊log2rank(P)⌋ if N isfar

  • therwise

Φ(Di)= ∑

N ∈D i

phi(N)

slide-4
SLIDE 4

CPSC 320 – 2012W T1 4

Observations

Observation 1: if N is close than rank(P) < 2 rank(N). Proof: if rank(P) ≥ 2 rank(N) then log

2 rank(P) ≥ log 2

rank(N) + 1 and so ⌊ log

2 rank(P) ⌋ > ⌊ log 2 rank(N) ⌋.

Observation 2: If N and P are both in the interval from k+1 to 2

k then ⌊ log 2 rank(P) ⌋ - ⌊ log 2 rank(N) ⌋ < rank(N)

Proof: ⌊log

2 rank(P)⌋ - ⌊log 2 rank(N)⌋ ≤ k - ⌊log 2 (k+1)⌋ <

k < rank(N). Corollary: for every node N, ϕ(N) ≥ 0.

slide-5
SLIDE 5

CPSC 320 – 2012W T1 5

Proof (part 1)

For makeSet(x):

cost

real(makeSet) ∈ Θ(1).

potential difference = 3 rank(new node) = 0. Hence cost

am(makeSet) ∈ Θ(1).

slide-6
SLIDE 6

CPSC 320 – 2012W T1 6

Proof (part 2)

For find(x):

Suppose the algorithm goes through x = x

0, x 1, ..., x l-1, x l

(the root) cost

real(find) = l + 1.

How does ϕ(N = x

j) change?

We will look at every node x

0, x 1, ..., x l-1, x l.

We will show that the potential ϕ(x

j) goes down by at least

1 for every j. Except for O(log* n) x

j's

slide-7
SLIDE 7

CPSC 320 – 2012W T1 7

Proof (part 2)

For find(x) – continued:

Case 1: N is x

l-1 or x l

The nodes x

l-1 and x l do not move. Hence neither ϕ(x l-1) nor

ϕ(x

l) changes.

There are only two such nodes. ⇒exceptions

Observe that in all other cases rank(x

l) > rank(P).

Case 2: N was close before path compression, N is really far after path compression.

ϕ(N) was at least 1, and is now 0. So it has decreased.

slide-8
SLIDE 8

CPSC 320 – 2012W T1 8

Proof (part 2)

For find(x) – continued:

Case 3: N was far before path compression, N is really far after path compression.

ϕ(N) was at least 1, and is now 0. So it has decreased.

Case 4: N was really far before path compression, N is really far after path compression.

ϕ(N) has not changed: it was and is still 0. There is at most one such node per interval. Hence there are O(log* n) such nodes. ⇒exceptions

slide-9
SLIDE 9

CPSC 320 – 2012W T1 9

Proof (part 2)

For find(x) – continued:

Case 5: N was close before path compression, N is close after path compression.

rank(x

l) > rank(P).

Hence 3 rank(N) – rank(x

l) < 3 rank(N) – rank (P).

This means that ϕ(N) has decreased.

slide-10
SLIDE 10

CPSC 320 – 2012W T1 10

Proof (part 2)

For find(x) – continued:

Case 6: N was far before path compression, N is far after path compression.

Before path compression ϕ(N) = rank(N) + ⌊ log2 rank(N) ⌋ – ⌊ log2 rank(P) ⌋. After path compression ϕ(N) = rank(N) + ⌊ log2 rank(N) ⌋ – ⌊ log2 rank(xl) ⌋. Is ⌊ log2 rank(xl) ⌋ > ⌊ log2 rank(P) ⌋ ? Maybe not for the far node closest to the root on the path x0, x1, ..., xl-1, xl. ⇒ one more exception But it's true for every other far node N on this path.

slide-11
SLIDE 11

CPSC 320 – 2012W T1 11

Proof (part 2)

For find(x) – continued:

Case 7: N was close before path compression, N is far after path compression.

Before path compression ϕ(N) = 3rank(N) – rank(P), which is bigger than rank(N) by observation 1. After path compression ϕ(N) = rank(N) + ⌊ log

2 rank(N) ⌋ –

⌊ log

2 rank(x l) ⌋, which by observation 2 is smaller than

rank(N). Hence ϕ(N) has decreased by at least 2.

slide-12
SLIDE 12

CPSC 320 – 2012W T1 12

Proof (part 2)

For find(x) – continued:

So the potential decreases by at least l + 1 - (log* n + 3). Therefore cost

am(find) ≤ l + 1 - (l + 1 - (log* n + 3)).

This means cost

am(find) ∈ O(log* n).

slide-13
SLIDE 13

CPSC 320 – 2012W T1 13

Proof (part 3)

For union(x,y):

Each of the two calls find(x), find(y) has amortized cost in O(log* n). What of the union operation itself?

Assume that x

l becomes a child of y l.

cost

real(union) ∈ Θ(1).

ϕ(x

l) was 3 rank(x l) before the union, and is now either 3

rank(x

l) – rank(y l), or rank(x l) + ⌊ log 2 rank(x l) ⌋ – ⌊ log 2

rank(y

l) ⌋, or 0 after the union.

So ϕ(x

l) has decreased.

slide-14
SLIDE 14

CPSC 320 – 2012W T1 14

Proof (part 3)

For union(x,y) – continued:

What of the union operation itself – continued?

If rank(y

l) increases, then ϕ(y l) will increase by 3.

The potential ϕ(z) associated with a child z of y

l may

decrease (since its parent rank has increased). So the potential difference is in Θ(1).

And hence cost

am(union) ∈ O(log* n).

Therefore we can conclude that a sequence of m

  • perations on an initially empty union/find data

structure with a total of n elements runs in O(m log* n) time in the worst case.