From caf91193b7f473d08b9e03f1b031e0dc5432497b Mon Sep 17 00:00:00 2001 From: Tim Vieira Date: Tue, 2 Jul 2013 17:48:27 -0400 Subject: [PATCH] added example of optimal path extraction for shortest path. --- examples/dijkstra-backpointers.dyna | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 examples/dijkstra-backpointers.dyna diff --git a/examples/dijkstra-backpointers.dyna b/examples/dijkstra-backpointers.dyna new file mode 100644 index 0000000..8c950b6 --- /dev/null +++ b/examples/dijkstra-backpointers.dyna @@ -0,0 +1,37 @@ +% single source shortest path with optimal path extraction. +path(start) min= 0. +path(B) min= path(A) + edge(A,B). +goal min= 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". + +% use argmin to determine backpointers +b(V) argmin= [path(U) + edge(U,V), U]. + +% extract cheapest path by following backpointers from each vertex. +bestpath(start) := [start]. +bestpath(V) := U is b(V), [V | bestpath(U)]. + +% the optimal path is the one from the `end`. +optimalpath = reverse(bestpath(end)). + +% --------------------------------------- +% list utilities +:- backchain reverse/1. +reverse([]) = []. +reverse([X|Xs]) = append(reverse(Xs), X). + +:- backchain append/2. +append([], Y) = [Y]. +append([X|Xs], Y) = [X|append(Xs, Y)]. -- 2.50.1