Skip to content

Commit 6f414cd

Browse files
committed
Extract BronKerbosch to library
1 parent fe45dcf commit 6f414cd

File tree

3 files changed

+36
-56
lines changed

3 files changed

+36
-56
lines changed

src/main/scala/eu/sim642/adventofcode2018/Day23.scala

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import eu.sim642.adventofcodelib.box.{Box3, Box4}
44
import eu.sim642.adventofcodelib.pos.Pos3
55
import eu.sim642.adventofcodelib.pos.Pos4
66
import eu.sim642.adventofcodelib.IntegralImplicits.*
7+
import eu.sim642.adventofcodelib.graph.BronKerbosch
78

89
import scala.collection.mutable
910
import scala.util.boundary
@@ -40,35 +41,9 @@ object Day23 {
4041
}
4142

4243
object NaiveCliquePart2Solution extends Part2Solution {
43-
def maximumClique(neighbors: Map[Nanobot, Set[Nanobot]]): Set[Nanobot] = {
44-
var best: Set[Nanobot] = Set.empty
45-
46-
def bronKerbosh(r: Set[Nanobot], p: Set[Nanobot], x: Set[Nanobot]): Unit = {
47-
if (p.isEmpty && x.isEmpty) {
48-
//println(r)
49-
if (r.size > best.size)
50-
best = r
51-
}
52-
else {
53-
//val u = p.headOption.getOrElse(x.head)
54-
val u = (p ++ x).maxBy(neighbors(_).size) // pivot on highest degree
55-
var p2 = p
56-
var x2 = x
57-
for (v <- p -- neighbors(u)) {
58-
bronKerbosh(r + v, p2 intersect neighbors(v), x2 intersect neighbors(v))
59-
p2 -= v
60-
x2 += v
61-
}
62-
}
63-
}
64-
65-
bronKerbosh(Set.empty, neighbors.keySet, Set.empty)
66-
best
67-
}
68-
6944
def maximumOverlap(nanobots: Seq[Nanobot]): Set[Nanobot] = {
7045
val neighbors: Map[Nanobot, Set[Nanobot]] = nanobots.map(nanobot1 => nanobot1 -> nanobots.filter(nanobot2 => nanobot2 != nanobot1 && nanobot1.overlaps(nanobot2)).toSet).toMap
71-
maximumClique(neighbors)
46+
BronKerbosch.maximumClique(neighbors)
7247
}
7348

7449
def closestMostNanobots(nanobots: Seq[Nanobot]): Int = {

src/main/scala/eu/sim642/adventofcode2024/Day23.scala

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package eu.sim642.adventofcode2024
22

3+
import eu.sim642.adventofcodelib.graph.BronKerbosch
4+
35
object Day23 {
46

57
type Computer = String
@@ -16,37 +18,9 @@ object Day23 {
1618

1719
def count3CliquesT(edges: Set[Edge]): Int = find3Cliques(edges).count(_.exists(_.startsWith("t")))
1820

19-
// copied from 2018 day 23
20-
// TODO: move to library
21-
def maximumClique(neighbors: Map[Computer, Set[Computer]]): Set[Computer] = {
22-
var best: Set[Computer] = Set.empty
23-
24-
def bronKerbosh(r: Set[Computer], p: Set[Computer], x: Set[Computer]): Unit = {
25-
if (p.isEmpty && x.isEmpty) {
26-
//println(r)
27-
if (r.size > best.size)
28-
best = r
29-
}
30-
else {
31-
//val u = p.headOption.getOrElse(x.head)
32-
val u = (p ++ x).maxBy(neighbors(_).size) // pivot on highest degree
33-
var p2 = p
34-
var x2 = x
35-
for (v <- p -- neighbors(u)) {
36-
bronKerbosh(r + v, p2 intersect neighbors(v), x2 intersect neighbors(v))
37-
p2 -= v
38-
x2 += v
39-
}
40-
}
41-
}
42-
43-
bronKerbosh(Set.empty, neighbors.keySet, Set.empty)
44-
best
45-
}
46-
4721
def maximumClique(edges: Set[Edge]): Set[Computer] = {
4822
val neighbors = (edges ++ edges.map(_.swap)).groupMap(_._1)(_._2)
49-
maximumClique(neighbors)
23+
BronKerbosch.maximumClique(neighbors)
5024
}
5125

5226
def lanPartyPassword(edges: Set[Edge]): String = {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package eu.sim642.adventofcodelib.graph
2+
3+
object BronKerbosch {
4+
5+
// moved from 2018 day 23
6+
def maximumClique[A](neighbors: Map[A, Set[A]]): Set[A] = {
7+
var best: Set[A] = Set.empty
8+
9+
def bronKerbosch(r: Set[A], p: Set[A], x: Set[A]): Unit = {
10+
if (p.isEmpty && x.isEmpty) {
11+
//println(r)
12+
if (r.size > best.size)
13+
best = r
14+
}
15+
else {
16+
//val u = p.headOption.getOrElse(x.head)
17+
val u = (p ++ x).maxBy(neighbors(_).size) // pivot on highest degree
18+
var p2 = p
19+
var x2 = x
20+
for (v <- p -- neighbors(u)) {
21+
bronKerbosch(r + v, p2 intersect neighbors(v), x2 intersect neighbors(v))
22+
p2 -= v
23+
x2 += v
24+
}
25+
}
26+
}
27+
28+
bronKerbosch(Set.empty, neighbors.keySet, Set.empty)
29+
best
30+
}
31+
}

0 commit comments

Comments
 (0)