Skip to content

Commit 28415fb

Browse files
committed
Merge branch 'set-cover'
2 parents 03ee825 + 396924b commit 28415fb

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

Set Cover (Unweighted)/README.markdown

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ For example, suppose you have a universe of `{1, 5, 7}` and you want to find the
1010
> {2}
1111
> {1, 2, 3}
1212
13-
You can see that the sets `{3, 1} {7, 6, 5, 4}` when unioned together will cover the universe of `{1, 5, 7}`. Yes, there may be additional elements in the sets returned by the algorithm, but every element in the universe is represented.
13+
You can see that the sets `{3, 1} {7, 6, 5, 4}` when unioned together will cover the universe of `{1, 5, 7}`. Yes, there may be additional elements in the sets returned by the algorithm, but every element in the universe is represented in the cover itself.
1414

15-
If there is no cover within the group of sets, the function returns nil. For example, if your universe is `{7, 9}`, there is no combination of sets within the grouping above that will yield a cover.
15+
There may be cases where no cover exists. For example, if your universe is `{7, 9}`, there is no combination of sets within the group above that will yield a cover.
1616

1717
## The algorithm
1818

19-
The Greedy Set Cover algorithm (unweighted) is provided here. It's known as greedy because it uses the largest intersecting set from the group of sets first before examining other sets in the group.
19+
The Greedy Set Cover algorithm (unweighted) is provided here. It's known as greedy because it uses the largest intersecting set from the group of sets first before examining other sets in the group. This is part of the reason why the cover may have additional elements which are not part of the universe.
2020

2121
The function (named `cover`) is provided as an extension of the Swift type `Set`. The function takes a single parameter, which is an array of sets. This array represents the group, and the set itself represents the universe.
2222

23+
One of the first things done in `cover` is to make a copy of the universe in `remainingSet`. Then, the algorithm enters a `while` loop in which a call to `largestIntersectingSet` is made. The value returned from `largestIntersectingSet` is the set which has the most elements in common with the remaining universe identified by `remainingSet`. If all sets have nothing in common, `largestIntersectingSet` returns `nil`.
24+
25+
If the result from `largestIntersectingSet` is not nil, that result is subtracted from `remainingSet` (reducing its size), and the loop continues until `remainingSet` has zero length (meaning a cover has been found) or until `largestIntersectingSet` returns `nil`.
26+
27+
If there is no cover within the group of sets, `cover` returns `nil`.
28+
2329
## See also
2430

2531
[Set cover problem on Wikipedia](https://en.wikipedia.org/wiki/Set_cover_problem)
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
let universe1 = Set(1...7)
22
let array1 = randomArrayOfSets(covering: universe1)
3-
let cover1 = universe1.cover(from: array1)
3+
let cover1 = universe1.cover(within: array1)
44

55
let universe2 = Set(1...10)
66
let array2: Array<Set<Int>> = [[1,2,3,4,5,6,7], [8,9]]
7-
let cover2 = universe2.cover(from: array2)
7+
let cover2 = universe2.cover(within: array2)
88

99
let universe3 = Set(["tall", "heavy"])
1010
let array3: Array<Set<String>> = [["tall", "light"], ["short", "heavy"], ["tall", "heavy", "young"]]
11-
let cover3 = universe3.cover(from: array3)
11+
let cover3 = universe3.cover(within: array3)
1212

1313
let universe4 = Set(["tall", "heavy", "green"])
14-
let cover4 = universe4.cover(from: array3)
14+
let cover4 = universe4.cover(within: array3)
1515

1616
let universe5: Set<Int> = [16, 32, 64]
1717
let array5: Array<Set<Int>> = [[16,17,18], [16,32,128], [1,2,3], [32,64,128]]
18-
let cover5 = universe5.cover(from: array5)
18+
let cover5 = universe5.cover(within: array5)
1919

2020
let universe6: Set<Int> = [24, 89, 132, 90, 22]
2121
let array6 = randomArrayOfSets(covering: universe6)
22-
let cover6 = universe6.cover(from: array6)
22+
let cover6 = universe6.cover(within: array6)
2323

2424
let universe7: Set<String> = ["fast", "cheap", "good"]
2525
let array7 = randomArrayOfSets(covering: universe7, minArraySizeFactor: 20.0, maxSetSizeFactor: 0.7)
26-
let cover7 = universe7.cover(from: array7)
26+
let cover7 = universe7.cover(within: array7)
2727

2828
let emptySet = Set<Int>()
29-
let coverTest1 = emptySet.cover(from: array1)
30-
let coverTest2 = universe1.cover(from: Array<Set<Int>>())
31-
let coverTest3 = emptySet.cover(from: Array<Set<Int>>())
29+
let coverTest1 = emptySet.cover(within: array1)
30+
let coverTest2 = universe1.cover(within: Array<Set<Int>>())
31+
let coverTest3 = emptySet.cover(within: Array<Set<Int>>())

Set Cover (Unweighted)/SetCover.playground/Sources/SetCover.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
public extension Set {
2-
func cover(from array: Array<Set<Element>>) -> Array<Set<Element>>? {
2+
func cover(within array: Array<Set<Element>>) -> Array<Set<Element>>? {
33
var result: [Set<Element>]? = [Set<Element>]()
44
var remainingSet = self
55

Set Cover (Unweighted)/SetCover.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extension Set {
2-
func cover(from array: Array<Set<Element>>) -> Array<Set<Element>>? {
2+
func cover(within array: Array<Set<Element>>) -> Array<Set<Element>>? {
33
var result: [Set<Element>]? = [Set<Element>]()
44
var remainingSet = self
55

0 commit comments

Comments
 (0)