Skip to content

Latest commit

 

History

History
125 lines (108 loc) · 9.61 KB

0000-18-union-find-set.adoc

File metadata and controls

125 lines (108 loc) · 9.61 KB

Union Find Set 并查集

并查集算法,英文是 Union-Find,是解决动态连通性(Dynamic Conectivity)问题的一种算法。动态连通性是计算机图论中的一种数据结构,动态维护图结构中相连信息。简单的说就是,图中各个节点之间是否相连、如何将两个节点连接,连接后还剩多少个连通分量。

动态连通性其实可以抽象成给一幅图连线。假设用一个数组表示一堆节点,每个节点都是一个连通分量。初始化视图如下:

并查集初始化
Figure 1. 并查集初始化

并查集的一个重要操作是 union(a, b),就是将节点 a 和节点 b 建立连接。如图所示:

并查集合并
Figure 2. 并查集合并

union(a, b) 还可以将已经建立的两个“子网”进行连接:

并查集再合并
Figure 3. 并查集再合并
Tip
在合并时,是修改根节点的指针 parent[ap] = bp,而不是修改节点的指针 parent[a] = bp

并查集除了 union,还有一个重要操作是 connnected(a, b)。判断方法也很简单,从节点 ab 开始,向上查找,直到两个节点的根节点,判断两个根节点是否相等即可判断两个节点是否已经连接。为了加快这个判断速度,可以对其进行“路径压缩”,直白点说,就是将所有树的节点,都直接指向根节点,这样只需要一步即可到达根节点。路径压缩如图所示:

并查集路径压缩
Figure 4. 并查集路径压缩

简单代码实现如下:

link:{labdir}/UnionFind.java[role=include]

经典题目

  1. 128. Longest Consecutive Sequence

  2. 130. Surrounded Regions

  3. 200. Number of Islands

  4. 261. Graph Valid Tree

  5. 305. Number of Islands II

  6. 323. Number of Connected Components in an Undirected Graph

  7. 399. Evaluate Division

  8. 547. Number of Provinces

  9. 684. Redundant Connection

  10. 685. Redundant Connection II

  11. 694. Number of Distinct Islands

  12. 695. Max Area of Island

  13. 711. Number of Distinct Islands II

  14. 721. Accounts Merge

  15. 737. Sentence Similarity II

  16. 765. Couples Holding Hands

  17. 778. Swim in Rising Water

  18. 785. Is Graph Bipartite?

  19. 803. Bricks Falling When Hit

  20. 827. Making A Large Island

  21. 839. Similar String Groups

  22. 886. Possible Bipartition

  23. 924. Minimize Malware Spread

  24. 928. Minimize Malware Spread II

  25. 947. Most Stones Removed with Same Row or Column

  26. 952. Largest Component Size by Common Factor

  27. 959. Regions Cut By Slashes

  28. 990. Satisfiability of Equality Equations

  29. 1020. Number of Enclaves

  30. 1061. Lexicographically Smallest Equivalent String

  31. 1101. The Earliest Moment When Everyone Become Friends

  32. 1102. Path With Maximum Minimum Value

  33. 1135. Connecting Cities With Minimum Cost

  34. 1168. Optimize Water Distribution in a Village

  35. 1202. Smallest String With Swaps

  36. 1254. Number of Closed Islands

  37. 1258. Synonymous Sentences

  38. 1267. Count Servers that Communicate

  39. 1319. Number of Operations to Make Network Connected

  40. 1361. Validate Binary Tree Nodes

  41. 1391. Check if There is a Valid Path in a Grid

  42. 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree

  43. 1559. Detect Cycles in 2D Grid

  44. 1569. Number of Ways to Reorder Array to Get Same BST

  45. 1579. Remove Max Number of Edges to Keep Graph Fully Traversable

  46. 1584. Min Cost to Connect All Points

  47. 1627. Graph Connectivity With Threshold

  48. 1631. Path With Minimum Effort

  49. 1632. Rank Transform of a Matrix

  50. 1697. Checking Existence of Edge Length Limited Paths

  51. 1722. Minimize Hamming Distance After Swap Operations

  52. 1724. Checking Existence of Edge Length Limited Paths II

  53. 1905. Count Sub Islands

  54. 1970. Last Day Where You Can Still Cross

  55. 1971. Find if Path Exists in Graph

  56. 1998. GCD Sort of an Array

  57. 2003. Smallest Missing Genetic Value in Each Subtree

  58. 2076. Process Restricted Friend Requests

  59. 2092. Find All People With Secret

  60. 2157. Groups of Strings

  61. 2204. Distance to a Cycle in Undirected Graph

  62. 2307. Check for Contradictions in Equations

  63. 2316. Count Unreachable Pairs of Nodes in an Undirected Graph

  64. 2334. Subarray With Elements Greater Than Varying Threshold

  65. 2368. Reachable Nodes With Restrictions

  66. 2371. Minimize Maximum Value in a Grid

  67. 2382. Maximum Segment Sum After Removals

  68. 2421. Number of Good Paths

  69. 2424. Longest Uploaded Prefix

  70. 2492. Minimum Score of a Path Between Two Cities

  71. 2493. Divide Nodes Into the Maximum Number of Groups

  72. 2503. Maximum Number of Points From Grid Queries

  73. 2573. Find the String with LCP

  74. 2617. Minimum Number of Visited Cells in a Grid

  75. 2658. Maximum Number of Fish in a Grid

  76. 2685. Count the Number of Complete Components

  77. 2709. Greatest Common Divisor Traversal

  78. 2782. Number of Unique Categories

  79. 2812. Find the Safest Path in a Grid

  80. 2852. Sum of Remoteness of All Cells

  81. 2948. Make Lexicographically Smallest Array by Swapping Elements

  82. 3108. Minimum Cost Walk in Weighted Graph

  83. 3235. Check if the Rectangle Corner Is Reachable

  84. 3378. Count Connected Components in LCM Graph

  85. 3383. Minimum Runes to Add to Cast Spell

  86. 3493. Properties Graph