public Grafo(List <Universal> listavuelos) { this.listavuelos = listavuelos; listaVertices = new List <char>(); for (int i = 0; i < listavuelos.Count; i++) { if (!listaVertices.Contains(listavuelos[i].getOrigen())) { listaVertices.Add(listavuelos[i].getOrigen()); } if (!listaVertices.Contains(listavuelos[i].getDestino())) { listaVertices.Add(listavuelos[i].getDestino()); } } for (int c = 0; c < listaVertices.Count; c++) { ListaAristas listaAristas = new ListaAristas(); for (int k = 0; k < listavuelos.Count; k++) { if (listaVertices[c] == listavuelos[k].getOrigen()) { Arista a = new Arista(listavuelos[k].getCosto(), listavuelos[k].getTiempo(), listavuelos[k].getOrigen(), listavuelos[k].getDestino()); listaAristas.Add(a); } } Vertice v = new Vertice(listaVertices[c], listaAristas); this.Add(v); } }
private Arista SeleccionPrim(List <Arista> Cand, Grafo P) { Vertice AuxO, AuxD; Arista AuxA = new Arista(); bool bandera = false; do { foreach (Arista A in Cand) { if (BuscarVertice(A.getOrigen()) != -1) { AuxO = this[BuscarVertice(A.getOrigen())]; } else { AuxO = new Vertice(); } if (BuscarVertice(A.getDestino()) != -1) { AuxD = this[BuscarVertice(A.getDestino())]; } else { AuxD = new Vertice(); } if (P.Contains(AuxO)) { if (!P.Contains(AuxD)) { AuxA = A; bandera = true; break; } } } if (!bandera) { foreach (Vertice V in this) { if (!P.Contains(V)) { P.Add(V); break; } } } } while (!bandera); Cand.Remove(AuxA); return(AuxA); }
public List <Arista> newPrim(Vertice Origen) { Grafo P = new Grafo(); //List<Vertice> P = new List<Vertice>(); P.Add(Origen); List <Arista> Cand = new List <Arista>(); List <Arista> Prometedor = new List <Arista>(); Arista NoDirigido; foreach (Vertice V in this) { foreach (Arista A in V.getListaAristas()) { Cand.Add(A); NoDirigido = new Arista(A.getCosto(), A.getTiempo(), A.getOrigen(), A.getDestino()); Cand.Add(NoDirigido); } } Arista Ar; List <Arista> LA = new List <Arista>(); do { Ar = SeleccionPrim(Cand, P);//truena Prometedor.Add(Ar); Console.WriteLine("Works " + Ar.getOrigen().ToString()); if (BuscarVertice(Ar.getOrigen()) != -1) { Vertice Nuevo = this[this.BuscarVertice(Ar.getDestino())]; P.Add(Nuevo); } else { Vertice Nuevo = this[this.BuscarVertice(Ar.getOrigen())]; P.Add(Nuevo); } } while (P.Count <= this.Count - 1); return(Prometedor); }
public void insertarArista(int costo, int tiempo, char origen, char destino) { bool agregable = true; for (int i = 0; i < this.Count; i++) { if (this[i].getOrigen() == origen) { foreach (Arista la in this[i].listaA) { if (la.getDestino() == destino) { agregable = false; } } if (agregable) { Arista a = new Arista(costo, tiempo, origen, destino); this[i].listaA.Add(a); break; } } } }
private void prim() { listBoxRecorrido.Items.Clear(); Font letra = new Font("Arial", 10); SolidBrush color = new SolidBrush(Color.Black); Color newColor = Color.FromArgb(75, Color.Red); Pen lapiz = new Pen(newColor, 8); int ponderacionTotal = 0; int indice; indice = comboOrigenes.SelectedIndex; Console.WriteLine("Seleccionado=" + graph[indice].getOrigen()); Vertice origen = graph[indice]; Vertice aux = origen; List <Vertice> aceptados = new List <Vertice>(); List <char> visitados = new List <char>(); // ordenarAdyacencia(graph); if (origen.getListaAristas().Count > 0) { Arista menor = origen.getListaAristas()[0]; List <Arista> ArcosUsados = new List <Arista>(); List <string> conjuntos = new List <string>(); List <Vertice> ListPrim = new List <Vertice>(); int cont = 0; int cont2 = 1; bool band1, band2, band4; int cont3 = 0; do { band1 = band2 = true; ListaAristas la = new ListaAristas(); //no estoy pasando lista aristas en blanco? foreach (Arista a in aux.getListaAristas()) { if (!ArcosUsados.Contains(a)) { Vertice NV = new Vertice(aux.getOrigen(), la); NV.getListaAristas().Add(a);//agregado extra NV.setX(aux.getX()); NV.setY(aux.getY()); NV.getListaAristas().Add(a); ListPrim.Add(NV); } } foreach (Vertice n in ListPrim) { if (menor.getCosto() >= n.getListaAristas()[0].getCosto() || ArcosUsados.Contains(menor)) { menor = n.getListaAristas()[0]; aux = n; } } foreach (String a in conjuntos) { if (Convert.ToString(aux.getOrigen()) == a) { band1 = false; } if (Convert.ToString(menor.getDestino()) == a) //checar aqui { band2 = false; } } if (band1 == true || band2 == true) { Vertice NV = aux; aceptados.Add(NV); visitados.Add(NV.getOrigen()); if (band1 == true) { conjuntos.Add(Convert.ToString(aux.getOrigen())); } if (band2 == true) { conjuntos.Add(Convert.ToString(aux.getListaAristas()[0].getDestino())); } ArcosUsados.Add(menor); } if (band1 == false && band2 == false) { ArcosUsados.Add(menor); } ListPrim.Remove(aux); Console.WriteLine("Menor= " + menor.getDestino()); //aux = menor; //aqui es el pedo foreach (Vertice v in graph) { if (v.getOrigen() == menor.getOrigen()) { foreach (Vertice v2 in graph) { if (v2.getOrigen() == v.getListaAristas()[0].getDestino()) { Console.WriteLine("Aux antes = " + aux.getOrigen()); aux = v2; Console.WriteLine("Aux despues= " + aux.getOrigen()); } } } } cont++; }while (ArcosUsados.Count < graph.Count()); } Console.WriteLine("LISTA DE ACEPTADOS"); foreach (Vertice a in aceptados) { Console.WriteLine(a.getOrigen()); } listBoxRecorrido.Items.Clear(); for (int i = 0; i < graph.Count - 1; i++) { for (int j = 0; j < graph[i].getListaAristas().Count; j++) { for (int k = 0; k < aceptados.Count; k++) { if (graph[i].getOrigen() == aceptados[k].getOrigen() && graph[i].getListaAristas()[j].getDestino() == aceptados[k].getListaAristas()[0].getDestino()) { panelGrafo.CreateGraphics().DrawLine(lapiz, graph[i].getX(), graph[i].getY(), graph[i].getListaAristas()[j].getX(), graph[i].getListaAristas()[j].getY()); listBoxRecorrido.Items.Add(graph[i].getOrigen().ToString() + graph[i].getListaAristas()[j].getDestino().ToString()); } } } } letras(); letreroARM.Text = "Peso total con Prim: " + ponderacionTotal; letreroARM.Visible = Enabled; }//fin