Algorithm Presentation Group: Tarjan Union Find Algorithm Group: - - PowerPoint PPT Presentation
Algorithm Presentation Group: Tarjan Union Find Algorithm Group: - - PowerPoint PPT Presentation
Algorithm Presentation Group: Tarjan Union Find Algorithm Group: Tarjan Dio, Zack, Chips 2016. 12 Contents 1 Basic Idea 2 Union By Rank 3 Path Compression 4 Analysis of Path Compression 5 Further Analysis Contents 1 Basic Idea
Union Find Algorithm
Group: Tarjan Dio, Zack, Chips
- 2016. 12
Contents
1
Basic Idea
2
Union By Rank
3
Path Compression
4
Analysis of Path Compression
5
Further Analysis
Contents
1 2 3 4 5
Basic Idea Union By Rank Path Compression Analysis of Path Compression Further Analysis
▪ Used to manage a bunch of disjoint sets. ▪ Find (v): return a canonical element of the set containing v. ▪ Union (u, v): merges the sets containing the canonical elements u and v. ▪ Make_Set (v): add some new element into the bunch. ▪ Example: Find (6) : return B
Bas asic idea ea
1, 2, 4 3 5, 7, 9 6, 8
A B C D
1, 2, 4 3 5, 6, 7, 8, 9
A B C Union (5, 6) =>
Contents
1 2 3 4 5
Basic Idea Union By Rank Path Compression Analysis of Path Compression Further Analysis
▪ Represent each set as a tree with data elements at the vertices. ▪ Each element u has a pointer to its parent. ▪ The root serves as the canonical element of the set. ▪ Observation: 𝑠𝑏𝑜𝑙(𝑦) = ℎ𝑓𝑗ℎ𝑢 𝑝𝑔 𝑡𝑣𝑐𝑢𝑠𝑓𝑓 𝑠𝑝𝑝𝑢𝑓𝑒 𝑐𝑧 𝑦. ▪ Lemma: 𝑡𝑗𝑨𝑓(𝑡𝑣𝑐𝑢𝑠𝑓𝑓 𝑝𝑔 𝑦) ≥ 2𝑠𝑏𝑜𝑙(𝑦) ▪ Corollary 1: 𝑠𝑏𝑜𝑙 𝑦 ≤ log 𝑛 ▪ Corollary 2: (𝑜𝑣𝑛𝑐𝑓𝑠 𝑝𝑔 𝑓𝑚𝑓𝑛𝑓𝑜𝑢 𝑠𝑏𝑜𝑙𝑓𝑒 𝑙) ≤ 𝑜/2𝑙
Rep epresent as as a a tree ee
Union by rank ank
Union (5, 6) => Union (4, 5) =>
Contents
1 2 3 4 5
Basic Idea Union By Rank Path Compression Analysis of Path Compression Further Analysis
▪ After find, change the parent pointers of all vertices along the path directly to v. ▪ Example:
Path Compression
▪ Observation: 𝑠𝑏𝑜𝑙 𝑦 ≠ ℎ𝑓𝑗ℎ𝑢 𝑦 𝑗𝑡 𝑞𝑝𝑡𝑡𝑗𝑐𝑚𝒇 ▪ Corollary 1, 2 still hold Corollary 1: 𝑠𝑏𝑜𝑙 𝑦 ≤ log 𝑛
Corollary 2: 𝑜𝑣𝑛𝑐𝑓𝑠 𝑝𝑔 𝑓𝑚𝑓𝑛𝑓𝑜𝑢 𝑠𝑏𝑜𝑙𝑓𝑒 𝑙 ≤ 𝑜/2𝑙
5
3 3 2 1
8 6 5
3 3 2
8 0 6
1 𝐺𝑗𝑜𝑒(8) →
Contents
1 2 3 4 5
Basic Idea Union By Rank Path Compression Analysis of Path Compression Further Analysis
▪ Define 𝑏𝑞 (𝑦) = 𝑠𝑏𝑜𝑙 (𝑞𝑏𝑠𝑓𝑜𝑢) – 𝑠𝑏𝑜𝑙(𝑦). (In the picture, blue number is rank, red number is gap). ▪ A vertex is happy if 𝑏𝑞(𝑦) > 𝑠𝑏𝑜𝑙(𝑦). (i.e. rank (parent) > 2rank (x)) and sad otherwise. ▪ How many vertices can be happy in the path from x to root? ▪ Assume x1, x2, x3, …, xk is the sequence of happy vertices
- n the path. 𝑠𝑏𝑜𝑙 𝑦𝑗 ≥ 2𝑗 − 1
𝑀𝑝(𝑛) ≥ 𝑠𝑏𝑜𝑙(𝑠𝑝𝑝𝑢) >= 2𝑙 − 1 𝑙 ≤ 𝑚𝑝𝑚𝑝(𝑛);
Analysis of Path Compression
u
11 8
v
19
z
5 6
y
3 2
x
3 Sad Happy Happy Sad
▪ Observation: n: every time x is part of a find operation, its gap increase by 1. ▪ Observation n 1: along path, ℎ𝑏𝑞𝑞𝑧 𝑤𝑓𝑠𝑢𝑗𝑑𝑓𝑡 ≤ 𝑚𝑝𝑚𝑝 𝑛 ▪ Observation n 2: A sad vertex will be happy after rank(x) find ops.
Analysis of Path Compression
u
root
2
y
50 51 28
x
30 20 1
root
51
y
50
x
30
u
2 𝐺𝑗𝑜𝑒(𝑣) → 1 21 49
▪ Suppose there are m find operations.
▪ 𝑗=1
𝑛 𝑢𝑗𝑛𝑓 𝑔𝑝𝑠 𝑗𝑢ℎ 𝑔𝑗𝑜𝑒 𝑝𝑞 = 𝑗=1 𝑛 (ℎ𝑏𝑞𝑞𝑧 𝑗𝑜 𝑗𝑢ℎ find + sad in 𝑗𝑢ℎ 𝑔𝑗𝑜𝑒)
▪ ≤ 𝑛𝑚𝑝𝑚𝑝 𝑛 + 𝑗=1
𝑛 𝑡𝑏𝑒 𝑗𝑜 𝑗𝑢ℎ 𝑔𝑗𝑜𝑒
▪ = 𝑛𝑚𝑝𝑚𝑝 𝑛 + 𝑦∈𝑛 𝑜𝑣𝑛𝑐𝑓𝑠 𝑝𝑔 𝑗 𝑡. 𝑢. 𝑦 𝑗𝑡 𝑡𝑏𝑒 𝑗𝑜 𝑗𝑢ℎ 𝑔𝑗𝑜𝑒
▪ Let 𝑇𝑗,𝑦 = 1, 𝑗𝑔 𝑦 𝑏𝑞𝑞𝑓𝑏𝑠𝑡 𝑏𝑡 𝑏 𝑡𝑏𝑒 𝑤𝑓𝑠𝑢𝑓𝑦 𝑗𝑜 𝑗𝑢ℎ 𝑔𝑗𝑜𝑒 𝑝𝑞, 0 𝑓𝑚𝑡𝑓. ▪ Let 𝐼𝑗,𝑦 = 1, 𝑗𝑔 𝑦 𝑏𝑞𝑞𝑓𝑏𝑠𝑡 𝑏𝑡 𝑏 ℎ𝑏𝑞𝑞𝑧 𝑤𝑓𝑠𝑢𝑓𝑦 𝑗𝑜 𝑗𝑢ℎ 𝑔𝑗𝑜𝑒 𝑝𝑞, 0 𝑓𝑚𝑡𝑓.
▪ 𝑈𝑗𝑛𝑓𝑡 𝑔𝑝𝑠 𝑏𝑚𝑚 𝑔𝑗𝑜𝑒𝑡 = 𝑗,𝑦 𝑇𝑦,𝑗 + 𝑗,𝑦 𝐼𝑦,𝑗 ▪ = 𝑗 𝑦 𝐼𝑦,𝑗 + 𝑗 𝑦 𝑇𝑦,𝑗 ≤ 𝑛𝑚𝑝𝑚𝑝 𝑛 + 𝑦 𝑠𝑏𝑜𝑙(𝑦) ▪ And 𝑦 𝑠𝑏𝑜𝑙(𝑦) ≤ 𝑙 𝑙 × 𝑜𝑣𝑛 𝑝𝑔 𝑦 𝑡. 𝑢. 𝑠𝑏𝑜𝑙 𝑦 = 𝑙 = 𝑙 𝑙 × 𝑜/2𝑙 ≤ 2𝑜
▪ So, 𝑈𝑗𝑛𝑓𝑡 𝑔𝑝𝑠 𝑏𝑚𝑚 𝑔𝑗𝑜𝑒𝑡 ≤ 𝑛𝑚𝑝𝑚𝑝 𝑛 + 2𝑜
Amortized Analysis
Contents
1 2 3 4 5
Basic Idea Union By Rank Path Compression Analysis of Path Compression Further Analysis
▪ We have proved that the time complexity of m FIND() operations is: 𝑃 #ℎ𝑏𝑞𝑞𝑧 𝑤𝑓𝑠𝑢𝑓𝑦 𝑤𝑗𝑡𝑗𝑢𝑡 + 𝑃 #𝑡𝑏𝑒 𝑤𝑓𝑠𝑢𝑓𝑦 𝑤𝑗𝑡𝑗𝑢𝑡 = 𝑃 𝑛𝑚𝑝𝑚𝑝𝑜 + 𝑃 𝑜 ▪ Let’s have a try
Fu Further Analysis
16 Maybe we can improve that part. And don’t change this part.
▪ Note that 𝑃 #𝑡𝑏𝑒 𝑤𝑓𝑠𝑢𝑓𝑦 𝑤𝑗𝑡𝑗𝑢𝑡 =
𝑤
𝑠𝑏𝑜𝑙(𝑤) = 𝑜
𝑙 𝑚𝑝𝑜 𝑙
2𝑙 = 𝑃(𝑜) ▪ This 𝑙 means 𝑛𝑗𝑜 𝑠𝑏𝑜𝑙 𝑤′𝑡 𝑞𝑏𝑠𝑓𝑜𝑢 − 𝑠𝑏𝑜𝑙(𝑤) if we want 𝑤 to be happy ▪ Obs. 1. As long as it’s not expensive than exponential function, the result is still 𝑃(𝑜). ▪ Obs. 2. We change the definition of “happy”, we may improve the complexity (of the “happy part”)
A tiny work
17
▪ In the previous work, we said a vertex 𝑌 is happy if 𝑠𝑏𝑜𝑙(𝑌′𝑡 𝑞𝑏𝑠𝑓𝑜𝑢) > 𝑠𝑏𝑜𝑙(𝑌) ▪ If we make a more strict constrain, we can reduce the number of “happy vertices”. ▪ Let's replace 𝑠𝑏𝑜𝑙(𝑌) by 𝑠𝑏𝑜𝑙(𝑌)2.
A tiny work
18
▪ De
- Def. We say a vertex 𝑤 is second-order happy iff 𝑠𝑏𝑜𝑙 𝑤′𝑡 𝑞𝑏𝑠𝑓𝑜𝑢 >
𝑠𝑏𝑜𝑙(𝑤)2 ▪ Now, the longest chain of second-order happy vertices must have the following ranks: 0,1,2,4,16,256,65536, … ▪ The log of ranks are like this: 0,1,2,4,8,16, … ▪ We can say that the number of happy vertices is 𝑃 𝑚𝑝𝑚𝑝 𝑠𝑏𝑜𝑙 𝑠𝑝𝑝𝑢 = 𝑃(𝑚𝑝𝑚𝑝𝑚𝑝𝑜)
A tiny work
▪ Let’s look at the “sad part”: 𝑃 #𝑡𝑏𝑒 𝑤𝑓𝑠𝑢𝑓𝑦 𝑤𝑗𝑡𝑗𝑢𝑡 =
𝑤
𝑠𝑏𝑜𝑙(𝑤)2 − 𝑠𝑏𝑜𝑙(𝑤) <
𝑤
𝑠𝑏𝑜𝑙(𝑤)2 = 𝑜
𝑙=0 𝑚𝑝𝑜 𝑙2
2𝑙 ▪ Just use some simple math in your high school, we can show that
𝑙=0
𝑙2 2𝑙 < 6 ▪ Now the complexity of m FIND() operations is 𝑃 𝑛𝑚𝑝𝑚𝑝𝑚𝑝𝑜 + 𝑃(𝑜)
A tiny work
Can an we e do bet etter?
𝑙=0 𝑚𝑝𝑜 𝑙2
2𝑙 ▪ 𝑙2 is too smaller than 2𝑙, we still have much room for improving the complexity ▪ We say second-order happy is defined as 𝑠𝑏𝑜𝑙 𝑞𝑏𝑠𝑓𝑜𝑢 > 𝑠𝑏𝑜𝑙(𝑤)2 ▪ That is to say 𝑚𝑝𝑚𝑝 𝑠𝑏𝑜𝑙 𝑞𝑏𝑠𝑓𝑜𝑢 > 𝑚𝑝𝑚𝑝 𝑠𝑏𝑜𝑙 𝑤 + 1 ▪ It’s easy to define “k-th order” happy
Mo More power!
▪ De
- Def. We say a vertex 𝑤 is k-th order happy iff
𝑚𝑝 𝑙 𝑠𝑏𝑜𝑙 𝑤′𝑡 𝑞𝑏𝑠𝑓𝑜𝑢 > 𝑚𝑝(𝑙) 𝑠𝑏𝑜𝑙 𝑤 + 1 ▪ 𝑚𝑝 𝑙 𝑦 means k iterations of log: 𝑚𝑝(𝑙)𝑦 = 𝑚𝑝𝑚𝑝 … 𝑚𝑝𝑦 ▪ Thus the number of k-th order happy vertices is 𝑃 𝑚𝑝 𝑙 𝑠𝑏𝑜𝑙 𝑠𝑝𝑝𝑢 = 𝑃(𝑚𝑝 𝑙+1 𝑜) ▪ Note that we may face the problem that we must compute log(0), we can ignore those vertices with small ranks (the number of which is independent of
- f n, i.e. 𝑃(1)) when we consider the number of transferred vertices in FIND()
Mo More power!
𝑙 𝑢𝑗𝑛𝑓𝑡
▪ Now, if we want a vertex happy, the maximal transfer operations must be 22…2𝑚𝑝 𝑙 𝑠𝑏𝑜𝑙 𝑤 +1 − 𝑠𝑏𝑜𝑙(𝑤) ▪ We can define a function to replace the complex expression ▪ Def. 𝐺
𝑙 𝑦 = 2𝑦
(𝑙 = 1) 2𝐺𝑙−1(𝑚𝑝𝑦) (𝑙 > 1) ▪ So the “sad part” becomes
𝑤
𝐺
𝑙 𝑦 − 𝑠𝑏𝑜𝑙 𝑤 < 𝑜 𝑙=𝑙0 𝑚𝑝𝑜 𝐺 𝑙(𝑦)
2𝑙
Mo More power!
▪ If the series 𝑙=𝑙0
∞ 𝐺𝑙(𝑦) 2𝑙 converges, the partial sum must be bounded
▪ That case, we need lim
𝑜→∞
𝐺
𝑙(𝑜)
2𝑜 = 0
Mo More power!
▪ In fact, if lim
𝑦→+∞ 𝐺𝑙−1(𝑦) 2𝑦
= 0, i.e. lim
𝑦→+∞ 𝐺𝑙−1(𝑚𝑝𝑦) 𝑦
= 0, we can see lim
𝑦→+∞
𝐺
𝑙(𝑦)
2𝑦 = lim
𝑦→+∞
2𝐺
𝑙−1(𝑚𝑝𝑦)
2𝑦 = lim
𝑦→+∞2𝑦(𝐺𝑙−1 𝑚𝑝𝑦 𝑦 −1) = lim 𝑦→+∞2−𝑦 = 0
▪ Besides, when k=1, lim
𝑦→+∞ 𝐺𝑙(𝑦) 2𝑦
= 0 ▪ So, lim
𝑜→∞ 𝐺𝑙(𝑜) 2𝑜
= 0 is true, and we also have lim
𝑦→+∞ 𝐺𝑙(𝑚𝑝𝑦) 𝑦
= 0 ▪ Now we use Cauchy test lim
𝑜→∞
𝑜 𝐺
𝑙(𝑜)
2𝑜 = lim
𝑜→∞2𝐺𝑙−1 𝑚𝑝𝑜 /𝑜−1 = 1
2 < 1
We e nee eed some e math
▪ Now we have 𝑙=𝑙0
𝑚𝑝𝑜 𝐺𝑙(𝑦) 2𝑙 < 𝑙=𝑙0 ∞ 𝐺𝑙(𝑦) 2𝑙
= 𝐷𝑝𝑜𝑡𝑢𝑏𝑜𝑢 ▪ So for ∀𝑙 ∈ 𝑂 the complexity is 𝑃 𝑛𝑚𝑝 𝑙 𝑜 + 𝑃(𝑜)
Mo More power!
Can an we e do bet etter?
▪ In fact, the complexity is 𝑃 𝑛𝛽 𝑜 + 𝑃(𝑜), where 𝛽(𝑜) is the inverse function of Ackermann Function, which is smaller than 𝑚𝑝(𝑙)𝑜 for any k
Yes es, , we e can an
单击此处编辑母版标题样式