All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.barrybecker4.discreteoptimization.shortestpaths.solver.DijkstrasPathSolver.scala Maven / Gradle / Ivy

The newest version!
package com.barrybecker4.discreteoptimization.shortestpaths.solver

import com.barrybecker4.common.format.FormatUtil
import com.barrybecker4.discreteoptimization.common.graph.algorithms.shortestpaths.DijkstrasAlgorithm
import com.barrybecker4.discreteoptimization.common.graph.{Graph, Path}
import com.barrybecker4.discreteoptimization.common.graph.directed.DirectedGraph
import com.barrybecker4.discreteoptimization.shortestpaths.model.ShortestPathsSolution

import scala.util.Random

object DijkstrasPathSolver {
  val BASE_NAME = "dijkstra"
}

/**
 */
class DijkstrasPathSolver extends ShortestPathsSolver {

  var graph: DirectedGraph = _

  /**
   * Find k shortest paths from source
   * For now, just print the shortest path k times
   */
  def findPaths(graph: DirectedGraph, sourceVertex: Int): ShortestPathsSolution = {

    val pathCalc = new DijkstrasAlgorithm(graph).findShortestPaths(0)

    val paths = Range(0, graph.numVertices).map(pathCalc.pathToVertex).toList
    val totalCost = paths.map(_.weight).sum
    val solution = ShortestPathsSolution(totalCost, paths)

    solution
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy