find_path complete
This commit is contained in:
parent
8749008d53
commit
516cd8b354
2 changed files with 19 additions and 62 deletions
|
|
@ -30,10 +30,10 @@ let () =
|
|||
(* Open file *)
|
||||
let graph = from_file infile in
|
||||
(* let g = clone_nodes graph in *)
|
||||
let () = aff (dfs graph 0 [] 3) ;
|
||||
let () = aff (find_path graph 0 4) ;
|
||||
Printf.printf "\n\n" ;
|
||||
hash
|
||||
in ()
|
||||
(* export outfile graph *)
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -8,77 +8,34 @@ let test graph =
|
|||
| x::_rest -> new_arc (clone_nodes graph) x
|
||||
|
||||
|
||||
(* let rec find_path graph s target =
|
||||
let arcs = out_arcs graph s in
|
||||
match arcs with
|
||||
| [] -> []
|
||||
| arc1::_restArcs ->
|
||||
try
|
||||
let dernier_arc=List.find (fun arc -> arc.tgt=target) arcs in
|
||||
[dernier_arc]
|
||||
with Not_found ->
|
||||
arc1::(find_path graph arc1.tgt target) *)
|
||||
|
||||
|
||||
let successors n graph =
|
||||
let successors = out_arcs graph n in
|
||||
List.map (fun arc -> arc.tgt) successors
|
||||
|
||||
(* let rec explore graph s =
|
||||
let rec loop voisins visited=
|
||||
match voisins with
|
||||
| [] -> visited
|
||||
| x::rest ->
|
||||
if not (List.mem x visited) then
|
||||
explore graph x
|
||||
else visited in
|
||||
loop graph [] *)
|
||||
|
||||
let memory = Hashtbl.create 20
|
||||
|
||||
let rec dfs graph node visited dest=
|
||||
if List.mem node visited || List.mem dest visited then
|
||||
begin
|
||||
Hashtbl.add memory node node ; visited
|
||||
end
|
||||
let rec find_path_aux graph idList src tgt =
|
||||
if src = tgt then
|
||||
[src]
|
||||
else
|
||||
let rec explore_neighbors visited_aux neighbors =
|
||||
match neighbors with
|
||||
| [] -> visited_aux
|
||||
| neighbor::rest ->
|
||||
let a = dfs graph neighbor visited_aux dest in
|
||||
explore_neighbors a rest in
|
||||
explore_neighbors (node::visited) (successors node graph)
|
||||
let rec loop neighbors idList =
|
||||
match neighbors with
|
||||
| [] -> [] (*pas de chemin*)
|
||||
| n1::rest ->
|
||||
let path = (find_path_aux graph (n1::idList) n1 tgt) in
|
||||
match path with
|
||||
| [] -> loop rest (n1::idList)
|
||||
| _ ->
|
||||
if List.mem n1 path then
|
||||
path
|
||||
else n1::path in
|
||||
loop (successors src graph) idList
|
||||
|
||||
let find_path graph src dest =
|
||||
(Hashtbl.clear memory) ;
|
||||
dfs graph src [] dest
|
||||
|
||||
|
||||
let hash = Hashtbl.add memory 2 2;
|
||||
Hashtbl.iter (fun x y -> Printf.printf "%d -> %d\n" x y) memory;;
|
||||
|
||||
(* let dfs graph start dest =
|
||||
let rec rdfs visited node =
|
||||
if not (List.mem node visited) && not(List.mem dest visited) then
|
||||
begin
|
||||
(* f node; *)
|
||||
let s = successors node graph in
|
||||
(* if List.mem dest s then
|
||||
dest::visited
|
||||
else *)
|
||||
List.fold_left rdfs (node::visited) s
|
||||
end
|
||||
else visited
|
||||
in rdfs [] start *)
|
||||
let find_path graph src tgt =
|
||||
src::(find_path_aux graph [] src tgt)
|
||||
|
||||
let rec aff nodes =
|
||||
match nodes with
|
||||
| [] -> ()
|
||||
| x::rest -> Printf.printf "%d " x ; aff rest
|
||||
|
||||
let rec create nodes =
|
||||
match nodes with
|
||||
| [] -> empty_graph
|
||||
| x::rest -> new_node (create rest) x
|
||||
|
||||
Loading…
Reference in a new issue