Advertisement
Guest User

stocking

a guest
Jun 9th, 2011
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. public Iterable<Vertice<V>> buscarCamino(Vertice<V> origen,
  2.         Vertice<V> destino) {
  3.     TDALista<Vertice<V>> l = new TDALista<Vertice<V>>();
  4.  
  5.     for (Vertice<V> v : vertices()) {
  6.         try {
  7.             visitados.put(v, false);
  8.         } catch (InvalidKeyException e) {
  9.         }
  10.     }
  11.  
  12.     buscarCamino(origen, destino, l);
  13.  
  14.     return l;
  15. }
  16.  
  17. // COPIAR (Es como un DFS)
  18. private boolean buscarCamino(Vertice<V> origen, Vertice<V> destino,
  19.         TDALista<Vertice<V>> l) {
  20.     try {
  21.         l.addLast(origen);
  22.         visitados.put(origen, true);
  23.         if (origen == destino) {
  24.             return true;
  25.         } else {
  26.             for (Arco<A> a : incidentEdges(origen)) {
  27.                 Vertice<V> vecino = opposite(origen, a);
  28.                 if (!visitados.get(vecino)) {
  29.                     boolean encontre = buscarCamino(vecino, destino, l);
  30.                     if (encontre)
  31.                         return true;
  32.                 }
  33.             }
  34.             // Si ando por aca ya a partir del origen no llego a destino
  35.             // Borro el ultimo que agrege a la lista!
  36.             l.remove(l.last());
  37.             return false;
  38.         }
  39.     } catch (InvalidKeyException e) {
  40.     } catch (InvalidPositionException e) {
  41.     } catch (EmptyListException e) {
  42.     }
  43.  
  44.     return false;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement