Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c2d2f77

Browse files
committed
Add BronKerbosch.maximalCliques iterator (unused)
1 parent 6f414cd commit c2d2f77

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎src/main/scala/eu/sim642/adventofcodelib/graph/BronKerbosch.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,35 @@ package eu.sim642.adventofcodelib.graph
22

33
object BronKerbosch {
44

5+
// unused
6+
def maximalCliques[A](neighbors: Map[A, Set[A]]): Iterator[Set[A]] = {
7+
8+
def bronKerbosch(r: Set[A], p: Set[A], x: Set[A]): Iterator[Set[A]] = {
9+
if (p.isEmpty) {
10+
if (x.isEmpty)
11+
Iterator.single(r)
12+
else
13+
Iterator.empty
14+
}
15+
else {
16+
//val u = p.headOption.getOrElse(x.head)
17+
val u = (p ++ x).maxBy(neighbors(_).size) // pivot on highest degree
18+
val vs = (p -- neighbors(u)).iterator
19+
Iterator.unfold((p, x))((p, x) => // foldLeftFlatMap
20+
vs.nextOption().map(v =>
21+
(bronKerbosch(r + v, p intersect neighbors(v), x intersect neighbors(v)), (p - v, x + v))
22+
)
23+
).flatten
24+
}
25+
}
26+
27+
bronKerbosch(Set.empty, neighbors.keySet, Set.empty)
28+
}
29+
30+
// TODO: would this be slower than direct implementation below?
31+
/*def maximumClique[A](neighbors: Map[A, Set[A]]): Set[A] =
32+
maximalCliques(neighbors).maxBy(_.size)*/
33+
534
// moved from 2018 day 23
635
def maximumClique[A](neighbors: Map[A, Set[A]]): Set[A] = {
736
var best: Set[A] = Set.empty

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /