]> hydra-www.ietfng.org Git - dyna2/commitdiff
simplified examples/dijkstra-backpointers.dyna, and added a variant
authorJason Eisner <jason@cs.jhu.edu>
Wed, 10 Jul 2013 08:35:01 +0000 (04:35 -0400)
committerJason Eisner <jason@cs.jhu.edu>
Wed, 10 Jul 2013 08:35:01 +0000 (04:35 -0400)
debug [changed mode: 0755->0644]
examples/dijkstra-backpointers.dyna
examples/dijkstra-backpointers2.dyna [new file with mode: 0644]

diff --git a/debug b/debug
old mode 100755 (executable)
new mode 100644 (file)
index b5506659af096cb7ee747cea9393ae122eff5aae..68f5f97e93a86be2923fc090508fb9b5d637e48f 100644 (file)
@@ -1,7 +1,16 @@
-% single source shortest path with optimal path extraction.
-path(start) min= 0                   with_key start.
+% Single source shortest path with optimal path extraction.
+
+% Cost of the optimal path from start to each node.  
+path(start) min= 0                   with_key [].
 path(B)     min= path(A) + edge(A,B) with_key A.
-goal        min= path(end)           with_key end.
+
+% The cost of the optimal path to the end node, and that path itself.
+goal        = path(end).
+optimalpath = reverse(bestpath(end)).
+
+% extract cheapest path by following backpointers from each vertex.
+bestpath(V) := [V | bestpath($key(path(V)))].
+bestpath(start) := [start].
 
 % expensive path
 edge("a","b") := 1.
@@ -16,12 +25,6 @@ edge("e","d") := 1.
 start := "a".
 end := "d".
 
-% extract cheapest path by following backpointers from each vertex.
-bestpath(start) := [start].
-bestpath(V) := U is $key(path(V)), [V | bestpath(U)] for V != start.
-
-% the optimal path is the one from the `end`.
-optimalpath = reverse(bestpath(end)).
 
 % ---------------------------------------
 % list utilities
diff --git a/examples/dijkstra-backpointers2.dyna b/examples/dijkstra-backpointers2.dyna
new file mode 100644 (file)
index 0000000..bb7948e
--- /dev/null
@@ -0,0 +1,32 @@
+% Single source shortest path with optimal path extraction.
+% This version is briefer but possibly less efficient.
+
+% Cost of the optimal path from start to each node.  
+% In each case, the associated key is a reversed version of the path itself.
+path(start) min= 0                   with_key [start].
+path(B)     min= path(A) + edge(A,B) with_key P for P is append($key(path(A)),B).
+
+% The cost of the optimal path to the end node, and that path itself.
+goal        = path(end).
+optimalpath = $key(path(end)).
+
+% expensive path
+edge("a","b") := 1.
+edge("b","c") := 1.
+edge("c","d") := 1.
+
+% cheap path
+edge("a","e") := 1.
+edge("e","d") := 1.
+
+% define begining and end.
+start := "a".
+end := "d".
+
+
+% ---------------------------------------
+% list utilities
+
+:- backchain append/2.
+append([], Y) = [Y].
+append([X|Xs], Y) = [X|append(Xs, Y)].