algorithm 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


  1. Algorithm Presentation ▪ Group: Tarjan

  2. Union Find Algorithm Group: Tarjan Dio, Zack, Chips 2016. 12

  3. Contents 1 Basic Idea 2 Union By Rank 3 Path Compression 4 Analysis of Path Compression 5 Further Analysis

  4. Contents 1 Basic Idea 2 Union By Rank 3 Path Compression 4 Analysis of Path Compression Further Analysis 5

  5. Bas asic idea ea ▪ 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 A A C C 3 3 1, 2, 1, 2, 4 4 Union (5, 6) => D B B 5, 6, 7, 5, 7, 6, 8 8, 9 9

  6. Contents 1 Basic Idea 2 Union By Rank 3 Path Compression 4 Analysis of Path Compression Further Analysis 5

  7. Rep epresent as as a a tree ee ▪ 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 𝑙

  8. Union by rank ank Union (5, 6) => Union (4, 5) =>

  9. Contents 1 Basic Idea 2 Union By Rank 3 Path Compression 4 Analysis of Path Compression Further Analysis 5

  10. Path Compression ▪ After find, change the parent pointers of all vertices along the path directly 3 3 to v. 3 3 ▪ Example: 0 2 2 5 5 0 1 0 6 𝐺𝑗𝑜𝑒(8) → 1 6 0 0 8 8 0 ▪ Observation: 𝑠𝑏𝑜𝑙 𝑦 ≠ ℎ𝑓𝑗𝑕ℎ𝑢 𝑦 𝑗𝑡 𝑞𝑝𝑡𝑡𝑗𝑐𝑚𝒇 ▪ Corollary 1, 2 still hold Corollary 1: 𝑠𝑏𝑜𝑙 𝑦 ≤ log 𝑛 Corollary 2: 𝑜𝑣𝑛𝑐𝑓𝑠 𝑝𝑔 𝑓𝑚𝑓𝑛𝑓𝑜𝑢 𝑠𝑏𝑜𝑙𝑓𝑒 𝑙 ≤ 𝑜/2 𝑙

  11. Contents 1 Basic Idea 2 Union By Rank 3 Path Compression 4 Analysis of Path Compression Further Analysis 5

  12. Analysis of Path Compression ▪ Define 𝑕𝑏𝑞 (𝑦) = 𝑠𝑏𝑜𝑙 (𝑞𝑏𝑠𝑓𝑜𝑢) – 𝑠𝑏𝑜𝑙(𝑦) . (In the picture, blue number is rank, 19 red number is gap). v ▪ A vertex is happy if 𝑕𝑏𝑞(𝑦) > 𝑠𝑏𝑜𝑙(𝑦). 8 (i.e. rank (parent) > 2rank (x)) and sad otherwise. 11 u Sad ▪ How many vertices can be happy in the path from x to root? 5 6 ▪ Assume x 1 , x 2 , x 3 , …, x k is the sequence of happy vertices z Happy on the path. 𝑠𝑏𝑜𝑙 𝑦 𝑗 ≥ 2 𝑗 − 1 2 3 𝑀𝑝𝑕(𝑛) ≥ 𝑠𝑏𝑜𝑙(𝑠𝑝𝑝𝑢) >= 2 𝑙 − 1 y Sad 3 𝑙 ≤ 𝑚𝑝𝑕𝑚𝑝𝑕(𝑛); 0 x Happy

  13. Analysis of Path Compression ▪ Observation: n: every time x is part of a find operation, its gap increase by 1. n 1: along path, ℎ𝑏𝑞𝑞𝑧 𝑤𝑓𝑠𝑢𝑗𝑑𝑓𝑡 ≤ 𝑚𝑝𝑕𝑚𝑝𝑕 𝑛 ▪ Observation ▪ Observation n 2: A sad vertex will be happy after rank(x) find ops. 51 51 root 1 root 50 30 y 20 49 x 1 𝐺𝑗𝑜𝑒(𝑣) → 21 50 2 28 30 y u 2 x u

  14. Amortized Analysis ▪ Suppose there are m find operations. 𝑛 𝑢𝑗𝑛𝑓 𝑔𝑝𝑠 𝑗 𝑢ℎ 𝑔𝑗𝑜𝑒 𝑝𝑞 = 𝑗=1 𝑛 (ℎ𝑏𝑞𝑞𝑧 𝑗𝑜 𝑗 𝑢ℎ find + sad in 𝑗 𝑢ℎ 𝑔𝑗𝑜𝑒) ▪ 𝑗=1 𝑛 𝑡𝑏𝑒 𝑗𝑜 𝑗 𝑢ℎ 𝑔𝑗𝑜𝑒 ▪ ≤ 𝑛𝑚𝑝𝑕𝑚𝑝𝑕 𝑛 + 𝑗=1 ▪ = 𝑛𝑚𝑝𝑕𝑚𝑝𝑕 𝑛 + 𝑦∈𝑛 𝑜𝑣𝑛𝑐𝑓𝑠 𝑝𝑔 𝑗 𝑡. 𝑢. 𝑦 𝑗𝑡 𝑡𝑏𝑒 𝑗𝑜 𝑗 𝑢ℎ 𝑔𝑗𝑜𝑒 ▪ Let 𝑇 𝑗,𝑦 = 1, 𝑗𝑔 𝑦 𝑏𝑞𝑞𝑓𝑏𝑠𝑡 𝑏𝑡 𝑏 𝑡𝑏𝑒 𝑤𝑓𝑠𝑢𝑓𝑦 𝑗𝑜 𝑗 𝑢ℎ 𝑔𝑗𝑜𝑒 𝑝𝑞, 0 𝑓𝑚𝑡𝑓. ▪ Let 𝐼 𝑗,𝑦 = 1, 𝑗𝑔 𝑦 𝑏𝑞𝑞𝑓𝑏𝑠𝑡 𝑏𝑡 𝑏 ℎ𝑏𝑞𝑞𝑧 𝑤𝑓𝑠𝑢𝑓𝑦 𝑗𝑜 𝑗 𝑢ℎ 𝑔𝑗𝑜𝑒 𝑝𝑞, 0 𝑓𝑚𝑡𝑓. ▪ 𝑈𝑗𝑛𝑓𝑡 𝑔𝑝𝑠 𝑏𝑚𝑚 𝑔𝑗𝑜𝑒𝑡 = 𝑗,𝑦 𝑇 𝑦,𝑗 + 𝑗,𝑦 𝐼 𝑦,𝑗 ▪ = 𝑗 𝑦 𝐼 𝑦,𝑗 + 𝑗 𝑦 𝑇 𝑦,𝑗 ≤ 𝑛𝑚𝑝𝑕𝑚𝑝𝑕 𝑛 + 𝑦 𝑠𝑏𝑜𝑙(𝑦) ▪ And 𝑦 𝑠𝑏𝑜𝑙(𝑦) ≤ 𝑙 𝑙 × 𝑜𝑣𝑛 𝑝𝑔 𝑦 𝑡. 𝑢. 𝑠𝑏𝑜𝑙 𝑦 = 𝑙 = 𝑙 𝑙 × 𝑜/2 𝑙 ≤ 2𝑜 ▪ So, 𝑈𝑗𝑛𝑓𝑡 𝑔𝑝𝑠 𝑏𝑚𝑚 𝑔𝑗𝑜𝑒𝑡 ≤ 𝑛𝑚𝑝𝑕𝑚𝑝𝑕 𝑛 + 2𝑜

  15. Contents 1 Basic Idea 2 Union By Rank 3 Path Compression 4 Analysis of Path Compression Further Analysis 5

  16. Fu Further Analysis ▪ We have proved that the time complexity of m F IND () operations is: 𝑃 #ℎ𝑏𝑞𝑞𝑧 𝑤𝑓𝑠𝑢𝑓𝑦 𝑤𝑗𝑡𝑗𝑢𝑡 + 𝑃 #𝑡𝑏𝑒 𝑤𝑓𝑠𝑢𝑓𝑦 𝑤𝑗𝑡𝑗𝑢𝑡 = 𝑃 𝑛𝑚𝑝𝑕𝑚𝑝𝑕𝑜 + 𝑃 𝑜 Maybe we can improve that part. And don’t change this part. ▪ Let’s have a try 16

  17. A tiny work ▪ 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”) 17

  18. A tiny work ▪ 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 . 18

  19. A tiny work Def. We say a vertex 𝑤 is second-order happy iff 𝑠𝑏𝑜𝑙 𝑤 ′ 𝑡 𝑞𝑏𝑠𝑓𝑜𝑢 > ▪ De 𝑠𝑏𝑜𝑙(𝑤) 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 𝑃 𝑚𝑝𝑕𝑚𝑝𝑕 𝑠𝑏𝑜𝑙 𝑠𝑝𝑝𝑢 = 𝑃(𝑚𝑝𝑕𝑚𝑝𝑕𝑚𝑝𝑕𝑜)

  20. A tiny work ▪ Let’s look at the “sad part”: 𝑠𝑏𝑜𝑙(𝑤) 2 − 𝑠𝑏𝑜𝑙(𝑤) 𝑃 #𝑡𝑏𝑒 𝑤𝑓𝑠𝑢𝑓𝑦 𝑤𝑗𝑡𝑗𝑢𝑡 = 𝑤 𝑚𝑝𝑕𝑜 𝑙 2 𝑠𝑏𝑜𝑙(𝑤) 2 = 𝑜 < 2 𝑙 𝑤 𝑙=0 ▪ Just use some simple math in your high school, we can show that 𝑙 2 2 𝑙 < 6 𝑙=0 ▪ Now the complexity of m F IND () operations is 𝑃 𝑛𝑚𝑝𝑕𝑚𝑝𝑕𝑚𝑝𝑕𝑜 + 𝑃(𝑜)

  21. Can an we e do bet etter?

  22. More power! Mo 𝑚𝑝𝑕𝑜 𝑙 2 2 𝑙 𝑙=0 ▪ 𝑙 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

  23. More power! Mo ▪ 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 of n, i.e. 𝑃(1) ) when we consider the number of transferred vertices in F IND ()

  24. More power! Mo ▪ Now, if we want a vertex happy, the maximal transfer operations must be 2 2 …2𝑚𝑝𝑕 𝑙 𝑠𝑏𝑜𝑙 𝑤 +1 − 𝑠𝑏𝑜𝑙(𝑤) ▪ We can define a function to replace the complex expression 𝑙 𝑦 = 2𝑦 (𝑙 = 1) ▪ Def. 𝐺 2 𝐺 𝑙−1 (𝑚𝑝𝑕𝑦) (𝑙 > 1) ▪ So the “sad part” becomes 𝑚𝑝𝑕𝑜 𝐺 𝑙 (𝑦) 𝐺 𝑙 𝑦 − 𝑠𝑏𝑜𝑙 𝑤 < 𝑜 2 𝑙 𝑤 𝑙=𝑙 0

  25. More power! Mo 𝐺 𝑙 (𝑦) ∞ ▪ If the series 𝑙=𝑙 0 2 𝑙 converges, the partial sum must be bounded ▪ That case, we need 𝐺 𝑙 (𝑜) lim = 0 2 𝑜 𝑜→∞

Recommend


More recommend