Skip to content

Commit 12fac48

Browse files
committed
Fixed spacing, and fixes some English.
1 parent 625a97e commit 12fac48

6 files changed

+229
-229
lines changed

Union-Find/UnionFind.playground/Contents.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
var dsu = UnionFindQuickUnion<Int>()
44

55
for i in 1...10 {
6-
dsu.addSetWith(i)
6+
dsu.addSetWith(i)
77
}
88
// now our dsu contains 10 independent sets
99

1010
// let's divide our numbers into two sets by divisibility by 2
1111
for i in 3...10 {
12-
if i % 2 == 0 {
13-
dsu.unionSetsContaining(2, and: i)
14-
} else {
15-
dsu.unionSetsContaining(1, and: i)
16-
}
12+
if i % 2 == 0 {
13+
dsu.unionSetsContaining(2, and: i)
14+
} else {
15+
dsu.unionSetsContaining(1, and: i)
16+
}
1717
}
1818

1919
// check our division
@@ -42,12 +42,12 @@ dsuForStrings.addSetWith("b")
4242

4343
// In that example we divide strings by its first letter
4444
for word in words {
45-
dsuForStrings.addSetWith(word)
46-
if word.hasPrefix("a") {
47-
dsuForStrings.unionSetsContaining("a", and: word)
48-
} else if word.hasPrefix("b") {
49-
dsuForStrings.unionSetsContaining("b", and: word)
50-
}
45+
dsuForStrings.addSetWith(word)
46+
if word.hasPrefix("a") {
47+
dsuForStrings.unionSetsContaining("a", and: word)
48+
} else if word.hasPrefix("b") {
49+
dsuForStrings.unionSetsContaining("b", and: word)
50+
}
5151
}
5252

5353
print(dsuForStrings.inSameSet("a", and: "all"))

Union-Find/UnionFind.playground/Sources/UnionFindQuickFind.swift

+40-40
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,48 @@ import Foundation
33
/// Quick-find algorithm may take ~MN steps
44
/// to process M union commands on N objects
55
public struct UnionFindQuickFind<T: Hashable> {
6-
private var index = [T: Int]()
7-
private var parent = [Int]()
8-
private var size = [Int]()
9-
10-
public init() {}
11-
12-
public mutating func addSetWith(_ element: T) {
13-
index[element] = parent.count
14-
parent.append(parent.count)
15-
size.append(1)
6+
private var index = [T: Int]()
7+
private var parent = [Int]()
8+
private var size = [Int]()
9+
10+
public init() {}
11+
12+
public mutating func addSetWith(_ element: T) {
13+
index[element] = parent.count
14+
parent.append(parent.count)
15+
size.append(1)
16+
}
17+
18+
private mutating func setByIndex(_ index: Int) -> Int {
19+
return parent[index]
20+
}
21+
22+
public mutating func setOf(_ element: T) -> Int? {
23+
if let indexOfElement = index[element] {
24+
return setByIndex(indexOfElement)
25+
} else {
26+
return nil
1627
}
17-
18-
private mutating func setByIndex(_ index: Int) -> Int {
19-
return parent[index]
20-
}
21-
22-
public mutating func setOf(_ element: T) -> Int? {
23-
if let indexOfElement = index[element] {
24-
return setByIndex(indexOfElement)
25-
} else {
26-
return nil
27-
}
28-
}
29-
30-
public mutating func unionSetsContaining(_ firstElement: T, and secondElement: T) {
31-
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
32-
if firstSet != secondSet {
33-
for index in 0..<parent.count {
34-
if parent[index] == firstSet {
35-
parent[index] = secondSet
36-
}
37-
}
38-
size[secondSet] += size[firstSet]
39-
}
28+
}
29+
30+
public mutating func unionSetsContaining(_ firstElement: T, and secondElement: T) {
31+
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
32+
if firstSet != secondSet {
33+
for index in 0..<parent.count {
34+
if parent[index] == firstSet {
35+
parent[index] = secondSet
36+
}
4037
}
38+
size[secondSet] += size[firstSet]
39+
}
4140
}
42-
43-
public mutating func inSameSet(_ firstElement: T, and secondElement: T) -> Bool {
44-
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
45-
return firstSet == secondSet
46-
} else {
47-
return false
48-
}
41+
}
42+
43+
public mutating func inSameSet(_ firstElement: T, and secondElement: T) -> Bool {
44+
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
45+
return firstSet == secondSet
46+
} else {
47+
return false
4948
}
49+
}
5050
}

Union-Find/UnionFind.playground/Sources/UnionFindQuickUnion.swift

+41-41
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,49 @@ import Foundation
33
/// Quick-Union algorithm may take ~MN steps
44
/// to process M union commands on N objects
55
public struct UnionFindQuickUnion<T: Hashable> {
6-
private var index = [T: Int]()
7-
private var parent = [Int]()
8-
private var size = [Int]()
9-
10-
public init() {}
11-
12-
public mutating func addSetWith(_ element: T) {
13-
index[element] = parent.count
14-
parent.append(parent.count)
15-
size.append(1)
6+
private var index = [T: Int]()
7+
private var parent = [Int]()
8+
private var size = [Int]()
9+
10+
public init() {}
11+
12+
public mutating func addSetWith(_ element: T) {
13+
index[element] = parent.count
14+
parent.append(parent.count)
15+
size.append(1)
16+
}
17+
18+
private mutating func setByIndex(_ index: Int) -> Int {
19+
if parent[index] == index {
20+
return index
21+
} else {
22+
parent[index] = setByIndex(parent[index])
23+
return parent[index]
1624
}
17-
18-
private mutating func setByIndex(_ index: Int) -> Int {
19-
if parent[index] == index {
20-
return index
21-
} else {
22-
parent[index] = setByIndex(parent[index])
23-
return parent[index]
24-
}
25+
}
26+
27+
public mutating func setOf(_ element: T) -> Int? {
28+
if let indexOfElement = index[element] {
29+
return setByIndex(indexOfElement)
30+
} else {
31+
return nil
2532
}
26-
27-
public mutating func setOf(_ element: T) -> Int? {
28-
if let indexOfElement = index[element] {
29-
return setByIndex(indexOfElement)
30-
} else {
31-
return nil
32-
}
33+
}
34+
35+
public mutating func unionSetsContaining(_ firstElement: T, and secondElement: T) {
36+
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
37+
if firstSet != secondSet {
38+
parent[firstSet] = secondSet
39+
size[secondSet] += size[firstSet]
40+
}
3341
}
34-
35-
public mutating func unionSetsContaining(_ firstElement: T, and secondElement: T) {
36-
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
37-
if firstSet != secondSet {
38-
parent[firstSet] = secondSet
39-
size[secondSet] += size[firstSet]
40-
}
41-
}
42-
}
43-
44-
public mutating func inSameSet(_ firstElement: T, and secondElement: T) -> Bool {
45-
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
46-
return firstSet == secondSet
47-
} else {
48-
return false
49-
}
42+
}
43+
44+
public mutating func inSameSet(_ firstElement: T, and secondElement: T) -> Bool {
45+
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
46+
return firstSet == secondSet
47+
} else {
48+
return false
5049
}
50+
}
5151
}

Union-Find/UnionFind.playground/Sources/UnionFindWeightedQuickFind.swift

+47-47
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,57 @@ import Foundation
33
/// Quick-find algorithm may take ~MN steps
44
/// to process M union commands on N objects
55
public struct UnionFindWeightedQuickFind<T: Hashable> {
6-
private var index = [T: Int]()
7-
private var parent = [Int]()
8-
private var size = [Int]()
9-
10-
public init() {}
11-
12-
public mutating func addSetWith(_ element: T) {
13-
index[element] = parent.count
14-
parent.append(parent.count)
15-
size.append(1)
6+
private var index = [T: Int]()
7+
private var parent = [Int]()
8+
private var size = [Int]()
9+
10+
public init() {}
11+
12+
public mutating func addSetWith(_ element: T) {
13+
index[element] = parent.count
14+
parent.append(parent.count)
15+
size.append(1)
16+
}
17+
18+
private mutating func setByIndex(_ index: Int) -> Int {
19+
return parent[index]
20+
}
21+
22+
public mutating func setOf(_ element: T) -> Int? {
23+
if let indexOfElement = index[element] {
24+
return setByIndex(indexOfElement)
25+
} else {
26+
return nil
1627
}
17-
18-
private mutating func setByIndex(_ index: Int) -> Int {
19-
return parent[index]
20-
}
21-
22-
public mutating func setOf(_ element: T) -> Int? {
23-
if let indexOfElement = index[element] {
24-
return setByIndex(indexOfElement)
28+
}
29+
30+
public mutating func unionSetsContaining(_ firstElement: T, and secondElement: T) {
31+
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
32+
if firstSet != secondSet {
33+
if size[firstSet] < size[secondSet] {
34+
for index in 0..<parent.count {
35+
if parent[index] == firstSet {
36+
parent[index] = secondSet
37+
}
38+
}
39+
size[secondSet] += size[firstSet]
2540
} else {
26-
return nil
27-
}
28-
}
29-
30-
public mutating func unionSetsContaining(_ firstElement: T, and secondElement: T) {
31-
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
32-
if firstSet != secondSet {
33-
if size[firstSet] < size[secondSet] {
34-
for index in 0..<parent.count {
35-
if parent[index] == firstSet {
36-
parent[index] = secondSet
37-
}
38-
}
39-
size[secondSet] += size[firstSet]
40-
} else {
41-
for index in 0..<parent.count {
42-
if parent[index] == secondSet {
43-
parent[index] = firstSet
44-
}
45-
}
46-
size[firstSet] += size[secondSet]
47-
}
41+
for index in 0..<parent.count {
42+
if parent[index] == secondSet {
43+
parent[index] = firstSet
4844
}
45+
}
46+
size[firstSet] += size[secondSet]
4947
}
48+
}
5049
}
51-
52-
public mutating func inSameSet(_ firstElement: T, and secondElement: T) -> Bool {
53-
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
54-
return firstSet == secondSet
55-
} else {
56-
return false
57-
}
50+
}
51+
52+
public mutating func inSameSet(_ firstElement: T, and secondElement: T) -> Bool {
53+
if let firstSet = setOf(firstElement), let secondSet = setOf(secondElement) {
54+
return firstSet == secondSet
55+
} else {
56+
return false
5857
}
58+
}
5959
}

0 commit comments

Comments
 (0)