Exemplo n.º 1
0
        public double GetValoareMaxima(Retea retea)
        {
            Console.WriteLine("Retea initiala: \n");
            Console.WriteLine(retea.ToString());

            Retea reteaUpdatata = UpdateRetea(retea);

            return(reteaUpdatata.GetValoareMaxima());
        }
Exemplo n.º 2
0
        private void InaintareReetichetare(Retea retea, Nod x)
        {
            List <Arc> arceDinX = retea.Arce.Where(a => a.X.Id == x.Id).OrderBy(a => a.Y.Id).ToList();

            Queue <Arc> coadaArce = new Queue <Arc>(arceDinX);
            Arc         arc       = coadaArce.Dequeue();

            bool esteReetichetat = false;

            do
            {
                if (arc.EsteAdmisibil(retea))
                {
                    arc.Flux += Math.Min(x.GetExces(retea), arc.GetCapacitateReziduala(retea));

                    Arc arcInvers = retea.Arce.FirstOrDefault(a => a.X.Id == arc.Y.Id && a.Y.Id == arc.X.Id);

                    if (arcInvers == null)
                    {
                        arcInvers = new Arc(arc.Y, arc.X, 0, 0);
                        retea.Arce.Add(arcInvers);
                    }

                    if (!_L.Contains(arc.Y) && !arc.Y.EsteNodSursa && !arc.Y.EsteNodStoc)
                    {
                        _L.Enqueue(arc.Y);
                    }
                }

                if (x.GetExces(retea) > 0)
                {
                    if (coadaArce.Count() > 0)
                    {
                        arc = coadaArce.Dequeue();
                    }
                    else
                    {
                        x.Distanta = arceDinX.Where(a => a.GetCapacitateReziduala(retea) > 0).Min(a => a.Y.Distanta + 1);

                        esteReetichetat = true;
                    }
                }

                Console.WriteLine(retea.ToString());

                Console.Write("Distante:");
                retea.Noduri.Select(n => n.Distanta).ToList().ForEach(d => Console.Write($"{d}, "));
                Console.WriteLine("\n");
            } while (x.GetExces(retea) != 0 && !esteReetichetat);

            if (x.GetExces(retea) > 0)
            {
                _L.Enqueue(x);
            }
        }
Exemplo n.º 3
0
        //private void Marire(Retea retea)
        //{
        //   Console.WriteLine("\n\n");
        //   Console.WriteLine(retea.ToString());

        //   Drum drum = CreeazaDrum(retea);
        //   Console.WriteLine(drum.AsString(retea));

        //   drum.Arce.ForEach(arc => arc.Flux += drum.GetCapacitateRezidualaMin(retea));

        //   //todo: check if arcs are updated by reference
        //}

        private void Marire(Retea retea)
        {
            //Console.WriteLine("\n\n");
            //Console.WriteLine(retea.ToString());

            Drum drum = CreeazaDrum(retea);
            //Console.WriteLine(drum.AsString(retea));


            double r = drum.GetCapacitateRezidualaMin(retea);

            Console.WriteLine($"{drum.AsString(retea)} | r(D)={r}");
            if (r > 0)
            {
                drum.Arce.ForEach(arc => {
                    arc.Flux += r;

                    Arc arcRetea   = retea.Arce.FirstOrDefault(a => a.X.Id == arc.X.Id && a.Y.Id == arc.Y.Id);
                    arcRetea.Flux += r;

                    Arc arcInvers = retea.Arce.FirstOrDefault(a => a.X.Id == arc.Y.Id && a.Y.Id == arc.X.Id);

                    if (arcInvers == null)
                    {
                        arcInvers = new Arc(arc.Y, arc.X, 0, 0);
                        retea.Arce.Add(arcInvers);
                    }
                    else
                    {
                        if (arcInvers.GetCapacitateReziduala(retea) <= 0)
                        {
                            retea.Arce.Remove(arcInvers);
                        }
                    }

                    if (arcRetea.GetCapacitateReziduala(retea) <= 0)
                    {
                        retea.Arce.Remove(arcRetea);
                    }
                });

                Console.WriteLine("\n\n");
                Console.WriteLine(retea.ToString());
                Console.WriteLine($"Max value: {retea.GetValoareMaxima()}");
            }

            //todo: check if arcs are updated by reference
        }
        private void Marire(Retea retea)
        {
            Console.WriteLine("\n\n");
            Console.WriteLine($"MARIRE FLUX NR.{_nrIteratie}: ");

            _nrIteratie++;

            // Predecesori
            List <Nod> predecesori = new List <Nod>();

            retea.Noduri.ForEach(n => predecesori.Add(n.Predecesor));

            Console.Write("Predecesori: ");
            predecesori.ForEach(p => Console.Write($"{((p != null) ? p.ToString() : "-")}, "));

            // Creare drum
            Drum   drum  = CreeazaDrum(retea);
            double rDrum = drum.GetCapacitateRezidualaMin(retea);

            Console.WriteLine($"{drum.AsString(retea)} | r(D)={rDrum}");
            Console.WriteLine();

            if (rDrum > 0)
            {
                drum.Arce.ForEach(arc => {
                    arc.Flux += rDrum;

                    Arc arcInvers = retea.Arce.FirstOrDefault(a => a.X.Id == arc.Y.Id && a.Y.Id == arc.X.Id);

                    if (arcInvers == null)
                    {
                        arcInvers = new Arc(arc.Y, arc.X, 0, 0);
                        retea.Arce.Add(arcInvers);
                    }
                });

                Console.WriteLine(retea.ToString());
                Console.WriteLine($"Max value: {retea.GetValoareMaxima()}");
            }
        }
Exemplo n.º 5
0
        private void Initializare(Retea retea)
        {
            _nodSursa = retea.Noduri.FirstOrDefault(nod => nod.EsteNodSursa);
            _nodStoc  = retea.Noduri.FirstOrDefault(nod => nod.EsteNodStoc);

            retea.Arce.ForEach(arc => arc.Flux = 0);

            _L = new Queue <Nod>();
            SetDistances(retea, _nodStoc);

            retea.Arce.Where(a => a.X.Id == _nodSursa.Id).ToList()
            .ForEach(arc => {
                arc.Flux += arc.GetCapacitateReziduala(retea);

                Arc arcInvers = retea.Arce.FirstOrDefault(a => a.X.Id == arc.Y.Id && a.Y.Id == arc.X.Id);

                if (arcInvers == null)
                {
                    arcInvers = new Arc(arc.Y, arc.X, 0, 0);
                    retea.Arce.Add(arcInvers);
                }

                if (arc.Y.GetExces(retea) > 0 && arc.Y.Id != _nodStoc.Id)
                {
                    _L.Enqueue(arc.Y);
                }
            });

            _nodSursa.Distanta = retea.Noduri.Count();

            Console.WriteLine("\nRetea initializata in Preflux FIFO");
            Console.WriteLine(retea.ToString());

            Console.Write("Distante: ");
            retea.Noduri.Select(n => n.Distanta).ToList().ForEach(d => Console.Write($"{d}, "));

            Console.WriteLine("\n");
        }