Beispiel #1
0
        public void KruskalMST(Grafo g)
        {
            Grafo       Aux   = new Grafo();
            List <Arco> Arcos = new List <Arco>();
            List <Arco> RelacionesArbolKruskal = new List <Arco>();

            foreach (Nodo n in g.Nodos)
            {
                foreach (Arco r in n.Relaciones)
                {
                    Arcos.Add(r);
                }
            }
            Arcos = Arcos.OrderBy(x => x.Peso).ToList();
            string CadAux = "Relaciones Ordenadas por el peso\n";

            for (int i = 0; i < g.Nodos.Count; i++)
            {
                bool Ciclo         = false;
                Arco RelacionMenor = BuscaRelacionMenorKruskal(Arcos);
                for (int j = 0; j < Aux.Nodos.Count; j++)
                {
                    if (GrafoContieneCiclo(j, j, Aux.GeneraMatrizAdyacencia(), Aux.Nodos.Count))
                    {
                        Ciclo = true;
                        break;
                    }
                }
                if (!Ciclo)
                {
                    if (!ConfirmaExistenciaNodosEnKruskal(Aux, RelacionMenor.Origen))
                    {
                        Nodo Nuevo = new Nodo();
                        Nuevo.Identificador = RelacionMenor.Origen;
                        Aux.AgregarNodo(Nuevo);
                    }
                    else if (!ConfirmaExistenciaNodosEnKruskal(Aux, RelacionMenor.Destino))
                    {
                        Nodo Nuevo = new Nodo();
                        Nuevo.Identificador = RelacionMenor.Destino;
                        Aux.AgregarNodo(Nuevo);
                    }
                    int IndiceNodoCrearRelacion   = ObtenIndiceNodo(Aux, RelacionMenor.Origen);
                    int IndiceNodoRelacionDestino = ObtenIndiceNodo(Aux, RelacionMenor.Destino);
                    if (IndiceNodoCrearRelacion != -1)
                    {
                        if (Aux.Nodos[IndiceNodoCrearRelacion].TieneRelacion(0))
                        {
                        }
                        Aux.Nodos[IndiceNodoCrearRelacion].AñadirRelacion(RelacionMenor.Destino, RelacionMenor.Peso);
                    }
                }
            }

            /*foreach(Arco a in Arcos)
             * {
             *  CadAux += "Origen: " + a.Origen + " , Destino: " + a.Destino + " , Peso: " + a.Peso + "\n";
             * }*/
            MessageBox.Show(CadAux);
        }
Beispiel #2
0
        //Algoritmo Matriz Permutada
        public bool IsomorfismoMatrizPermutada(Grafo g1, Grafo g2)
        {
            NumPermutacionTranspuesta = 0;
            SaveFileDialog VentanaGuardar = new SaveFileDialog();

            MessageBox.Show("Por favor selecciona donde guardar el resultado");
            if (VentanaGuardar.ShowDialog() == DialogResult.OK)
            {
                RutaResultado = VentanaGuardar.FileName + ".txt";
            }
            int[,] A = g1.GeneraMatrizAdyacencia();
            int[,] B = g2.GeneraMatrizAdyacencia();
            int[,] MatrizPermutacion = new int[g1.Nodos.Count, g2.Nodos.Count];
            int[,] MatrizTranspuesta = new int[g1.Nodos.Count, g2.Nodos.Count];
            int count = g1.Nodos.Count;

            StreamWriter Escritor = new StreamWriter(RutaResultado);
            List <int[]> pesos    = CalculaPesosMatriz(A, count);
            List <int[]> pesos2   = CalculaPesosMatriz(B, count);
            List <int>   f        = pesos[0].ToList();
            List <int>   g        = pesos2[0].ToList();
            List <int>   mper     = new List <int>();

            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < count; j++)
                {
                    MatrizPermutacion[i, j] = 0;
                    MatrizTranspuesta[i, j] = 0;
                }
            }

            for (int i = 0; i < f.Count; i++)
            {
                for (int j = 0; j < g.Count; j++)
                {
                    if (f[i] == g[j])
                    {
                        if (!mper.Contains(j))
                        {
                            MatrizPermutacion[i, j] = 1;
                            MatrizTranspuesta[j, i] = 1;
                            mper.Add(j);
                            break;
                        }
                    }
                }
            }

            Escritor.WriteLine("\t\t\t\t Matriz Permutación ");
            Escritor.Write(FormateaMatrizaCadena(MatrizPermutacion));
            Escritor.WriteLine("\t\t\t\t Matriz Transpuesta");
            Escritor.Write(FormateaMatrizaCadena(MatrizTranspuesta));
            return(CreaPermutaciones(MatrizPermutacion, MatrizTranspuesta, A, B, count, f, Escritor));
        }
        private void AdyacenciaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Formato = "Matriz Adyacencia \n";

            int[,] MatrizAd = grafo.GeneraMatrizAdyacencia();
            for (int i = 0; i < MatrizAd.GetLength(0); i++)
            {
                for (int j = 0; j < MatrizAd.GetLength(1); j++)
                {
                    Formato += MatrizAd[i, j].ToString();
                    Formato += "   ";
                }
                Formato += "\n";
            }
            MessageBox.Show(Formato);
        }
 private void algoritmoFuerzaBrutaToolStripMenuItem_Click(object sender, EventArgs e)
 {
     grafo2 = VentanaIsomorfismo.grafo;
     if (grafo2.Nodos.Count > 0)
     {
         if ((grafo.ObtenNumeroRelaciones() == grafo2.ObtenNumeroRelaciones()) && (grafo.Nodos.Count == grafo2.Nodos.Count))
         {
             bool Respuesta = Algoritmos.FuerzaBruta(grafo.GeneraMatrizAdyacencia(), grafo2.GeneraMatrizAdyacencia(), grafo.Nodos.Count);
             if (Respuesta)
             {
                 MessageBox.Show("Los grafos son isomorficos entre si según el algoritmo de fuerza bruta");
             }
             else
             {
                 MessageBox.Show("Los grafos no son isomorficos entre si según el algoritmo de fuerza bruta");
             }
         }
         else
         {
             MessageBox.Show("El número de relaciones o el número de nodos de los 2 grafos no son iguales");
         }
     }
     else
     {
         MessageBox.Show("El grafo no ha sido cargado");
     }
 }
Beispiel #5
0
        public bool AlgoritmoManualBotello(Grafo grafo1, Grafo grafo2)
        {
            SaveFileDialog VentanaGuardar  = new SaveFileDialog();
            string         CadenaAEscribir = "";
            string         Ruta            = "";
            int            SumaColumna     = 0;

            int[,] MatrizU = grafo1.GeneraMatrizAdyacencia();
            int[,] MatrizV = grafo2.GeneraMatrizAdyacencia();
            List <int> ArregloGradosSalidaColumna;

            VentanaGuardar.Filter = "Texto (*.txt)|*.txt";
            MessageBox.Show("Por favor selecciona donde guardar el resultado");
            if (VentanaGuardar.ShowDialog() == DialogResult.OK)
            {
                Ruta = VentanaGuardar.FileName;
            }

            if ((grafo1.Nodos.Count == grafo2.Nodos.Count) && (grafo1.ObtenNumeroRelaciones() == grafo2.ObtenNumeroRelaciones())) // Si tenemos el mismo número de relaciones y nodos
            {
                for (int i = 0; i < grafo1.Nodos.Count - 1; i++)
                {
                    ArregloGradosSalidaColumna = new List <int>();
                    SumaColumna = grafo1.ObtenGradoEntradaNodo(grafo1.Nodos[i]);
                    for (int j = 0; j < grafo1.Nodos.Count; j++)
                    {
                        if (MatrizU[j, i] == 1)
                        {
                            int NumAuxGrados = 0;
                            for (int z = 0; z < grafo1.Nodos.Count; z++)
                            {
                                NumAuxGrados += MatrizU[z, j];
                            }
                            ArregloGradosSalidaColumna.Add(NumAuxGrados);
                        }
                    }
                    int ColSim = BuscaColumnaSimilar(SumaColumna, i + 1, MatrizV, grafo2, ArregloGradosSalidaColumna);
                    if (ColSim != 0)
                    {
                        CadenaAEscribir += "Comparando con la columna: " + (i + 1) + " de la matriz 1 \n" + "Columna Similar encontrada: " + (ColSim + 1) + " en la matriz 2 " + "\n";
                        intercambiaColumnna(i, ColSim, MatrizV, grafo1.Nodos.Count);
                        intercambiaRenglon(i, ColSim, MatrizV, grafo1.Nodos.Count);
                        CadenaAEscribir += "\nNueva Matriz: \n" + FormateaMatrizaCadena(MatrizV) + "\n";
                        if (ComparaMatrices(MatrizU, MatrizV, grafo1.Nodos.Count))
                        {
                            CadenaAEscribir += "\n\nLos grafos son isomorfos";
                            using (StreamWriter Escritor = new StreamWriter(Ruta))
                            {
                                Escritor.WriteLine(CadenaAEscribir);
                            }
                            return(true);
                        }
                    }
                    else
                    {
                        CadenaAEscribir += "No se encontaron coincidencias con la columna: " + (i + 1) + " de la matriz 1\n";
                    }
                }
            }
            else
            {
                MessageBox.Show("Los grafos no tienen el mismo número de nodos o el mismo número de relaciones!!!!");
            }
            CadenaAEscribir += "\n Los grafos no son isomorfos";
            using (StreamWriter Escritor = new StreamWriter(Ruta))
            {
                Escritor.WriteLine(CadenaAEscribir);
            }
            return(false);
        }