From 6c88eb310c43ff31aa2416a49f73316651d996f6 Mon Sep 17 00:00:00 2001 From: kevin Date: Sat, 12 Dec 2020 19:47:38 +0100 Subject: [PATCH] cleanup + comments --- Makefile | 2 +- graphs/graph3 | 21 -------- graphs/graph3.dot | 14 ------ graphs/graph3.svg | 119 -------------------------------------------- graphs/graph4 | 21 -------- graphs/graph4.dot | 14 ------ graphs/graph4.svg | 119 -------------------------------------------- src/BLF.ml | 64 ++++++++++++------------ src/BLF.mli | 4 +- src/FFAlgorithm.ml | 14 +++--- src/FFAlgorithm.mli | 15 +----- src/ftest.ml | 4 +- src/tool.mli | 1 + 13 files changed, 46 insertions(+), 366 deletions(-) delete mode 100644 graphs/graph3 delete mode 100644 graphs/graph3.dot delete mode 100644 graphs/graph3.svg delete mode 100644 graphs/graph4 delete mode 100644 graphs/graph4.dot delete mode 100644 graphs/graph4.svg diff --git a/Makefile b/Makefile index 876857b..cd8fd35 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ edit: demo: build @echo "\n==== EXECUTING ====\n" - ./ftest.native graphs/graph1 1 2 outfile + ./ftest.native graphs/graph1 0 5 outfile @echo "\n==== RESULT ==== (content of outfile) \n" @cat outfile diff --git a/graphs/graph3 b/graphs/graph3 deleted file mode 100644 index 9e53954..0000000 --- a/graphs/graph3 +++ /dev/null @@ -1,21 +0,0 @@ -% This is a graph. - -n 0.0 1.0 -n 1.0 1.0 -n 2.0 1.0 -n 3.0 1.0 -n 4.0 1.0 -n 5.0 1.0 - -e 0 2 8/8 -e 0 3 10/10 -e 0 1 7/7 -e 2 4 9/12 -e 3 4 5/5 -e 3 2 1/2 -e 3 1 4/11 -e 1 4 0/1 -e 1 5 11/21 -e 4 5 14/14 - -% End of graph diff --git a/graphs/graph3.dot b/graphs/graph3.dot deleted file mode 100644 index 6112173..0000000 --- a/graphs/graph3.dot +++ /dev/null @@ -1,14 +0,0 @@ -digraph graphique1 { - size="20" - node [shape = circle]; - 0 -> 2 [ label = "8/8" ]; - 0 -> 3 [ label = "10/10" ]; - 0 -> 1 [ label = "7/7" ]; - 2 -> 4 [ label = "9/12" ]; - 3 -> 4 [ label = "5/5" ]; - 3 -> 2 [ label = "1/2" ]; - 3 -> 1 [ label = "4/11" ]; - 1 -> 4 [ label = "0/1" ]; - 1 -> 5 [ label = "11/21" ]; - 4 -> 5 [ label = "14/14" ]; -} diff --git a/graphs/graph3.svg b/graphs/graph3.svg deleted file mode 100644 index 36d9264..0000000 --- a/graphs/graph3.svg +++ /dev/null @@ -1,119 +0,0 @@ - - - - - - -graphique1 - - - -0 - -0 - - - -2 - -2 - - - -0->2 - - -8/8 - - - -3 - -3 - - - -0->3 - - -10/10 - - - -1 - -1 - - - -0->1 - - -7/7 - - - -4 - -4 - - - -2->4 - - -9/12 - - - -3->2 - - -1/2 - - - -3->1 - - -4/11 - - - -3->4 - - -5/5 - - - -1->4 - - -0/1 - - - -5 - -5 - - - -1->5 - - -11/21 - - - -4->5 - - -14/14 - - - diff --git a/graphs/graph4 b/graphs/graph4 deleted file mode 100644 index f7565be..0000000 --- a/graphs/graph4 +++ /dev/null @@ -1,21 +0,0 @@ -% This is a graph. - -n 0.0 1.0 -n 1.0 1.0 -n 2.0 1.0 -n 3.0 1.0 -n 4.0 1.0 -n 5.0 1.0 - -e 4 5 4/4 -e 4 3 7/7 -e 3 5 19/20 -e 3 2 0/9 -e 2 4 11/14 -e 2 1 2/4 -e 1 3 12/12 -e 1 2 0/10 -e 0 2 13/13 -e 0 1 10/16 - -% End of graph diff --git a/graphs/graph4.dot b/graphs/graph4.dot deleted file mode 100644 index 332fe6c..0000000 --- a/graphs/graph4.dot +++ /dev/null @@ -1,14 +0,0 @@ -digraph graphique1 { - size="20" - node [shape = circle]; - 4 -> 5 [ label = "4/4" ]; - 4 -> 3 [ label = "7/7" ]; - 3 -> 5 [ label = "19/20" ]; - 3 -> 2 [ label = "0/9" ]; - 2 -> 4 [ label = "11/14" ]; - 2 -> 1 [ label = "2/4" ]; - 1 -> 3 [ label = "12/12" ]; - 1 -> 2 [ label = "0/10" ]; - 0 -> 2 [ label = "13/13" ]; - 0 -> 1 [ label = "10/16" ]; -} diff --git a/graphs/graph4.svg b/graphs/graph4.svg deleted file mode 100644 index abb0cd8..0000000 --- a/graphs/graph4.svg +++ /dev/null @@ -1,119 +0,0 @@ - - - - - - -graphique1 - - - -4 - -4 - - - -5 - -5 - - - -4->5 - - -4/4 - - - -3 - -3 - - - -4->3 - - -7/7 - - - -3->5 - - -19/20 - - - -2 - -2 - - - -3->2 - - -0/9 - - - -2->4 - - -11/14 - - - -1 - -1 - - - -2->1 - - -2/4 - - - -1->3 - - -12/12 - - - -1->2 - - -0/10 - - - -0 - -0 - - - -0->2 - - -13/13 - - - -0->1 - - -10/16 - - - diff --git a/src/BLF.ml b/src/BLF.ml index 11316de..945e329 100644 --- a/src/BLF.ml +++ b/src/BLF.ml @@ -3,48 +3,48 @@ open Graph type path = id list (*type record avec id noeud et son cout*) -type t_cost={ +type t_cost = { mutable cout:int; mutable father:int } -let blf gr id_src id_dest= +let blf graph idSrc = (*je compte le nb de noeuds dans le graphe pour instancier mon tableau*) - let nb_n=n_fold gr (fun acu id->acu+1) 0 in + let nb_n = n_fold graph (fun acu id->acu+1) 0 in - let cost ={cout=max_int; father=(-1)} in + let cost = {cout=max_int; father=(-1)} in - let acu =Array.make nb_n cost in + let acu = Array.make nb_n cost in (*je fais un fold_left pour pouvoir individualiser au niveau de la mémoire les cases de la table*) - let blf_tab=n_fold gr (fun acu id->acu.(id)<-{cout=max_int; father=(-1)}; acu ) acu in - blf_tab.(id_src).cout<-0; - let file_id=[id_src] in - let file_marque =[] in + let blfTab = n_fold graph (fun acu id->acu.(id)<-{cout = max_int; father = (-1) }; acu ) acu in + blfTab.(idSrc).cout <- 0; + let fileId = [idSrc] in + let fileMarquee = [] in - let rec blf_rec gr file_id file_marque= match file_id with - |[]-> blf_tab + let rec blf_rec graph fileId fileMarquee= match fileId with + |[]-> blfTab |a::b-> - let l_out_arc=out_arcs gr a in - let rec loop_suc l_out_arc blf_tab file = - match l_out_arc with - |[]-> blf_rec gr file (a::file_marque) - |(id,label)::d-> - if label != 0 && (blf_tab.(a).cout+label) blf_rec graph file (a::fileMarquee) + |(id,label)::d -> + if label != 0 && (blfTab.(a).cout+label) < blfTab.(id).cout then begin - blf_tab.(id).cout<-(blf_tab.(a).cout+label); - blf_tab.(id).father<-a; - if not (List.mem id file_marque) then loop_suc d blf_tab (id::file) else loop_suc d blf_tab file + blfTab.(id).cout <- (blfTab.(a).cout+label); + blfTab.(id).father <- a; + if not (List.mem id fileMarquee) then loop_suc d blfTab (id::file) else loop_suc d blfTab file end - else loop_suc d blf_tab file in - loop_suc l_out_arc blf_tab b in - blf_rec gr file_id file_marque + else loop_suc d blfTab file in + loop_suc lOutArcs blfTab b in + blf_rec graph fileId fileMarquee -(*avec blf_tab, on retrace chemin avec les pères*) -let get_path gr id_src id_dest= - let blf_tab=blf gr id_src id_dest in - let path=[id_dest] in - let rec loop path blf_tab id_src id_dest= - let father_id=blf_tab.(id_dest).father in match father_id with - |(-1)->None - |a->if a == id_src then Some (id_src::path) else loop (a::path) blf_tab id_src a in - loop path blf_tab id_src id_dest +(*avec blfTab, on retrace chemin avec les pères*) +let get_path graph idSrc idDst = + let blfTab = blf graph idSrc in + let path = [idDst] in + let rec loop path blfTab idSrc idDst = + let fatherId = blfTab.(idDst).father in match fatherId with + |(-1) -> None + |a -> if a == idSrc then Some (idSrc::path) else loop (a::path) blfTab idSrc a in + loop path blfTab idSrc idDst diff --git a/src/BLF.mli b/src/BLF.mli index e8dc9e4..0882d5d 100644 --- a/src/BLF.mli +++ b/src/BLF.mli @@ -7,6 +7,8 @@ type t_cost={ mutable father:int } -val blf: int graph -> id -> id -> t_cost array +(* Execute the Bellman-Ford algorithm on a graph from the node with the specified id *) +val blf: int graph -> id -> t_cost array +(* Return a path option from a source node to a destination node *) val get_path: int graph -> id -> id -> path option \ No newline at end of file diff --git a/src/FFAlgorithm.ml b/src/FFAlgorithm.ml index bd0223b..8d8e15d 100644 --- a/src/FFAlgorithm.ml +++ b/src/FFAlgorithm.ml @@ -60,17 +60,17 @@ let get_final_graph (initGraph : int graph) (residualGraph : int graph) = e_fold initGraph ( fun acu id1 id2 x -> - let label_arc = (match find_arc initGraphString id1 id2 with + let labelArc = (match find_arc initGraphString id1 id2 with |None -> 0 |Some x -> x) in - let label_rev_arc = match find_arc residualGraphString id2 id1 with + let labelRevArc = match find_arc residualGraphString id2 id1 with |None -> 0 |Some x -> (match find_arc initGraphString id2 id1 with |None -> x |Some y -> x-y) in - let label_arc = string_of_int label_arc in - let label_rev_arc = if (label_rev_arc > 0) then (string_of_int label_rev_arc) else "0" in - new_arc acu id1 id2 (label_rev_arc^"/"^label_arc) + let labelArc = string_of_int labelArc in + let labelRevArc = if (labelRevArc > 0) then (string_of_int labelRevArc) else "0" in + new_arc acu id1 id2 (labelRevArc^"/"^labelArc) ) finalGraph @@ -87,15 +87,12 @@ let ford_fulk_algorithm (graph : int graph) (origin : id) (sink : id) = (let path = x in let arcs = create_arcs_from_nodes path in - (*let () = printf "dans boucle\n" in*) - (* Find the min value of the path *) let min = get_min_label_from_path graph arcs in (* Substract the min to every arc of the path *) let graph = add_value_to_arcs graph arcs (-min) in - (* Get the reverse path *) let reverse = rev_arcs arcs in @@ -105,6 +102,7 @@ let ford_fulk_algorithm (graph : int graph) (origin : id) (sink : id) = (* Add the min to the flow *) let flow = flow + min in boucle graph origin sink flow) in + let (maxFlow, residualGraph) = boucle graph origin sink flow in let finalGraph = get_final_graph initGraph residualGraph in (maxFlow, finalGraph) diff --git a/src/FFAlgorithm.mli b/src/FFAlgorithm.mli index 8a19f29..7479cb4 100644 --- a/src/FFAlgorithm.mli +++ b/src/FFAlgorithm.mli @@ -2,19 +2,8 @@ open Graph open Tool open BLF - +(* Return a int graph from a string graph *) val g_to_int: string graph -> int graph +(* Return a string graph after applying the ford-fulkerson algorithm on an int graph (capacity) *) val ford_fulk_algorithm : int graph -> id -> id -> (int * string graph) - -(* val g_to_string: int graph -> string graph *) - -(* val only_one_edge: int graph -> int graph *) - -(* for testing purpose *) - -(* val rev_arcs: (id * id) list -> (id * id) list - -val add_value_to_arcs: int graph -> (id * id) list -> int -> int graph - -val get_final_graph: int graph -> int graph -> string graph *) \ No newline at end of file diff --git a/src/ftest.ml b/src/ftest.ml index 33135d5..1b5e8bf 100644 --- a/src/ftest.ml +++ b/src/ftest.ml @@ -8,7 +8,7 @@ open Sys let () = (*/!\ Format de la commande pour lancer le test : - ./ftest.native [nom_fichier_lecture] [id_source] [id_dest] [nom_fichier_ecriture] + ./ftest.native [path_input_file] [source] [sink] [path_output_file] ex : ./ftest.native graphs/graph1 0 5 graphs/graph3 *) (* Check the number of command-line arguments *) @@ -39,8 +39,6 @@ let () = let () = printf "max flow = %d\n" flow in let () = write_file outfile finalGraph in let () = export outfile finalGraph in - (* let () = export infile graph in *) - (*Uncomment the following line if you have graphviz installed *) (*let retour = command ("dot -Tsvg "^outfile^".dot > "^outfile^".svg") in*) diff --git a/src/tool.mli b/src/tool.mli index 0ca3797..831ccd8 100644 --- a/src/tool.mli +++ b/src/tool.mli @@ -6,4 +6,5 @@ val clone_nodes: 'a graph -> 'b graph (* Apply a function f to every label of the graph's arcs *) val gmap: 'a graph -> ('a -> 'b) -> 'b graph +(* Add a value to the capacity of the arc id1 id2 in the graph*) val add_arc: int graph -> id -> id -> int -> int graph \ No newline at end of file