Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Iterable<Vertice<V>> caminoMasCorto(Vertice<V> v1, Vertice<V> v2) {
- try {
- // Primero desvisito todos los vertices
- for (Vertice<V> v : vertices())
- visitados.put(v, false);
- TDACola<Vertice<V>> c = new TDACola<Vertice<V>>();
- c.enqueue(v1);
- visitados.put(v1, true);
- // <Actual, Padre>
- TDAMapeo<Vertice<V>, Vertice<V>> historial = new TDAMapeo<Vertice<V>, Vertice<V>>(
- new Comp());
- historial.put(v1, null);
- while (!c.isEmpty()) {
- Vertice<V> w = c.dequeue();
- // Si quiero sin direcciones simplemente hago incidentEdges
- for (Arco<A> a : incidentEdges(w)) {
- Vertice<V> ww = opposite(w, a);
- if (!visitados.get(ww)) {
- visitados.put(ww, true);
- c.enqueue(ww);
- historial.put(ww, w);
- // visito ww
- if (ww == v2) {
- TDALista<Vertice<V>> camino = new TDALista<Vertice<V>>();
- while (ww != null) {
- camino.addFirst(ww);
- ww = historial.get(ww);
- }
- return camino;
- }
- }
- }
- }
- } catch (EmptyQueueException e) {
- } catch (InvalidPositionException e) {
- } catch (InvalidKeyException e) {
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement