Last active
November 5, 2018 21:38
-
-
Save novakov-alexey-zz/6ec70409fad63c30fba73d306a406d70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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