diff --git a/graphs/flow_graph1.txt b/graphs/flow_graph1.txt index 03a7ecc..dc2e634 100644 --- a/graphs/flow_graph1.txt +++ b/graphs/flow_graph1.txt @@ -7,15 +7,15 @@ n 200 400 3 n 380 300 4 n 380 200 5 -e 0 2 0 5 -e 0 3 1 5 -e 0 1 2 5 -e 2 4 3 5 -e 3 4 4 5 -e 3 2 5 5 -e 3 1 6 5 -e 1 4 7 5 -e 1 5 8 5 -e 4 5 9 5 +e 0 2 0 4 +e 0 3 1 4 +e 0 1 2 4 +e 2 4 3 4 +e 3 4 4 4 +e 3 2 5 4 +e 3 1 6 4 +e 1 4 7 4 +e 1 5 8 4 +e 4 5 9 4 % End of graph diff --git a/src/ftest.ml b/src/ftest.ml index 8f82245..1e807bf 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -1,5 +1,6 @@ open Gfile open Tools +open Fulkerson let () = @@ -31,12 +32,24 @@ let () = (*let g = clone_nodes graph in*) let flow_graph_1 = create_flow_graph graph in - let flow_graph_2 = update_flow_graph flow_graph_1 (fun _ -> true) 5 in + let flow_graph_2 = update_flow_graph flow_graph_1 (fun _ -> true) 4 in let string_flow_graph = gmap flow_graph_2 (fun x -> string_of_int x) in (* Rewrite the graph that has been read. *) let () = write_file outfile string_flow_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 + + () *) 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