public int getYde(CNodoVertice cnv) { List <int> yes = new List <int>(); int ymin = -1; foreach (CArista a in arcos_arbol) { CVertice or = a.getVOrigen(), de = a.getVDestino(); int num_or = or.getNumero(), num_de = de.getNumero(); if (or.getId() == cnv.getVertice().getId() && num_or < num_de) { if (de.getBajo() != 0) { yes.Add(de.getBajo()); } } else if (de.getId() == cnv.getVertice().getId() && num_de < num_or) { if (or.getBajo() != 0) { yes.Add(or.getBajo()); } } if (yes.Count != 0) { yes.Sort(); ymin = yes[0]; } } return(ymin); }
public int getZde(CNodoVertice cnv) { List <int> zetas; int zmin = -1; if (arcos_arbol.Count != 0 && arcos_retroceso.Count != 0) { zetas = new List <int>(); foreach (CArista a in arcos_retroceso) { if (a.getVOrigen().getId() == cnv.getVertice().getId()) { zetas.Add(a.getVDestino().getNumero()); } else if (a.getVDestino().getId() == cnv.getVertice().getId()) { zetas.Add(a.getVOrigen().getNumero()); } } if (zetas.Count != 0) { zetas.Sort(); zmin = zetas[0]; } } return(zmin); }
public void RP_RPA(CNodoVertice cnv) { cnv.getVertice().setVisitado(true); foreach (CNodoVertice w in cnv.getRelaciones()) { if (w.getVertice().getVisitado() != true) { RP_RPA(w); } } cnv.getVertice().setBajo(minimo(cnv.getVertice().getNumero(), getZde(cnv), getYde(cnv))); }
public void RP_R(CNodoVertice cnv, ref int ndes) { cnv.getVertice().setVisitado(true); cnv.getVertice().setNumero(++num); foreach (CNodoVertice w in cnv.getRelaciones()) { if (w.getVertice().getVisitado() != true) { int nd = 0; RP_R(w, ref nd); descendientes[G.getListaAdyacencia().IndexOf(w)] = nd; ndes += nd + 1; enlistaArcosArbol(cnv.getVertice(), w.getVertice()); } } }
private void conteoDeCaminosToolStripMenuItem_Click(object sender, EventArgs e) { if (grafos != null && grafo_activo != null) { if (grafo_activo.getNumeroVertices() >= 2) { DConteo dc = new DConteo(grafo_activo.getNumeroVertices()); if (dc.ShowDialog() == DialogResult.OK) { CNodoVertice cnv1 = grafo_activo.getListaAdyacencia()[(dc.na) - 1], cnv2 = grafo_activo.getListaAdyacencia()[(dc.nb) - 1]; int num_cam = grafo_activo.calculaCaminosREntre(cnv1, cnv2, dc.r), ncam = dc.r; if (cnv1.getVertice().getId() != cnv2.getVertice().getId()) { MessageBox.Show(" Existen " + num_cam.ToString() + " caminos de longitud " + ncam.ToString() + " entre el vértice " + cnv1.getVertice().getId().ToString() + " al vértice " + cnv2.getVertice().getId().ToString() + " ", "Conteo de caminos", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(" Existen " + num_cam.ToString() + " circuitos de longitud " + ncam.ToString() + " para el vértice " + cnv1.getVertice().getId().ToString() + " ", "Conteo de circuitos", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { MessageBox.Show(" Deben existir por lo menos 2 vertices en el grafo! ", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public bool tieneHijosConBajoMayorOIgualAlNumDe(CNodoVertice cnv) { foreach (CArista a in arcos_arbol) { CVertice or = a.getVOrigen(), de = a.getVDestino(); int num_or = or.getNumero(), num_de = de.getNumero(); if (or.getId() == cnv.getVertice().getId() && num_or < num_de && de.getBajo() >= cnv.getVertice().getNumero()) { return(true); } else if (de.getId() == cnv.getVertice().getId() && num_de < num_or && or.getBajo() >= cnv.getVertice().getNumero()) { return(true); } } return(false); }
public static bool hayVerticeGradoCero(CNodoVertice cnv) { if (cnv.getVertice().getGrado() == 0) { return(true); } else { return(false); } } //Delegado para List<CNodoVertice>.Exist
}//Construye la matriz a partir de la lista de adyacencia del grafo public bool esIncidenteCon(CArista ari, CNodoVertice nodo_od) { bool encontrado = false; if (ari.getVOrigen().getId() == nodo_od.getVertice().getId() || ari.getVDestino().getId() == nodo_od.getVertice().getId()) { encontrado = true; } return(encontrado); } //Verifica que existe incidencia en lista de adyacencia
public void RecorridoEnProfundidad(CNodoVertice cnvid) { if (cnvid.getVertice().getVisitado() != true) { int nd = 0; RP_R(cnvid, ref nd); descendientes[G.getListaAdyacencia().IndexOf(cnvid)] = nd; } foreach (CNodoVertice cnv in G.getListaAdyacencia()) { if (cnv.getVertice().getId() != cnvid.getVertice().getId()) { if (cnv.getVertice().getVisitado() != true) { int nd = 0; RP_R(cnv, ref nd); descendientes[G.getListaAdyacencia().IndexOf(cnv)] = nd; } } } }
public bool laRaizTiene2OMasHijos(CNodoVertice cnv_ini) { int num_hijos = 0; foreach (CArista a in arcos_arbol) { CVertice or = a.getVOrigen(), de = a.getVDestino(); int num_or = or.getNumero(), num_de = de.getNumero(); if ((or.getId() == cnv_ini.getVertice().getId() && num_or < num_de) || (de.getId() == cnv_ini.getVertice().getId() && num_de < num_or)) { num_hijos++; } } if (num_hijos >= 2) { return(true); } return(false); }
public CNodoVertice getCNVdeNumeroMasAltoEnG() { CNodoVertice mayor = G.getListaAdyacencia()[0]; foreach (CNodoVertice cnv in G.getListaAdyacencia()) { if (cnv.getVertice().getNumero() > mayor.getVertice().getNumero()) { mayor = cnv; } } return(mayor); }
public void CF_R(CNodoVertice cnv, ref List <int> ar, int vuelta) { cnv.getVertice().setVisitado(true); foreach (CNodoVertice w in cnv.getRelaciones()) { if (w.getVertice().getVisitado() != true) { ar.Add(w.getVertice().getId()); CF_R(w, ref ar, vuelta); } } if (vuelta == 1) { cnv.getVertice().setNumero(++num); } else { cnv.getVertice().setNumero(++num2); } }
} //Construye la matriz a partir de la lista de adyacencia del grafo public bool estaEnListaRelaciones(CNodoVertice nodo, CNodoVertice relacion) { bool encontrado = false; foreach (CNodoVertice cnv in nodo.getRelaciones()) { if (relacion.getVertice().getId() == cnv.getVertice().getId()) { encontrado = true; break; } } return(encontrado); } //Verifica que existe relacion en lista de adyacencia
//Puntos de articulacion public void RecorridoEnProfundidadPtosArticulacion(CNodoVertice cnvid) { foreach (CNodoVertice cv in G.getListaAdyacencia()) { cv.getVertice().setVisitado(false); } if (cnvid.getVertice().getVisitado() != true) { RP_RPA(cnvid); } foreach (CNodoVertice cnv in G.getListaAdyacencia()) { if (cnv.getVertice().getId() != cnvid.getVertice().getId()) { if (cnv.getVertice().getVisitado() != true) { RP_RPA(cnv); } } } }
//Otros de componentes fuertes public List <CNodoVertice> getListaNuevaDe(CNodoVertice nv) { List <CNodoVertice> listanueva = new List <CNodoVertice>(); foreach (CNodoVertice cnv in G.getListaAdyacencia()) { foreach (CNodoVertice rel in cnv.getRelaciones()) { if (rel.getVertice().getId() == nv.getVertice().getId()) { listanueva.Add(getCNVenGR(cnv.getVertice().getId())); } } } return(listanueva); }
public void sacaPuntosDeArticulacion(CNodoVertice cnv_ini) { List <CNodoVertice> puntos_articulacion = new List <CNodoVertice>(); if (laRaizTiene2OMasHijos(cnv_ini)) { puntos_articulacion.Add(cnv_ini); } foreach (CNodoVertice cnv in G.getListaAdyacencia()) { if (cnv.getVertice().getId() != cnv_ini.getVertice().getId() && tieneHijosConBajoMayorOIgualAlNumDe(cnv)) { puntos_articulacion.Add(cnv); } } if (puntos_articulacion.Count != 0) { string cad = "{ "; foreach (CNodoVertice p in puntos_articulacion) { p.getVertice().setRelleno(Color.LightPink.ToArgb()); p.getVertice().dibujate(Graphics.FromImage(G.getBMP()), G.getBMP(), tp); cad += "(" + p.getVertice().getId().ToString() + ")"; if (puntos_articulacion.IndexOf(p) != puntos_articulacion.Count - 1) { cad += ", "; } } cad += " }. "; MessageBox.Show(" Vértices Cut : " + cad, "Puntos de articulación", MessageBoxButtons.OK, MessageBoxIcon.Information); foreach (CNodoVertice p in puntos_articulacion) { p.getVertice().borrate(Graphics.FromImage(G.getBMP()), G.getBMP(), tp); p.getVertice().setRelleno(Color.LightGoldenrodYellow.ToArgb()); p.getVertice().dibujate(Graphics.FromImage(G.getBMP()), G.getBMP(), tp); } } else { MessageBox.Show(" No existen vértices CUT para el Grafo " + G.getId().ToString() + ". ", "Puntos de articulación", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public void calculaCaminoMasCorto(CNodoVertice origen) { List <CNodoVertice> S = new List <CNodoVertice>(); List <CNodoVertice> P = new List <CNodoVertice>(); List <CNodoVertice> VS; int[] D = new int[n]; S.Add(origen); for (int i = 0; i < n; i++) { D[i] = C[V.IndexOf(origen), i]; P.Add(origen); } for (int j = 0; j < n - 1; j++) { VS = VminusS(S); CNodoVertice w = menorVminusS(D, VS); S.Add(w); VS = VminusS(S); foreach (CNodoVertice cnv in VS) { if (G.sonVerticesAdyacentes(cnv.getVertice(), w.getVertice())) { int aux = D[V.IndexOf(cnv)]; if (C[V.IndexOf(w), V.IndexOf(cnv)] == INFINITO) { C[V.IndexOf(w), V.IndexOf(cnv)] -= D[V.IndexOf(w)]; } D[V.IndexOf(cnv)] = minimoDe(D[V.IndexOf(cnv)], D[V.IndexOf(w)] + C[V.IndexOf(w), V.IndexOf(cnv)]); if (aux != D[V.IndexOf(cnv)]) { P[V.IndexOf(cnv)] = w; } } } } muestraResultado(D, P, origen); }
//Recorrido en Amplitud public void recorridoEnAmplitud(CNodoVertice vert_ini) { vert_ini.getVertice().setVisitado(true); List <CNodoVertice> Vp = construyeLAdyPrima(G.getListaAdyacencia(), vert_ini); List <CNodoVertice> Q = new List <CNodoVertice>(); Q.Add(vert_ini); while (Vp.Count != 0) { CNodoVertice u = Q[0]; Q.RemoveAt(0); foreach (CNodoVertice w in u.getRelaciones()) { if (!w.getVertice().getVisitado()) { w.getVertice().setVisitado(true); Q.Add(w); enlistaArcosArbol(u.getVertice(), w.getVertice()); Vp.Remove(w); } } } }
public void muestraResultado(int[] pesos_totales, List <CNodoVertice> caminos, CNodoVertice origen) { string camaux = ""; DataTable dt = new DataTable(); object[] values = new object[3]; int ind = 0; List <int> camino = new List <int>(); bool sincamino = false; dt.Columns.Add("Destino"); dt.Columns.Add("Camino"); dt.Columns.Add("Peso Total"); for (int i = 0; i < pesos_totales.Length; i++) { ind = i; values[0] = V[i].getVertice().getId().ToString(); if (pesos_totales[i] != INFINITO && pesos_totales[i] >= 0) { values[2] = " " + pesos_totales[i].ToString(); } else { sincamino = true; values[2] = " - "; } while (origen.getVertice().getId() != caminos[ind].getVertice().getId()) { camino.Add(caminos[ind].getVertice().getId()); ind = V.IndexOf(caminos[ind]); } camaux += origen.getVertice().getId().ToString() + ", "; for (int y = camino.Count - 1; y >= 0; y--) { camaux += camino[y].ToString() + ", "; } camaux += V[i].getVertice().getId().ToString(); if (sincamino) { values[1] = " No existe"; sincamino = false; } else { values[1] = camaux; } dt.Rows.Add(values); camaux = ""; camino.Clear(); } DDijkstra dij = new DDijkstra(dt, origen.getVertice().getId()); dij.ShowDialog(); }
public int ordenaPorNumero(CNodoVertice nv1, CNodoVertice nv2) { return(nv2.getVertice().getNumero().CompareTo(nv1.getVertice().getNumero())); }
public int ordenaPorID(CNodoVertice nv1, CNodoVertice nv2) { return(nv1.getVertice().getId().CompareTo(nv2.getVertice().getId())); }