You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Union-Find/README.markdown
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ What do we mean by this? For example, the Union-Find data structure could be kee
9
9
[ g, d, c ]
10
10
[ i, j ]
11
11
12
-
These sets are disjoint because they have no members in common.
12
+
These sets are disjoint because they have no members in common.
13
13
14
14
Union-Find supports three basic operations:
15
15
@@ -41,9 +41,9 @@ Example: If `parent` looks like this,
41
41
42
42
parent [ 1, 1, 1, 0, 2, 0, 6, 6, 6 ]
43
43
i 0 1 2 3 4 5 6 7 8
44
-
44
+
45
45
then the tree structure looks like:
46
-
46
+
47
47
1 6
48
48
/ \ / \
49
49
0 2 7 8
@@ -63,7 +63,7 @@ Note that the `parent[]` of a root node points to itself. So `parent[1] = 1` and
63
63
Let's look at the implementation of these basic operations, starting with adding a new set.
64
64
65
65
```swift
66
-
publicmutatingfuncaddSetWith(element: T) {
66
+
publicmutatingfuncaddSetWith(_element: T) {
67
67
index[element] = parent.count// 1
68
68
parent.append(parent.count) // 2
69
69
size.append(1) // 3
@@ -83,7 +83,7 @@ When you add a new element, this actually adds a new subset containing just that
83
83
Often we want to determine whether we already have a set that contains a given element. That's what the **Find** operation does. In our `UnionFind` data structure it is called `setOf()`:
@@ -109,7 +109,7 @@ Because we're dealing with a tree structure, this is a recursive method.
109
109
110
110
Recall that each set is represented by a tree and that the index of the root node serves as the number that identifies the set. We're going to find the root node of the tree that the element we're searching for belongs to, and return its index.
111
111
112
-
1. First, we check if the given index represents a root node (i.e. a node whose `parent` points back to the node itself). If so, we're done.
112
+
1. First, we check if the given index represents a root node (i.e. a node whose `parent` points back to the node itself). If so, we're done.
113
113
114
114
2. Otherwise we recursively call this method on the parent of the current node. And then we do a **very important thing**: we overwrite the parent of the current node with the index of root node, in effect reconnecting the node directly to the root of the tree. The next time we call this method, it will execute faster because the path to the root of the tree is now much shorter. Without that optimization, this method's complexity is **O(n)** but now in combination with the size optimization (covered in the Union section) it is almost **O(1)**.
115
115
@@ -130,8 +130,8 @@ Now if we need to call `setOf(4)` again, we no longer have to go through node `2
130
130
There is also a helper method to check that two elements are in the same set:
0 commit comments