feat: find_path
This commit is contained in:
parent
7e93db8348
commit
4ac464339b
2 changed files with 33 additions and 0 deletions
26
src/findpath.ml
Normal file
26
src/findpath.ml
Normal file
|
|
@ -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 -> [];
|
||||
7
src/findpath.mli
Normal file
7
src/findpath.mli
Normal 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 -> id list
|
||||
Loading…
Reference in a new issue