0% encontró este documento útil (0 votos)
510 vistas1 página

GRAFOS en C#

Este documento describe una clase Grafos que implementa un grafo no dirigido mediante una matriz de adyacencia. La clase incluye métodos para insertar y eliminar vértices y aristas, comprobar si el grafo está vacío o lleno, buscar un vértice, y mostrar el grafo en una cuadrícula de datos. También se describe una clase Form1 que usa un objeto Grafos para mostrar un grafo en una interfaz gráfica y permitir la inserción y eliminación de vértices y aristas mediante botones.

Cargado por

Mamani Rider
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOCX, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
510 vistas1 página

GRAFOS en C#

Este documento describe una clase Grafos que implementa un grafo no dirigido mediante una matriz de adyacencia. La clase incluye métodos para insertar y eliminar vértices y aristas, comprobar si el grafo está vacío o lleno, buscar un vértice, y mostrar el grafo en una cuadrícula de datos. También se describe una clase Form1 que usa un objeto Grafos para mostrar un grafo en una interfaz gráfica y permitir la inserción y eliminación de vértices y aristas mediante botones.

Cargado por

Mamani Rider
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOCX, PDF, TXT o lee en línea desde Scribd

GRAFOS

(Grafos.cs)
namespace Estructuras
{ class Grafos ……………………………………………………………Form1.cs…………………………………………………………………………….
{ private Boolean Vacio; private Boolean Llena;
private Boolean BuscarVertice; private Boolean InsertarArco; namespace Estructuras
{
const int max = 10; private int cant; private int cursor; public partial class Form1 : Form
private String[] vertice = new String[max]; {
private int[,] matrizarcos = new int[max, max]; Grafos grafo1=new Grafos();
public Form1()
public Grafos() { InitializeComponent();
{ Vacio = false; Llena = false; BuscarVertice = false; }
cant = 0; cursor = 0; private void button1_Click(object sender, EventArgs e)
} { grafo1.InsertarVerticeM(textBox1.Text);
private void VacioM() grafo1.MostrarGrafo(dataGridView1);
{ if (cant == 0) }
Vacio = true; private void button2_Click(object sender, EventArgs e)
Else Vacio = false; { grafo1.EliminarVertice(textBox1.Text);
} grafo1.MostrarGrafo(dataGridView1);
private void LlenaM() }
{ if (cant == max) private void button4_Click(object sender, EventArgs e)
Llena = true; { grafo1.InsertarArcoM(textBox2.Text, textBox3.Text);
Else Llena = false; grafo1.MostrarGrafo(dataGridView1);
} }
private void BuscarVerticeM(String v) private void button5_Click(object sender, EventArgs e)
{ Boolean exito; int i; exito = false; {
for (i = 1; i <= cant; i++) grafo1.EliminarArco(textBox2.Text, textBox3.Text);
if (vertice[i] == v) grafo1.MostrarGrafo(dataGridView1);
{ exito = true; cursor = i; break; }
} private void Form1_Load(object sender, EventArgs e)
BuscarVertice = exito; { }
} private void label1_Click(object sender, EventArgs e)
public void InsertarVerticeM(String v) { }
{ LlenaM(); private void textBox1_TextChanged(object sender, EventArgs e)
if (Llena) { }
{ MessageBox.Show("Grafo lleno"); } private void label4_Click(object sender, EventArgs e)
else { }
{ BuscarVerticeM(v); private void textBox4_TextChanged(object sender, EventArgs e)
if (BuscarVertice) { }
MessageBox.Show("El vertice ya existe");
else
{ cant++; vertice[cant] = v; }
for (byte j = 1; j <= cant; j++) }
{ matrizarcos[cant, j] = 0;
matrizarcos[j, cant] = 0;
} -------------------La clase Principal---------------------------
}
}
} namespace Estructuras
public void InsertarArcoM(String x, String y) {
{ Boolean exito; int aux1; int aux2; exito = false; static class Program
BuscarVerticeM(x); {
if (BuscarVertice) /// <summary>
{ aux1 = cursor; /// Punto de entrada principal para la aplicación.
BuscarVerticeM(y); /// </summary>
if (BuscarVertice) [STAThread]
{ aux2 = cursor; matrizarcos[aux1, aux2] = 1; static void Main()
exito = true; {
} Application.EnableVisualStyles();
} Application.SetCompatibleTextRenderingDefault(false);
InsertarArco = exito; Application.Run(new Form1());
} }
public void EliminarArco(String x, String y) }
{ int aux1; int aux2; }
BuscarVerticeM(x);
if (BuscarVertice)
{ aux1 = cursor; BuscarVerticeM(y);
if (BuscarVertice)
{ aux2 = cursor; matrizarcos[aux1, aux2] = 0; }
}
}
public void EliminarVertice(String v)
{ int p;
VacioM();
if (Vacio)
MessageBox.Show("No hay vertice para poder eliminar");
else
{ BuscarVerticeM(v);
p = cursor;
for (int i = 1; i <= cant; i++)
for (int j = 1; j <= cant; j++)
{ if (i >= p)
{ matrizarcos[i, j] = matrizarcos[i + 1, j];
vertice[i] = vertice[i + 1];
}
}
for (int i = 1; i <= cant; i++)
for (int j = 1; j <= cant; j++)
if (j >= p)
matrizarcos[i, j] = matrizarcos[i, j + 1];
cant = cant - 1;
}
}
public void MostrarGrafo(DataGridView s)
{
s.RowCount = cant + 1;
s.ColumnCount = cant + 1;
for (int i = 1; i <= cant; i++)
for (int j = 1; j <= cant; j++)
{
s.Rows[i].Cells[j].Value = Convert.ToString(matrizarcos[i, j]);
s.Rows[i].Cells[0].Value = vertice[i];
s.Rows[0].Cells[i].Value = vertice[i];
}
}
public void getproxvertice(String v)
{ int aux; BuscarVerticeM(v);
if (BuscarVertice)
{ aux = cursor; vertice[aux] = vertice[aux + 1];
}
else
MessageBox.Show("El vertice no existe");

}
}

GRAFOS 
(Grafos.cs) 
namespace Estructuras 
{  class Grafos 
    {   private Boolean Vacio; private Boolean Llena; 
        p

También podría gustarte