Advertisement
heavenriver

2012_06_18.java (ex. 3)

Jan 8th, 2013
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. // Esame del 18 giugno 2012, 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 has String nick; double x; double y; Object profile; and get methods
  6. class Edge has double distance;
  7.  
  8. public Collection<Vertex> introduction(String nick)
  9.     {
  10.     HashSet<Vertex> people = new HashSet<Vertex>();
  11.     for(Vertex v : vertices)
  12.         {
  13.         if(v.getNick().equals(nick))
  14.             DFS(v, people, new HashSet<Vertex>());
  15.         }
  16.     return people;
  17.     }
  18.  
  19. public void DFS(Vertex v, HashSet<Vertex> people, HashSet<Vertex> visited)
  20.     {
  21.     visited.put(v);
  22.     for(Edge e : incidentEdges(v))
  23.         {
  24.         Vertex w = opposite(e, v);
  25.         if(!visited.contains(w))
  26.             {
  27.             people.put(w);
  28.             DFS(w, people, visited);
  29.             }
  30.         }
  31.     }
  32.  
  33. public Collection<Vertex> compatible(String nick, double dist)
  34.     {
  35.     HashSet<Vertex> compatible = new HashSet<Vertex>();
  36.     for(Vertex v : vertices)
  37.         {
  38.         if(v.getNick().equals(nick))
  39.             DFS_C(v, nick, dist, compatible, new HashSet<Vertex>());
  40.         }
  41.     return compatible;
  42.     }
  43.  
  44. public void DFS_C(Vertex v, String nick, double dist, HashSet<Vertex> compatible, HashSet<Vertex> visited)
  45.     {
  46.     visited.put(v);
  47.     for(Edge e : incidentEdges(v))
  48.         {
  49.         Vertex w = opposite(v);
  50.         if(!visited.contains(w))
  51.             {
  52.             if(compatibili(nick, w.getNick()) && distanzaIstantanea(nick, w.getNick()) <= dist)
  53.                 compatible.put(w);
  54.             DFS_C(w, nick, dist, compatible, visited);
  55.             }
  56.         }
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement