Created
January 9, 2016 15:39
-
-
Save semenodm/e4f4ad4e132f71fa79ad to your computer and use it in GitHub Desktop.
Topological order by Corman
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
| package org.sdo.alg.graph | |
| case class TopoGraph(adjList: Map[String, List[String]]) { | |
| var visited: Set[String] = Set() | |
| var order: List[String] = Nil | |
| def topologicalOrder(): List[String] = { | |
| adjList foreach { | |
| case (vertex, _) => | |
| topologicalOrder(vertex) | |
| } | |
| order | |
| } | |
| def topologicalOrder(vertex: String): Unit = { | |
| if (!visited.contains(vertex)) { | |
| visited += vertex | |
| adjList.getOrElse(vertex, Nil).foreach { child => | |
| topologicalOrder(child) | |
| } | |
| order ::= vertex | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment