Skip to content

Instantly share code, notes, and snippets.

@bmarcot
Last active December 20, 2015 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmarcot/6090596 to your computer and use it in GitHub Desktop.
Save bmarcot/6090596 to your computer and use it in GitHub Desktop.
Work in progress...
def tsort(gs: List[(Int, List[Int])]): List[Any] = {
if (gs.isEmpty) List()
else for {
g <- gs
if (g._2.isEmpty)
} yield g._1 :: tsort(gs.diff(List(g)).map(x => (x._1, x._2.diff(List(g._1)))))
} //> tsort: (gs: List[(Int, List[Int])])List[Any]
def tsort_aux(xs: List[Any]): List[List[Int]] = {
if (xs.isEmpty) List(List())
else
}
tsort(List((1, List(2, 3)), (2, List(3)), (3, List())))
//> res0: List[Any] = List(List(3, List(2, List(1))))
tsort(List((1, List(3, 5)), (2, List(3)), (3, List(4, 5)), (4, List()), (5, List())))
//> res1: List[Any] = List(List(4, List(5, List(3, List(1, List(2)), List(2, Lis
//| t(1))))), List(5, List(4, List(3, List(1, List(2)), List(2, List(1))))))
// -----------------------------
def get_outbounds(vs: List[(Int, List[Int])]): List[Int] = {
vs match {
case v :: vs => {
if (v._2.isEmpty) v._1 :: topologic(vs)
else topologic(vs)
}
case _ => Nil
}
}
def build_list(vs: List[(Int, List[Int])], xs: List[Int]): List[(Int, List[Int])] = {
case v :: vs => {
if (xs.exist(_ == v._1)) build_list(vs, xs)
else (v._1, v._2.filter(xs.exists(_ == x._1) == false)) :: build_list(vs, xs)
}
case _ => Nil
}
def topologic(vs: List[(Int, List[Int])]): List[Int] = {
vs match {
case Nil => Nil
case _ => get_outbounds(vs) ::: topologic(build_list(vs, get_outbounds(vs)))
}
}
/* g match {
case Nil => Nil
case _ => for {
(v, a) <- g
if (a.isEmpty)
} yield v :: topologic(g.filter(_._1 != v))
}*/
println(topologic(List((1, List()), (2, List(3)), (3, List(1, 4)), (4, List()))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment