Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/main/scala/org/bearmug/algo/course01/week04/Karger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class Karger(m: Map[Int, List[Int]]) {
}

m match {
case e if e.isEmpty => Int.MaxValue
case _ => (0 until m.size).map { _ => calcCut(m, m.keys.toVector) } min
case _ => (0 until (m.size max 10)).map { _ => calcCut(m, m.keys.toVector) }.min
}
}
}
Expand All @@ -38,8 +37,12 @@ object Karger {
def apply(m: Map[Int, List[Int]]): Karger = new Karger(m)

def apply(fileName: String): Karger =
apply(Source.fromURL(getClass.getResource(fileName)).getLines().map{ _.split("\t").map(_.toInt).toList match {
case n :: rest => n -> rest
}}.toMap)
apply(Source.fromURL(getClass.getResource(fileName))
.getLines()
.map(_
.split("\t")
.map(_.toInt)
.toList)
.map(l => l.head -> l.tail).toMap)
}

Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ class InversionsSuite extends FunSuite {
assert(Inversions((1 to 100000 reverse).toVector).inversionsPar(4) == (100000 / 2) * (100000 - 1))
}

test("inversionsForce works for single element") {
assert(Inversions(Vector(1)).inversionsForce() == 0)
}

test("inversionsForce works for of non-inversed elements") {
assert(Inversions(Vector(1, 2, 3)).inversionsForce() == 0)
}

test("inversionsForce works for of three two inversed elements") {
assert(Inversions(Vector(3, 2, 1)).inversionsForce() == 3)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class KargerSuite extends FunSuite {
assert(Karger(Map[Int, List[Int]]()).minCut() == Int.MaxValue)
}

test("minCut works for one-node graph") {
assert(Karger(Map(1 -> List(2))).minCut() == Int.MaxValue)
}

test("minCut works for two-nodes graph") {
assert(Karger(Map(1 -> List(2), 2 -> List(1))).minCut() == 1)
}
Expand Down