Advertisement
Guest User

stocking

a guest
Jun 9th, 2011
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. public Iterable<Vertice<V>> caminoMasCorto(Vertice<V> v1, Vertice<V> v2) {
  2.     try {
  3.         // Primero desvisito todos los vertices
  4.         for (Vertice<V> v : vertices())
  5.             visitados.put(v, false);
  6.  
  7.         TDACola<Vertice<V>> c = new TDACola<Vertice<V>>();
  8.         c.enqueue(v1);
  9.         visitados.put(v1, true);
  10.  
  11.         // <Actual, Padre>
  12.         TDAMapeo<Vertice<V>, Vertice<V>> historial = new TDAMapeo<Vertice<V>, Vertice<V>>(
  13.                 new Comp());
  14.         historial.put(v1, null);
  15.  
  16.         while (!c.isEmpty()) {
  17.             Vertice<V> w = c.dequeue();
  18.  
  19.             // Si quiero sin direcciones simplemente hago incidentEdges
  20.             for (Arco<A> a : incidentEdges(w)) {
  21.                 Vertice<V> ww = opposite(w, a);
  22.                 if (!visitados.get(ww)) {
  23.                     visitados.put(ww, true);
  24.                     c.enqueue(ww);
  25.                     historial.put(ww, w);
  26.  
  27.                     // visito ww
  28.                     if (ww == v2) {
  29.                         TDALista<Vertice<V>> camino = new TDALista<Vertice<V>>();
  30.                         while (ww != null) {
  31.                             camino.addFirst(ww);
  32.                             ww = historial.get(ww);
  33.                         }
  34.                         return camino;
  35.                     }
  36.                 }
  37.             }
  38.  
  39.         }
  40.     } catch (EmptyQueueException e) {
  41.     } catch (InvalidPositionException e) {
  42.     } catch (InvalidKeyException e) {
  43.     }
  44.  
  45.     return null;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement