Advertisement
heavenriver

2011_02_07.java (ex. 3)

Jan 8th, 2013
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. // Esame del 7 febbraio 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. public Graph chiusuraTransitiva()
  6.     {
  7.     Graph out = this.clone();
  8.     HashMap<Object, Integer> visited = new HashMap<Object, Integer>(); // Memorizza sia i Vertex che gli Edge visitati, ergo Object
  9.     int iter = 0;
  10.     for(Vertex<V> v : vertices)
  11.         {
  12.         DFS(v, out, visited, iter);
  13.         iter++;
  14.         }
  15.     return out;
  16.     }
  17.  
  18. public void DFS(Vertex v, Graph out, HashMap<Object, Integer> visited, int iter)
  19.     {
  20.     visited.put(v, iter);
  21.     for(Edge e : incidentEdges(v))
  22.         {
  23.         if(!visited.containsKey(e))
  24.             {
  25.             Vertex w = opposite(e, v);
  26.             if(!visited.containsKey(w)) // Controlla che w non sia stato già visitato
  27.                 {
  28.                 visited.put(e, iter); // Se non lo è stato, viene visitato
  29.                 DFS(w, out, visited, iter);
  30.                 }
  31.             else
  32.                 {
  33.                 for(Object o : visited.keySet()) // Se lo è stato, vengono esaminati tutti i nodi visitati in questa DFS e collegati al nodo corrente per realizzare la chiusura
  34.                     {
  35.                     if(o instanceof Vertex)
  36.                         {
  37.                         Vertex x = (Vertex)o;
  38.                         if(visited.get(x) == visited.get(w) && x != w && !areAdjacent(w, x))
  39.                             insertEdge(w, x);
  40.                         }
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement