# HG changeset patch # User Christian Urban # Date 1478092960 0 # Node ID f31c22f4f1047e2b0965d1ad3ce777d83b7e541b # Parent 575a5c07c7fee51c90e30016b79f63358fd6780b updated diff -r 575a5c07c7fe -r f31c22f4f104 progs/knight1.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/knight1.scala Wed Nov 02 13:22:40 2016 +0000 @@ -0,0 +1,76 @@ +import scala.util._ + +class Computation[A,B](value: A, function: A => B) { + lazy val result = function(value) +} + + +def print_board(n: Int)(steps: List[(Int, Int)]): Unit = { + println + for (i <- 0 until n) { + for (j <- 0 until n) { + print(f"${steps.indexOf((i, j))}%3.0f ") + } + println + } +} + +def add_pair(x: (Int, Int))(y: (Int, Int)) = + (x._1 + y._1, x._2 + y._2) + +def is_legal(n: Int)(x: (Int, Int)) = + 0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n + +def moves(n: Int)(steps: List[(Int, Int)])(x: (Int, Int)): List[(Int, Int)] = { + List((1, 2),(2, 1),(2, -1),(1, -2), + (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)).filterNot(steps.contains(_)) +} + +def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = + moves(n)(steps)(x).sortBy(moves(n)(steps)(_).length) + +moves(8)(Nil)(1,3) +ordered_moves(8)(Nil)(1,3) +ordered_moves(8)(List((2, 4), (2, 6)))(1,3) + +def first[A, B](xs: List[A], f: A => Set[B]): Set[B] = xs match { + case Nil => Set() + case x::xs => { + val result = f(x) + if (result == Set()) first(xs, f) else result + } +} + +// non-circular tour +def tour(n: Int)(steps: List[(Int, Int)]): Option[List[(Int, Int)]] = { + if (steps.length == n * n) Some(steps) + else + { val list = moves(n)(steps)(steps.head) map (x => new Computation(x, ((x:(Int, Int)) => tour(n)(x::steps)))) + val found = list.par find (_.result.isDefined) + found map (_.result.get) + } +} + +val n = 6 +println(s"simple tour: n = $n") + +val starts = for (i <- (0 until n).toList; + j <- (0 until n).toList) yield new Computation ((i, j), ((x:(Int, Int)) => tour(n)(x::Nil))) + +val found = starts.par find (_.result.isDefined) +print_board(n)((found map (_.result.get)).get) + +//for measuring time +def time_needed[T](i: Int, code: => T) = { + val start = System.nanoTime() + for (j <- 1 to i) code + val end = System.nanoTime() + (end - start)/(i * 1.0e9) +} + +//for (i <- 1 to 20) { +// println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) +//} + + + diff -r 575a5c07c7fe -r f31c22f4f104 progs/knight2.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/knight2.scala Wed Nov 02 13:22:40 2016 +0000 @@ -0,0 +1,43 @@ +import scala.util._ + +def print_board(n: Int)(steps: List[(Int, Int)]): Unit = { + for (i <- 0 until n) { + for (j <- 0 until n) { + print(f"${steps.indexOf((i, j))}%3.0f ") + } + println + } + //readLine() + System.exit(0) +} + +def add_pair(x: (Int, Int))(y: (Int, Int)) = + (x._1 + y._1, x._2 + y._2) + +def is_legal(n: Int)(x: (Int, Int)) = + 0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n + +def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = { + List((1, 2),(2, 1),(2, -1),(1, -2), + (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)) +} + +def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = + moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length) + +moves(8)(1,3) +ordered_moves(8)(Nil)(1,3) +ordered_moves(8)(List((2, 4), (2, 6)))(1,3) + +// non-circle tour parallel +def tour(n: Int)(steps: List[(Int, Int)]): Unit = { + if (steps.length == n * n) + print_board(n)(steps) + else + for (x <- moves(n)(steps.head); if (!steps.contains(x))) tour(n)(x :: steps) +} + +val n = 8 +println(s"simple tour: n = $n") + +for (i <- 0 until n; j <- 0 until n) tour(n)(List((i, j))) diff -r 575a5c07c7fe -r f31c22f4f104 progs/knight3.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/knight3.scala Wed Nov 02 13:22:40 2016 +0000 @@ -0,0 +1,45 @@ +import scala.util._ + +def print_board(n: Int)(steps: List[(Int, Int)]): Unit = { + for (i <- 0 until n) { + for (j <- 0 until n) { + print(f"${steps.indexOf((i, j))}%3.0f ") + } + println + } + //readLine() + System.exit(0) +} + +def add_pair(x: (Int, Int))(y: (Int, Int)) = + (x._1 + y._1, x._2 + y._2) + +def is_legal(n: Int)(x: (Int, Int)) = + 0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n + +def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = { + List((1, 2),(2, 1),(2, -1),(1, -2), + (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)) +} + +def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = + moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length) + +moves(8)(1,3) +ordered_moves(8)(Nil)(1,3) +ordered_moves(8)(List((2, 4), (2, 6)))(1,3) + +// non-circle tour parallel +def tour(n: Int)(steps: List[(Int, Int)]): Unit = { + if (steps.length == n * n && moves(n)(steps.head).contains(steps.last)) + print_board(n)(steps) + else + for (x <- moves(n)(steps.head).par; if (!steps.contains(x))) tour(n)(x :: steps) +} + +val n = 7 +println(s"circle tour parallel: n = $n") + +val starts = for (i <- 0 until n; j <- 0 until n) yield (i, j) + +starts.par.foreach((x:(Int, Int)) => tour(n)(List(x))) diff -r 575a5c07c7fe -r f31c22f4f104 progs/knight4.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/knight4.scala Wed Nov 02 13:22:40 2016 +0000 @@ -0,0 +1,45 @@ +import scala.util._ + +def print_board(n: Int)(steps: List[(Int, Int)]): Unit = synchronized { + for (i <- 0 until n) { + for (j <- 0 until n) { + print(f"${steps.indexOf((i, j))}%3.0f ") + } + println + } + //readLine() + System.exit(0) +} + +def add_pair(x: (Int, Int))(y: (Int, Int)) = + (x._1 + y._1, x._2 + y._2) + +def is_legal(n: Int)(x: (Int, Int)) = + 0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n + +def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = { + List((1, 2),(2, 1),(2, -1),(1, -2), + (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)) +} + +def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = + moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length) + +moves(8)(1,3) +ordered_moves(8)(Nil)(1,3) +ordered_moves(8)(List((2, 4), (2, 6)))(1,3) + +// non-circle tour parallel +def tour(n: Int)(steps: List[(Int, Int)]): Unit = { + if (steps.length == n * n && moves(n)(steps.head).contains(steps.last)) + print_board(n)(steps) + else + for (x <- ordered_moves(n)(steps)(steps.head).par; if (!steps.contains(x))) tour(n)(x :: steps) +} + +val n = 7 +println(s"circle tour parallel fast: n = $n") + +val starts = for (i <- 0 until n; j <- 0 until n) yield (i, j) + +starts.par.foreach((x:(Int, Int)) => tour(n)(List(x))) diff -r 575a5c07c7fe -r f31c22f4f104 progs/knight5.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/knight5.scala Wed Nov 02 13:22:40 2016 +0000 @@ -0,0 +1,45 @@ +import scala.util._ + +def print_board(n: Int)(steps: List[(Int, Int)]): Unit = synchronized { + for (i <- 0 until n) { + for (j <- 0 until n) { + print(f"${steps.indexOf((i, j))}%3.0f ") + } + println + } + //readLine() + System.exit(0) +} + +def add_pair(x: (Int, Int))(y: (Int, Int)) = + (x._1 + y._1, x._2 + y._2) + +def is_legal(n: Int)(x: (Int, Int)) = + 0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n + +def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = { + List((1, 2),(2, 1),(2, -1),(1, -2), + (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)) +} + +def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = + moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length) + +moves(8)(1,3) +ordered_moves(8)(Nil)(1,3) +ordered_moves(8)(List((2, 4), (2, 6)))(1,3) + +// non-circle tour parallel +def tour(n: Int)(steps: List[(Int, Int)]): Unit = { + if (steps.length == n * n) // && moves(n)(steps.head).contains(steps.last)) + print_board(n)(steps) + else + for (x <- ordered_moves(n)(steps)(steps.head); if (!steps.contains(x))) tour(n)(x :: steps) +} + +val n = 21 +println(s"circle tour parallel fast: n = $n") + +val starts = for (i <- 0 until n; j <- 0 until n) yield (i, j) + +starts.par.foreach((x:(Int, Int)) => tour(n)(List(x))) diff -r 575a5c07c7fe -r f31c22f4f104 progs/re.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/re.scala Wed Nov 02 13:22:40 2016 +0000 @@ -0,0 +1,104 @@ + +abstract class Rexp +case object ZERO extends Rexp +case object ONE extends Rexp +case class CHAR(c: Char) extends Rexp +case class ALT(r1: Rexp, r2: Rexp) extends Rexp +case class SEQ(r1: Rexp, r2: Rexp) extends Rexp +case class STAR(r: Rexp) extends Rexp +case class NTIMES(r: Rexp, n: Int) extends Rexp +case class UPNTIMES(r: Rexp, n: Int) extends Rexp + +// nullable function: tests whether the regular +// expression can recognise the empty string +def nullable (r: Rexp) : Boolean = r match { + case ZERO => false + case ONE => true + case CHAR(_) => false + case ALT(r1, r2) => nullable(r1) || nullable(r2) + case SEQ(r1, r2) => nullable(r1) && nullable(r2) + case STAR(_) => true + case NTIMES(r, i) => if (i == 0) true else nullable(r) + case UPNTIMES(r: Rexp, n: Int) => true +} + +// derivative of a regular expression w.r.t. a character +def der (c: Char, r: Rexp) : Rexp = r match { + case ZERO => ZERO + case ONE => ZERO + case CHAR(d) => if (c == d) ONE else ZERO + case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) + case SEQ(r1, r2) => + if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) + else SEQ(der(c, r1), r2) + case STAR(r1) => SEQ(der(c, r1), STAR(r1)) + case NTIMES(r1, i) => + if (i == 0) ZERO else + if (nullable(r1)) SEQ(der(c, r1), UPNTIMES(r1, i - 1)) + else SEQ(der(c, r1), NTIMES(r1, i - 1)) + case UPNTIMES(r1, i) => + if (i == 0) ZERO + else SEQ(der(c, r1), UPNTIMES(r1, i - 1)) +} + +def simp(r: Rexp) : Rexp = r match { + case ALT(r1, r2) => (simp(r1), simp(r2)) match { + case (ZERO, r2s) => r2s + case (r1s, ZERO) => r1s + case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s) + } + case SEQ(r1, r2) => (simp(r1), simp(r2)) match { + case (ZERO, _) => ZERO + case (_, ZERO) => ZERO + case (ONE, r2s) => r2s + case (r1s, ONE) => r1s + case (r1s, r2s) => SEQ(r1s, r2s) + } + case r => r +} + + +// derivative w.r.t. a string (iterates der) +def ders (s: List[Char], r: Rexp) : Rexp = s match { + case Nil => r + case c::s => ders(s, simp(der(c, r))) +} + + +// main matcher function +def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) + +//one or zero +def OPT(r: Rexp) = ALT(r, ONE) + +//evil regular expressions +def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) +val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) + + +def time_needed[T](i: Int, code: => T) = { + val start = System.nanoTime() + for (j <- 1 to i) code + val end = System.nanoTime() + (end - start)/(i * 1.0e9) +} + + +//test: (a?{n}) (a{n}) +for (i <- 1 to 8001 by 1000) { + println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) +} + +for (i <- 1 to 8001 by 1000) { + println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) +} + +//test: (a*)* b +for (i <- 1 to 7000001 by 500000) { + println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) +} + +for (i <- 1 to 7000001 by 500000) { + println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) +} +