Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
29
Task/Dijkstras-algorithm/Scala/dijkstras-algorithm-1.scala
Normal file
29
Task/Dijkstras-algorithm/Scala/dijkstras-algorithm-1.scala
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
object Dijkstra {
|
||||
|
||||
type Path[Key] = (Double, List[Key])
|
||||
|
||||
def Dijkstra[Key](lookup: Map[Key, List[(Double, Key)]], fringe: List[Path[Key]], dest: Key, visited: Set[Key]): Path[Key] = fringe match {
|
||||
case (dist, path) :: fringe_rest => path match {case key :: path_rest =>
|
||||
if (key == dest) (dist, path.reverse)
|
||||
else {
|
||||
val paths = lookup(key).flatMap {case (d, key) => if (!visited.contains(key)) List((dist + d, key :: path)) else Nil}
|
||||
val sorted_fringe = (paths ++ fringe_rest).sortWith {case ((d1, _), (d2, _)) => d1 < d2}
|
||||
Dijkstra(lookup, sorted_fringe, dest, visited + key)
|
||||
}
|
||||
}
|
||||
case Nil => (0, List())
|
||||
}
|
||||
|
||||
def main(x: Array[String]): Unit = {
|
||||
val lookup = Map(
|
||||
"a" -> List((7.0, "b"), (9.0, "c"), (14.0, "f")),
|
||||
"b" -> List((10.0, "c"), (15.0, "d")),
|
||||
"c" -> List((11.0, "d"), (2.0, "f")),
|
||||
"d" -> List((6.0, "e")),
|
||||
"e" -> List((9.0, "f")),
|
||||
"f" -> Nil
|
||||
)
|
||||
val res = Dijkstra[String](lookup, List((0, List("a"))), "e", Set())
|
||||
println(res)
|
||||
}
|
||||
}
|
||||
57
Task/Dijkstras-algorithm/Scala/dijkstras-algorithm-2.scala
Normal file
57
Task/Dijkstras-algorithm/Scala/dijkstras-algorithm-2.scala
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import scala.collection.mutable
|
||||
|
||||
class Dijkstra[Key] {
|
||||
|
||||
type PathInfo = (Double, List[Key])
|
||||
type Path = List[Key]
|
||||
type MinHeap[PathInfo] = mutable.PriorityQueue[PathInfo]
|
||||
|
||||
final def dijkstra(weightedGraph: Map[Key, List[(Double, Key)]],
|
||||
start: Key,
|
||||
dest: Key)(implicit ord: Ordering[PathInfo]): PathInfo =
|
||||
dijkstraHelper(weightedGraph, mutable.PriorityQueue((0.0, List(start))), dest)
|
||||
|
||||
@annotation.tailrec
|
||||
private final def dijkstraHelper(weightedGraph: Map[Key, List[(Double, Key)]],
|
||||
fringe: MinHeap[PathInfo],
|
||||
dest: Key,
|
||||
visited: Set[Key] = Set.empty[Key])(implicit ord: Ordering[PathInfo]): PathInfo = {
|
||||
|
||||
def updateFringe(frng: MinHeap[PathInfo], currentDist: Double, currentPath: Path): MinHeap[PathInfo] =
|
||||
(currentPath : @unchecked) match {
|
||||
case keys @ key :: _ =>
|
||||
weightedGraph(key)
|
||||
.withFilter { case (_, k) => !visited.contains(k) }
|
||||
.map { case (d, k) => (currentDist + d, k :: keys) } // updated PathInfo's
|
||||
.foreach { p => frng.enqueue(p) }
|
||||
|
||||
frng
|
||||
}
|
||||
|
||||
if (fringe.isEmpty)
|
||||
(0, Nil)
|
||||
else {
|
||||
(fringe.dequeue() : @unchecked) match {
|
||||
case (dist, path @ `dest` :: _) =>
|
||||
(dist, path.reverse)
|
||||
|
||||
case (dist, path @ key :: _) =>
|
||||
dijkstraHelper(weightedGraph, updateFringe(fringe, dist, path), dest, visited + key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def main(x: Array[String]): Unit = {
|
||||
val weightedGraph = Map(
|
||||
"a" -> List((7.0, "b"), (9.0, "c"), (14.0, "f")),
|
||||
"b" -> List((10.0, "c"), (15.0, "d")),
|
||||
"c" -> List((11.0, "d"), (2.0, "f")),
|
||||
"d" -> List((6.0, "e")),
|
||||
"e" -> List((9.0, "f")),
|
||||
"f" -> Nil
|
||||
)
|
||||
|
||||
val res = dijkstra[String](weightedGraph, "a", "e")
|
||||
println(res)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue