From 4ac464339b9413f5a92241713d7741c18a34ccd5 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Mon, 2 Dec 2024 16:27:38 +0100 Subject: [PATCH] feat: find_path --- src/findpath.ml | 26 ++++++++++++++++++++++++++ src/findpath.mli | 7 +++++++ 2 files changed, 33 insertions(+) create mode 100644 src/findpath.ml create mode 100644 src/findpath.mli diff --git a/src/findpath.ml b/src/findpath.ml new file mode 100644 index 0000000..d3219b5 --- /dev/null +++ b/src/findpath.ml @@ -0,0 +1,26 @@ +open Graph + +let child_nodes (gr : 'a graph) (a: id) = List.map (fun x -> x.tgt) (out_arcs gr a) + +let find_path (gr: 'a graph) (begin_node: id) (end_node: id) = + let rec find_way s acu = + let s_child = child_nodes gr s in + + if s == end_node then + acu + else + let next_rec = List.filter (fun x -> not (List.mem x acu)) s_child in + let rec fp list = + match list with + | [] -> raise Not_found + | x::rest -> + try + find_way x (acu@[x]) + with + | Not_found -> fp rest + in fp next_rec + in + try + find_way begin_node [begin_node] + with + | Not_found -> []; diff --git a/src/findpath.mli b/src/findpath.mli new file mode 100644 index 0000000..421aa23 --- /dev/null +++ b/src/findpath.mli @@ -0,0 +1,7 @@ +open Graph + +(* return the id nodes lists of the child of a node *) +val child_nodes: 'a graph -> id -> id list + +(* search path between 2 node and return the path : a list of node *) +val find_path: 'a graph -> id -> id -> id list \ No newline at end of file