Skip to content

Commit

Permalink
Add a new example of topological sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Akinori ABE committed Aug 16, 2015
1 parent 2ce2cbb commit cf20ca5
Show file tree
Hide file tree
Showing 8 changed files with 945 additions and 800 deletions.
63 changes: 63 additions & 0 deletions examples/topological_sort/tsort.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
(** Example: topological sort *)

#use "list.ml"

let tsort vs es =
let is_leaf es v =
list_for_all (fun e -> match e with (_, v2) -> v <> v2) es
in
let partition_leaves vs es =
list_partition (fun e -> match e with (v, _) -> list_mem v vs) es
in
let rec aux acc vs es =
match list_partition (is_leaf es) vs with
| (vs1, []) -> list_flatten (list_rev (vs1 :: acc))
| (vs1, vs2) ->
match partition_leaves vs1 es with (_, es2) -> aux (vs1 :: acc) vs2 es2
in
aux [] vs es

(* +----> [2] --> [5] <-- [7]
| ^ ^
| | |
[1] <-- [3] -----+ |
^ ^ |
| | |
+----- [4] <---------- [6] *)
let vertices = [1; 2; 3; 4; 5; 6; 7]
let edges = [ (1, 2); (* (vertex_begin, vertex_end) *)
(2, 5);
(3, 1);
(3, 5);
(4, 1);
(4, 3);
(6, 4);
(6, 7);
(7, 5) ]

(* Result: 6, 4, 7, 3, 1, 2, 5 *)
let xs = tsort vertices edges
let x0 = list_nth xs 0
let x1 = list_nth xs 1
let x2 = list_nth xs 2
let x3 = list_nth xs 3
let x4 = list_nth xs 4
let x5 = list_nth xs 5
let x6 = list_nth xs 6

(*!
// This is C++ code.
#include <cstdio>
int main () { // We use printf in order to output readable assembly code.
std::printf("%d ", x0::val);
std::printf("%d ", x1::val);
std::printf("%d ", x2::val);
std::printf("%d ", x3::val);
std::printf("%d ", x4::val);
std::printf("%d ", x5::val);
std::printf("%d\n", x6::val);
return 0;
}
*)
1 change: 1 addition & 0 deletions gen_files.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ let make_ml in_file out_file =
let () =
make_ml "examples/fib/fib.ml" "src/example_fib.ml";
make_ml "examples/quicksort/qsort.ml" "src/example_qsort.ml";
make_ml "examples/topological_sort/tsort.ml" "src/example_tsort.ml";
make_ml "examples/dijkstra/dijkstra.ml" "src/example_dijkstra.ml";
make_ml "include/evilml.hpp" "src/evilml_hpp.ml";
let in_mls = Sys.readdir "include"
Expand Down
1 change: 1 addition & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ <h1>Evil ML: ML to C++ template language</h1>
Examples:
<input type="button" value="Fibonacci" id="btn_ex_fib">
<input type="button" value="Quick sort" id="btn_ex_qsort">
<input type="button" value="Topological sort" id="btn_ex_tsort">
<input type="button" value="Dijkstra's algoritm" id="btn_ex_dijkstra">
</div>
<hr>
Expand Down
Loading

0 comments on commit cf20ca5

Please sign in to comment.