public void Ordinal()
        {
            var ff = new FordFulkerson();
            ff.AddEdge(0, 1, 10);
            ff.AddEdge(0, 2, 2);
            ff.AddEdge(1, 2, 6);
            ff.AddEdge(1, 3, 6);
            ff.AddEdge(2, 4, 5);
            ff.AddEdge(3, 2, 3);
            ff.AddEdge(3, 4, 8);
            Assert.AreEqual(11, ff.GetMaxFlow(0, 4));

            ff = new FordFulkerson();
            ff.AddEdge(0, 1, 10);
            ff.AddEdge(0, 2, 2);
            ff.AddEdge(1, 2, 6);
            ff.AddEdge(1, 3, 6);
            ff.AddEdge(2, 4, 5);
            ff.AddEdge(3, 2, 3);
            ff.AddEdge(3, 4, 8);
            Assert.AreEqual(11, ff.GetMaxFlow(1, 4));

            ff = new FordFulkerson();
            ff.AddEdge(0, 1, 10);
            ff.AddEdge(0, 2, 2);
            ff.AddEdge(1, 2, 6);
            ff.AddEdge(1, 3, 6);
            ff.AddEdge(2, 4, 5);
            ff.AddEdge(3, 2, 3);
            ff.AddEdge(3, 4, 8);
            Assert.AreEqual(9, ff.GetMaxFlow(1, 2));
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            //Matrix matrix = new Matrix();
            //matrix.Run();

            new FordFulkerson().Run();
            FordFulkerson.PrintLn("Press key to exit ...");
            Console.ReadLine();
        }
        public void Test()
        {
            FlowNetwork network = GraphGenerator.flowNetwork();

            FordFulkerson fordFulkerson = new FordFulkerson(network, 0, 7);

            console.WriteLine("max-flow: " + fordFulkerson.Value);
            Console.WriteLine("max-flow: " + fordFulkerson.Value);
        }
Esempio n. 4
0
        public void GivenNullArcsWhenTryToFindMaximumFlowThenThrowError()
        {
            // arrange
            var source      = new SimpleNode("START");
            var destination = new SimpleNode("DESTINATION");

            // act & assert
            Assert.Throws <ArgumentNullException>(() => FordFulkerson.FindMaximumFlow(null, source, destination));
            Assert.Throws <ArgumentNullException>(() => FordFulkerson.FindMaximumFlow <IArcWithFlow <SimpleNode> >(null));
        }
        //https://github.com/lucasrabiec/MaximumFlowProblem
        public double Calculate(Graph g)
        {
            if (g.order == 0 || !InvariantBool.IsConnected.Calculate(g))
            {
                return(0);
            }
            FordFulkerson fordF = new FordFulkerson(g.adjacencyMatrix);

            return(fordF.FindMaximumFlow());
        }
Esempio n. 6
0
        /// <summary>
        /// The execute calculate graph.
        /// </summary>
        private void ExecuteCalculateGraph()
        {
            const string Source        = "s";
            const string Target        = "t";
            var          fordFulkerson = new FordFulkerson(this.visualizedGraph, Source, Target);

            this.graphSteps   = fordFulkerson.RunAlgorithm();
            this.IsCalculated = true;
            this.MaxFlow      = fordFulkerson.MaxFlow;
            this.MinCut       = fordFulkerson.MinCut;

            this.currentStep = 0;
            this.RaiseFlowAndResidualGraphChanged(this);
        }
Esempio n. 7
0
        public void Baseline()
        {
            var sut   = new FordFulkerson();
            var graph = new int[6][];

            graph[0] = new [] { 0, 16, 13, 0, 0, 0 };
            graph[1] = new [] { 0, 0, 10, 12, 0, 0 };
            graph[2] = new [] { 0, 4, 0, 0, 14, 0 };
            graph[3] = new [] { 0, 0, 9, 0, 0, 20 };
            graph[4] = new[] { 0, 0, 0, 7, 0, 4 };
            graph[5] = new[] { 0, 0, 0, 0, 0, 0 };

            Assert.Equal(23, sut.MaxFlow(graph));
        }
Esempio n. 8
0
        /*
         * Too long
         * public static void YangLiuPeptidesWithAllProteins()
         * {
         *  vsCSV csvPeptides = new vsCSV(@"G:\Thibault\-=Proteomics_Raw_Data=-\ELITE\DEC18_2012\DMatton\Clustering_186716\Cluster_Intensity_peptides_NormP.csv");
         *  vsCSVWriter writer = new vsCSVWriter(@"G:\Thibault\-=Proteomics_Raw_Data=-\ELITE\DEC18_2012\DMatton\Clustering_186716\ProteinsPerPeptidesFromDatabases.csv");
         *
         *  NucleicAcid.InitHash();
         *
         *  FileStream protein_fasta_database1 = new FileStream(@"G:\Thibault\Olivier\Databases\DMatton\Matton_Illumina_Anthesis_WithReverse.fasta", FileMode.Open, FileAccess.Read, FileShare.Read);
         *  List<Protein> proteins1 = new List<Protein>(ProteinFastaReader.ReadProteins(protein_fasta_database1, false));
         *  foreach(Protein prot in proteins1)
         *      prot.BaseSequence = prot.BaseSequence.Replace('T','U');
         *
         *  FileStream protein_fasta_database2 = new FileStream(@"G:\Thibault\Olivier\Databases\DMatton\mattond_20110418_WithReverse_EditedJuly2013.fasta", FileMode.Open, FileAccess.Read, FileShare.Read);
         *  List<Protein> proteins2 = new List<Protein>(ProteinFastaReader.ReadProteins(protein_fasta_database2, false));
         *  foreach (Protein prot in proteins2)
         *      prot.BaseSequence = prot.BaseSequence.Replace('T', 'U');
         *
         *  writer.AddLine(csvPeptides.LINES_LIST[0]);
         *  Dictionary<string, List<string>> dicOfPepProt = new Dictionary<string, List<string>>();
         *  for (int i = 1; i < csvPeptides.LINES_LIST.Count; i++)
         *  {
         *      string[] splits = csvPeptides.LINES_LIST[i].Split(vsCSV._Generic_Separator);
         *      string seq = splits[4];
         *
         *      StringBuilder sb = new StringBuilder();
         *      foreach (string alternateSeq in NucleicAcid.ConvertAAToNAs(seq))
         *      {
         *          foreach (Protein prot in proteins1)
         *              if (prot.BaseSequence.Contains(alternateSeq))
         *                  sb.Append(prot.Description + " ");
         *          foreach (Protein prot in proteins2)
         *              if (prot.BaseSequence.Contains(alternateSeq))
         *                  sb.Append(prot.Description + " ");
         *      }
         *      if(sb.Length == 0)
         *          Console.WriteLine("Zut");
         *      writer.AddLine(csvPeptides.LINES_LIST[i] + "," + sb.ToString().Trim());
         *  }
         *  writer.writeToFile();
         * }//*/
        /*
         * Only one protein returned by Mascot ... so the list of protein is always of length one
         * public static void YangLiuPeptidesWithAllProteins()
         * {
         *  vsCSV csvIDs = new vsCSV(@"G:\Thibault\-=Proteomics_Raw_Data=-\ELITE\DEC18_2012\DMatton\Clustering_186716\AllProteinPerPeptides.csv");
         *  //vsCSV csvPeptides = new vsCSV(@"G:\Thibault\-=Proteomics_Raw_Data=-\ELITE\DEC18_2012\DMatton\Clustering_186716\Cluster_Intensity_peptides_NormP.csv");
         *  vsCSVWriter writer = new vsCSVWriter(@"G:\Thibault\-=Proteomics_Raw_Data=-\ELITE\DEC18_2012\DMatton\Clustering_186716\ProteinsPerPeptides.csv");
         *
         *  Dictionary<string, List<string>> dicOfPepProt = new Dictionary<string, List<string>>();
         *  for (int i = 1; i < csvIDs.LINES_LIST.Count; i++)
         *  {
         *      string[] splits = csvIDs.LINES_LIST[i].Split(vsCSV._Generic_Separator);
         *      string seq = splits[1];
         *      List<string> protIDs;
         *      if(!dicOfPepProt.ContainsKey(seq))
         *          dicOfPepProt.Add(seq, new List<string>());
         *      protIDs = dicOfPepProt[seq];
         *
         *      AddOnce(protIDs, splits[2]);
         *
         *      for (int k = 3; k < splits.Length; k++)
         *      {
         *          if (!string.IsNullOrEmpty(splits[k]))
         *          {
         *              string[] prots = splits[k].Split(' ');
         *              seq = prots[3];
         *              if (!dicOfPepProt.ContainsKey(seq))
         *                  dicOfPepProt.Add(seq, new List<string>());
         *              protIDs = dicOfPepProt[seq];
         *
         *              AddOnce(protIDs, prots[0]);
         *          }
         *      }
         *  }
         *
         *  writer.AddLine("Peptide Sequence,Protein IDs");
         *  foreach (string key in dicOfPepProt.Keys)
         *  {
         *      StringBuilder sb = new StringBuilder(key);
         *      foreach (string prot in dicOfPepProt[key])
         *          sb.Append("," + prot);
         *      writer.AddLine(sb.ToString());
         *  }
         *  writer.writeToFile();
         * }//*/

        public static void TestGraphOptimization(Result result)
        {
            FordFulkerson             ff    = new FordFulkerson();
            List <FordFulkerson.Node> nodes = new List <FordFulkerson.Node>();
            List <FordFulkerson.Edge> edges = new List <FordFulkerson.Edge>();

            FordFulkerson.Node source = new FordFulkerson.Node("Source");
            nodes.Add(source);
            //foreach potential node
            //{
            //    nodes.Add(theNode);
            //    ff.AddNode(theNode);
            //    ff.AddEdge(source, theNode, capacity);
            //}
            ff.Optimize();
        }
Esempio n. 9
0
        public void BaselineWithStraightLines()
        {
            var sut   = new FordFulkerson();
            var graph = new int[4][];

            for (var i = 0; i < graph.Length; i++)
            {
                graph[i] = new int[4];
            }

            graph[0][1] = 3;
            graph[0][2] = 2;
            graph[1][3] = 2;
            graph[2][3] = 3;

            Assert.Equal(4, sut.MaxFlow(graph));
        }
Esempio n. 10
0
        public void UnitFordFulkerson()
        {
            // Let us create a graph shown in the above example
            int[,] graph = new int[6, 6] {
                { -1, 16, 13, 0, 0, 0 },
                { 0, -1, 10, 12, 0, 0 },
                { 0, 4, -1, 0, 14, 0 },
                { 0, 0, 9, -1, 0, 20 },
                { 0, 0, 0, 7, -1, 4 },
                { 0, 0, 0, 0, 0, -1 }
            };
            FordFulkerson newFlot = new FordFulkerson();

            newFlot.TwoDimensionArray = graph;
            newFlot.NumberOfNodes     = 6;

            //Console.WriteLine(newFlot.FordFulkersonWork(0, 5));
            Assert.AreEqual("23", newFlot.FordFulkersonWork(0, 5).ToString());
        }
    /**//**/ public static void main(string[] strarr)
    {
        int         num         = Integer.parseInt(strarr[0]);
        int         num2        = Integer.parseInt(strarr[1]);
        int         i           = 2 * num;
        int         i2          = 2 * num + 1;
        FlowNetwork flowNetwork = new FlowNetwork(2 * num + 2);

        for (int j = 0; j < num2; j++)
        {
            int k    = StdRandom.uniform(num);
            int num3 = StdRandom.uniform(num) + num;
            flowNetwork.addEdge(new FlowEdge(k, num3, double.PositiveInfinity));
            StdOut.println(new StringBuilder().append(k).append("-").append(num3).toString());
        }
        for (int j = 0; j < num; j++)
        {
            flowNetwork.addEdge(new FlowEdge(i, j, (double)1f));
            flowNetwork.addEdge(new FlowEdge(j + num, i2, (double)1f));
        }
        FordFulkerson fordFulkerson = new FordFulkerson(flowNetwork, i, i2);

        StdOut.println();
        StdOut.println(new StringBuilder().append("Size of maximum matching = ").append(ByteCodeHelper.d2i(fordFulkerson.value())).toString());
        for (int k = 0; k < num; k++)
        {
            Iterator iterator = flowNetwork.adj(k).iterator();
            while (iterator.hasNext())
            {
                FlowEdge flowEdge = (FlowEdge)iterator.next();
                if (flowEdge.from() == k && flowEdge.flow() > (double)0f)
                {
                    StdOut.println(new StringBuilder().append(flowEdge.from()).append("-").append(flowEdge.to()).toString());
                }
            }
        }
    }
Esempio n. 12
0
    void CriarNodos()
    {
        List <string> arquivoLido  = new List <string>(LerAqruivo().Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
        List <int>    numerosLidos = new List <int>();

        for (int i = 0; i < arquivoLido.Count; i++)
        {
            numerosLidos.Add(Int32.Parse(arquivoLido[i]));
        }



        for (int i = 0; i < numerosLidos.Count; i = i + 3)
        {
            //print(numerosLidos[i]);
            bool       novo;
            GameObject nodo;
            if (GameObject.Find("Nodo " + numerosLidos[i].ToString()) == null)
            {
                nodo = Instantiate(nodoPrefab, transform.position, Quaternion.identity, transform);
                novo = true;
            }
            else
            {
                nodo = GameObject.Find("Nodo " + numerosLidos[i].ToString());
                novo = false;
            }

            Nodo nodoScript = nodo.GetComponent <Nodo>();

            nodoScript.numero          = numerosLidos[i];
            nodoScript.gameObject.name = "Nodo " + numerosLidos[i].ToString();


            Conexao conexao = Instantiate(conexaoPrefab);
            conexao.transform.SetParent(nodo.transform);
            conexao.origem  = numerosLidos[i];
            conexao.destino = numerosLidos[i + 1];
            conexao.custo   = numerosLidos[i + 2];

            nodoScript.conexoes.Add(conexao);


            if (novo)
            {
                nodos.Add(nodoScript);
            }
        }

        print("numero de nodos na lista: " + nodos.Count);
        int numeroDeConexoes = 0;

        foreach (Nodo nodo in nodos)
        {
            foreach (Conexao conexao in nodo.conexoes)
            {
                numeroDeConexoes++;
            }
        }
        print(numeroDeConexoes);
        FlowNetwork flowNetwork = new FlowNetwork(numeroDeConexoes);

        foreach (Nodo nodo in nodos)
        {
            foreach (Conexao conexao in nodo.conexoes)
            {
                flowNetwork.AddEdge(new FlowEdge(nodo.numero, conexao.destino, conexao.custo));
            }
        }

        FordFulkerson fordFulkerson = new FordFulkerson(flowNetwork, 1, 99);

        print(fordFulkerson.Value);
    }