From 8749008d53ea6b73ecb1e78f32e6d30a71c31eea Mon Sep 17 00:00:00 2001 From: Achraf Date: Mon, 4 Dec 2023 07:19:20 +0100 Subject: [PATCH] dfs fulkerson wip --- src/ftest.ml | 17 +++++++--- src/fulkerson.ml | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 src/fulkerson.ml diff --git a/src/ftest.ml b/src/ftest.ml index faec057..0c3b77a 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -1,5 +1,6 @@ open Gfile -open Tools +(* open Tools *) +open Fulkerson let () = @@ -19,7 +20,7 @@ let () = (* Arguments are : infile(1) source-id(2) sink-id(3) outfile(4) *) let infile = Sys.argv.(1) - and outfile = Sys.argv.(4) + (* and outfile = Sys.argv.(4) *) (* These command-line arguments are not used for the moment. *) and _source = int_of_string Sys.argv.(2) @@ -28,10 +29,16 @@ let () = (* Open file *) let graph = from_file infile in - let g = clone_nodes graph in + (* let g = clone_nodes graph in *) + let () = aff (dfs graph 0 [] 3) ; + Printf.printf "\n\n" ; + hash + in () + + (* Rewrite the graph that has been read. *) - let () = write_file outfile g in + (* let () = write_file outfile g in - () + () *) diff --git a/src/fulkerson.ml b/src/fulkerson.ml new file mode 100644 index 0000000..90644ee --- /dev/null +++ b/src/fulkerson.ml @@ -0,0 +1,84 @@ +open Graph +open Tools + +let test graph = + let arcs = out_arcs graph 1 in + match arcs with + | [] -> clone_nodes 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 + 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 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 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