From 516cd8b354b66dbd499e8d15f87c0ff3714820d3 Mon Sep 17 00:00:00 2001 From: Bensebaa Date: Mon, 4 Dec 2023 18:38:21 +0100 Subject: [PATCH] find_path complete --- src/ftest.ml | 4 +-- src/fulkerson.ml | 77 +++++++++++------------------------------------- 2 files changed, 19 insertions(+), 62 deletions(-) diff --git a/src/ftest.ml b/src/ftest.ml index 0c3b77a..05bbe97 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -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 *) diff --git a/src/fulkerson.ml b/src/fulkerson.ml index 90644ee..1bcf9f7 100644 --- a/src/fulkerson.ml +++ b/src/fulkerson.ml @@ -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 \ No newline at end of file