Skip to content

Instantly share code, notes, and snippets.

@novakov-alexey-zz
Last active November 5, 2018 21:38
Show Gist options
  • Save novakov-alexey-zz/6ec70409fad63c30fba73d306a406d70 to your computer and use it in GitHub Desktop.
Save novakov-alexey-zz/6ec70409fad63c30fba73d306a406d70 to your computer and use it in GitHub Desktop.
/**
* Algorithm is based on Java version implemented
* by Princeton University https://algs4.cs.princeton.edu/44sp/
*/
final case class DirectedEdge(from: Int, to: Int, weight: Double)
final case class EdgeWeightedDigraph(adj: Map[Int, List[DirectedEdge]] = Map.empty)
object EdgeWeightedDigraphOps {
implicit class EdgeWeightedDigraphOps(g: EdgeWeightedDigraph) {
/**
* Adds directed edge 'e' to EdgeWeightedDigraph by creating
* a full-copy adjacency list, i.e. this operation
* leaves current graph 'g' immutable
*
* @param e - DirectedEdge to add into the graph 'g'
* @return returns new graph with edge 'e' added to it
*/
def addEdge(e: DirectedEdge): EdgeWeightedDigraph = {
val list = g.adj.getOrElse(e.from, List.empty)
val adj = g.adj + (e.from -> (list :+ e))
EdgeWeightedDigraph(adj)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment