Exemplo n.º 1
0
        public int componentes()
        {
            List <List <NodoP> > componentes = new List <List <NodoP> >();
            List <NodoP>         nue         = new List <NodoP>();
            Grafo aux  = new Grafo(grafo);
            bool  enco = false;

            foreach (NodoP nod in grafo)
            {
                foreach (List <NodoP> n in componentes)
                {
                    if (enco == false)
                    {
                        if (n.Find(delegate(NodoP f) { if (f.NOMBRE == nod.NOMBRE)
                                                       {
                                                           return(true);
                                                       }
                                                       else
                                                       {
                                                           return(false);
                                                       } }) != null)
                        {
                            enco = true;
                        }
                    }
                }
                if (enco == false)
                {
                    nue = new List <NodoP>();
                    grafo.Componentes2(nod, nue);
                    componentes.Add(nue);
                }
                enco = false;
            }
            foreach (NodoP re in grafo)
            {
                foreach (NodoRel rela in re.relaciones)
                {
                    rela.VISITADA = false;
                }
            }
            return(componentes.Count);
        }
Exemplo n.º 2
0
        public Form3(Grafo gra, int tip)
        {
            tipo  = tip;
            grafo = new Grafo();
            bool enco = false;

            InitializeComponent();
            g = CreateGraphics();
            Relaciones.Select();
            band   = false;
            accion = 0;
            // label2.Visible = Componentes.Visible = false;
            grafo         = gra;
            AutoScroll    = true;
            Vertices.Text = grafo.Count().ToString();
            Aristas.Text  = grafo.Aristas.Count().ToString();
            if (grafo.Aristas.Count > 0)
            {
                if (grafo.Aristas.ToArray()[0].Tipo == 2)
                {
                    Grado.Text = ((grafo.Aristas.Count()) * 2).ToString();
                }
                else
                {
                    Grado.Text = ((grafo.Aristas.Count())).ToString();
                }
            }


            List <List <NodoP> > componentes = new List <List <NodoP> >();
            List <NodoP>         nue         = new List <NodoP>();

            if (tipo == 2)
            {
                Text = "Grafo - Propiedades (No Dirigido)";
                foreach (NodoP nod in grafo)
                {
                    foreach (List <NodoP> n in componentes)
                    {
                        if (enco == false)
                        {
                            if (n.Find(delegate(NodoP f) { if (f.NOMBRE == nod.NOMBRE)
                                                           {
                                                               return(true);
                                                           }
                                                           else
                                                           {
                                                               return(false);
                                                           } }) != null)
                            {
                                enco = true;
                            }
                        }
                    }
                    if (enco == false)
                    {
                        nue = new List <NodoP>();
                        grafo.Componentes2(nod, nue);
                        componentes.Add(nue);
                    }
                    enco = false;
                }
                Componentes.Visible = true;
                //label2.Visible = true;
            }

            label6.Visible  = true;
            label7.Visible  = true;
            Interno.Visible = true;
            Externo.Visible = true;
            Interno.Items.Clear();
            Externo.Items.Clear();
            foreach (NodoP nodo in grafo)
            {
                Interno.Items.Add(nodo.NOMBRE.ToString());
                Externo.Items.Add(nodo.NOMBRE.ToString());
            }

            foreach (NodoP re in grafo)
            {
                foreach (NodoRel rela in re.relaciones)
                {
                    rela.VISITADA = false;
                }
            }
            Componentes.Text          = componentes.Count.ToString();
            dataGridView1.ColumnCount = grafo.Count + 1;
            dataGridView1.RowCount    = grafo.Count + 1;

            grafo.CreaMatriz();
            //MessageBox.Show(grafo.Aristas[1].PESO.ToString());
            for (int i = 0; i <= grafo.Count; i++)
            {
                for (int j = 0; j <= grafo.Count; j++)
                {
                    if (i == 0 && j > 0)
                    {
                        dataGridView1.Rows[i].Cells[j].Value = grafo[j - 1].NOMBRE.ToString();
                    }
                    else
                    if (j == 0 && i > 0)
                    {
                        dataGridView1.Rows[i].Cells[j].Value = grafo[i - 1].NOMBRE.ToString();
                    }
                    else
                    if (i != 0 && j != 0)
                    {
                        dataGridView1.Rows[i].Cells[j].Value = grafo.MATRIZ[i - 1][j - 1].ToString();
                    }
                }
            }
        }