Example #1
0
        private void seleccionKruskal(Arista candidato, List <string> componentes, ListaArista arbolRM)
        {
            int    borrar = -1;
            string origen, destino;

            origen  = candidato.getOrigin().getCity().getName();
            destino = candidato.getDestination().getCity().getName();
            for (int i = 0; i < componentes.Count; i++)
            {
                if (componentes[i].Contains(origen))
                {
                    for (int j = 0; j < componentes.Count; j++)
                    {
                        if (componentes[j].Contains(destino))
                        {
                            if (i != j)
                            {
                                borrar          = j;
                                componentes[i] += componentes[j];
                                arbolRM.Add(candidato);
                            }
                        }
                    }
                }
            }
            if (borrar > -1)
            {
                componentes.RemoveAt(borrar);
            }
        }
Example #2
0
        public void kruskal()
        {
            ListaArista   candidatos  = new ListaArista();
            ListaArista   arbolRM     = new ListaArista();
            List <string> componentes = new List <string>();

            inicializaComponentes(componentes);
            inicializaCandidatos(candidatos, 1);
            candidatos.quickSort(0, candidatos.Count - 1);

            foreach (Arista candidato in candidatos)
            {
                seleccionKruskal(candidato, componentes, arbolRM);
                if (arbolRM.existence(candidato))
                {
                    Console.WriteLine(candidato.getOrigin().getCity().getName() +
                                      "->" + candidato.getDestination().getCity().getName() + " "
                                      + candidato.getCost().ToString() + " SI");
                }
                else
                {
                    Console.WriteLine(candidato.getOrigin().getCity().getName() +
                                      "->" + candidato.getDestination().getCity().getName() + " "
                                      + candidato.getCost().ToString() + " NO");
                }
            }
            Console.WriteLine("");
            dibujaARM(arbolRM);
        }
Example #3
0
        public void prim()
        {
            ListaArista   candidatos  = new ListaArista();
            ListaArista   arbolRM     = new ListaArista();
            List <string> componentes = new List <string>();
            string        origen      = labelCity.Text;

            inicializaComponentes(componentes);
            inicializaCandidatos(candidatos, 2);
            candidatos.quickSort(0, candidatos.Count - 1);

            seleccionPrim(candidatos, origen, arbolRM);
            dibujaARM(arbolRM);
        }
Example #4
0
        private void dibujaARM(ListaArista arbolRM)
        {
            Pen pen2 = new Pen(Color.Green, 3);
            AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);

            pen2.CustomEndCap = bigArrow;
            int X, Y, J, K;

            cost = 0;
            foreach (Arista arista in arbolRM)
            {
                cost += arista.getCost();
                X     = arista.getOrigin().getCity().getX() + 10;
                Y     = arista.getOrigin().getCity().getY() + 10;
                J     = arista.getDestination().getCity().getX() + 10;
                K     = arista.getDestination().getCity().getY() + 10;
                panelMap.CreateGraphics().DrawLine(pen2, X, Y, J, K);
            }
        }
Example #5
0
 private void inicializaCandidatos(ListaArista candidatos, int option)
 {
     foreach (Node node in graph.getNodeList())
     {
         foreach (Adjacent ady in node.getAdjacentList())
         {
             Arista arista = new Arista();
             arista.setOrigin(node);
             arista.setDestination(ady.getNode());
             if (option == 1)
             {
                 arista.setCost(ady.getTime());
             }
             else if (option == 2)
             {
                 arista.setCost(ady.getCost());
             }
             candidatos.Add(arista);
         }
     }
 }
Example #6
0
        private void seleccionPrim(ListaArista candidatos, string origenPrim, ListaArista arbolRM)
        {
            bool   repetir;
            string origen, destino;

            while (origenPrim.Length < graph.getNodeList().Count)
            {
                repetir = false;
                for (int i = 0; i < candidatos.Count; i++)
                {
                    origen  = candidatos[i].getOrigin().getCity().getName();
                    destino = candidatos[i].getDestination().getCity().getName();

                    if (origenPrim.Contains(origen))
                    {
                        if (!origenPrim.Contains(destino))
                        {
                            arbolRM.Add(candidatos[i]);
                            int cost = candidatos[i].getCost();
                            Console.WriteLine(origen + "->" + destino + " " + cost.ToString() + " SI");
                            origenPrim += destino;
                            candidatos.RemoveAt(i);
                            repetir = true;
                            break;
                        }
                    }
                    if (origenPrim.Contains(destino))
                    {
                        if (!origenPrim.Contains(origen))
                        {
                            arbolRM.Add(candidatos[i]);
                            int cost = candidatos[i].getCost();
                            Console.WriteLine(origen + "->" + destino + " " + cost.ToString() + " SI");
                            origenPrim += origen;
                            candidatos.RemoveAt(i);
                            repetir = true;
                            break;
                        }
                    }
                    if (origenPrim.Contains(origen))
                    {
                        if (origenPrim.Contains(destino))
                        {
                            candidatos.RemoveAt(i);
                            repetir = true;
                            break;
                        }
                    }
                    if (origenPrim.Contains(destino))
                    {
                        if (origenPrim.Contains(origen))
                        {
                            candidatos.RemoveAt(i);
                            repetir = true;
                            break;
                        }
                    }
                }
                if (!repetir)
                {
                    for (int i = 0; i < candidatos.Count; i++)
                    {
                        origen  = candidatos[i].getOrigin().getCity().getName();
                        destino = candidatos[i].getDestination().getCity().getName();

                        if (!origenPrim.Contains(origen))
                        {
                            if (!origenPrim.Contains(destino))
                            {
                                arbolRM.Add(candidatos[i]);
                                int cost = candidatos[i].getCost();
                                Console.WriteLine(origen + "->" + destino + " " + cost.ToString() + " SI");
                                origenPrim += origen;
                                origenPrim += destino;
                                candidatos.RemoveAt(i);
                                repetir = true;
                                break;
                            }
                        }
                        if (!origenPrim.Contains(destino))
                        {
                            if (!origenPrim.Contains(origen))
                            {
                                arbolRM.Add(candidatos[i]);
                                int cost = candidatos[i].getCost();
                                Console.WriteLine(origen + "->" + destino + " " + cost.ToString() + " SI");
                                origenPrim += destino;
                                origenPrim += origen;
                                candidatos.RemoveAt(i);
                                repetir = true;
                                break;
                            }
                        }
                    }
                }
            }

            Console.WriteLine("");
        }