This commit is contained in:
Leandro-rdz 2024-12-02 16:38:58 +01:00
commit a39d5596b7
2 changed files with 42 additions and 0 deletions

35
src/findpath.ml Normal file
View file

@ -0,0 +1,35 @@
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) (filter: 'a arc -> bool)=
let rec find_way s acu =
let s_child = child_nodes gr s in
let arc x1 x2 =
match find_arc gr x1 x2 with
| Some x -> x
| None -> raise Not_found
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
if (filter (arc s x)) then
find_way x (acu@[x])
else
fp rest
with
| Not_found -> fp rest
in fp next_rec
in
try
find_way begin_node [begin_node]
with
| Not_found -> [];

7
src/findpath.mli Normal file
View file

@ -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 -> ('a arc -> bool) -> id list