Advertisement
heavenriver

2011_03_04.java (ex. 3)

Jan 10th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. // Esame del 4 marzo 2011, esercizio 3
  2. // Ricordarsi di specificare sempre, nei commenti al codice, l'utilità di ogni metodo.
  3. // Per le implementazioni standard di un grafo riferirsi a GraphImplementations.java
  4.  
  5. class Vertex is Integer;
  6. class Edge has double weight;
  7. class Graph has Edge[][] adjMatrix;
  8.  
  9. public List<Integer> searchPath(int src, int dest, double maxWeight)
  10.     {
  11.     LinkedList<LinkedList<Integer>> config = new LinkedList<LinkedList<Integer>>(); // Memorizza le configurazioni che portano all'ultimo nodo della lista-nella-lista
  12.     HashSet<Integer> visited = new HashSet<Integer>();
  13.     visited.put(src);
  14.     List<Integer> path = new LinkedList<Integer>();
  15.     path.add(src);
  16.     config.add(path);
  17.     while(!config.isEmpty() && visited.size() < vertices.size())
  18.         {
  19.         List<Integer> pathfinder = config.poll();
  20.         Integer v = pathfinder.peekLast();
  21.         if(v == dest) return pathfinder; // Se l'ultimo nodo è il nodo destinazione, restituisce la lista
  22.         for(int i = 0; i < adjMatrix.length; i++)
  23.             {
  24.             if(adjMatrix[v][i] != null)
  25.                 {
  26.                 Integer w = opposite(adjMatrix[v][i], v);
  27.                 if(!visited.contains(w))
  28.                     {
  29.                     visited.put(w);
  30.                     List<Integer> p2 = pathfinder.clone(); // Aggiunge una nuova configurazione all'insieme per ogni arco incidente
  31.                     p2.add(w);
  32.                     config.add(p2);
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.     return null;
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement