public void DFS(Stack <Vertice> Pila, List <Vertice> Visitados, List <Vertice> Camino) { Vertice v_act = Pila.Pop(); foreach (Arista a in v_act.getLa()) { bool Band = false; foreach (Vertice v in Visitados) { if (v.getId() == a.getVertex().getId()) { Band = true; break; } } if (Band == false) { if (ArmPrim == true) { foreach (Vertice Ref in ArbolPrim.getLv()) { if (a.getVertex().getId() == Ref.getId()) { Visitados.Add(a.getVertex()); Pila.Push(Ref); Camino.Add(a.getVertex()); DFS(Pila, Visitados, Camino); Camino.Add(a.getVertexOrigen()); } } } if (ArmKruskal == true) { foreach (Vertice Ref in ArbolKruskal.getLv()) { if (a.getVertex().getId() == Ref.getId()) { Visitados.Add(a.getVertex()); Pila.Push(Ref); Camino.Add(a.getVertex()); DFS(Pila, Visitados, Camino); Camino.Add(a.getVertexOrigen()); } } } } } }
private void BtnAgentePrim_Click(object sender, EventArgs e) { ArmPrim = true; ArmKruskal = false; pictureBox1.BackgroundImage = ARM; pictureBox1.BackgroundImageLayout = ImageLayout.Zoom; List <Vertice> Visitados = new List <Vertice>(); Stack <Vertice> Pila = new Stack <Vertice>(); CaminoPrim = new List <Vertice>(); int Buscado = comboBox1.SelectedIndex + 1; foreach (Vertice v in ArbolPrim.getLv()) { if (Buscado == v.getId()) { Pila.Push(v); Visitados.Add(v); CaminoPrim.Add(v); break; } } DFS(Pila, Visitados, CaminoPrim); while (Visitados.Count != ArbolPrim.getLv().Count) { foreach (Vertice v in ArbolPrim.getLv()) { bool Band = false; foreach (Vertice Bus in Visitados) { if (v.getId() == Bus.getId()) { Band = true; break; } } if (Band == false) { Visitados.Add(v); Pila.Push(v); CaminoPrim.Add(v); DFS(Pila, Visitados, CaminoPrim); } } } Graphics graphics = Graphics.FromImage(copia); LimpiarBitmap(copia, Color.Transparent); pictureBox1.Image = copia; BrochaAgente = new SolidBrush(Color.Red); List <Point> Linea = new List <Point>(); Point punto = new Point(); int b, c; listBox1.Items.Clear(); LlenalistBox(); listBox1.Update(); Agente agente = new Agente(CaminoPrim[0], 1, CaminoPrim); while (true) { b = agente.GetcontAristas(); bool Encontrado = false; foreach (Arista a in agente.getCamino()[b].getLa()) { if (b < CaminoPrim.Count) { if (a.getVertex().getId() == agente.getCamino()[b + 1].getId()) { Linea = a.getLinea(); Encontrado = true; } } } if (Encontrado == false) { agente.SetcontPunto(Linea.Count - 5); } c = agente.GetcontPuntos(); if (Linea.Count - 5 <= c) { c = Linea.Count - 1; } punto = Linea[c]; pictureBox1.Refresh(); LimpiarBitmap(copia, Color.Transparent); DibujarCirculo(punto, BrochaAgente, copia); DibujarId(punto, drawBrush, copia, agente.getId()); agente.SetcontPunto(agente.GetcontPuntos() + 7); if (agente.GetcontPuntos() >= Linea.Count) { agente.SetcontPunto(0); agente.SetcontAristas(agente.GetcontAristas() + 1); } if (agente.GetcontAristas() + 1 == CaminoPrim.Count) { DibujarCirculo(punto, BrochaAgente, copia); break; } } pictureBox1.Refresh(); }