Advertisement
Guest User

esercitazione 21 maggio

a guest
May 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class GraphServices {
  4.  
  5.    
  6.     public static <V> void bfs(Graph<V> g) {
  7.         Collection<Node<V>> nodi_grafo = new LinkedList<>();
  8.         nodi_grafo = g.getNodes();
  9.         for (Node<V> s : nodi_grafo) {bfsAux(g, s);}
  10.        
  11.         return;
  12.     }
  13.  
  14.     private static <V> void bfsAux(Graph<V> g, Node<V> s) {
  15.         ArrayDeque<Node<V>> livello = new ArrayDeque<>();
  16.         if (s.stato != Node.Stato.EXPLORED) {
  17.             s.stato = Node.Stato.EXPLORED;
  18.             System.out.println(s);
  19.         }
  20.        
  21.         livello.addLast(s);
  22.        
  23.         while (!livello.isEmpty()) {
  24.             ArrayDeque<Node<V>> nuovoLivello = new ArrayDeque<>();
  25.             for (Node<V> u : livello) {
  26.                 for (Edge<V> e : g.getOutEdges(u)) {
  27.                     Node<V> v = e.getTarget();
  28.                     if (v.stato != Node.Stato.EXPLORED) {
  29.                         v.stato = Node.Stato.EXPLORED;
  30.                         System.out.println(v.getElement());
  31.                         nuovoLivello.addLast(v);                       
  32.                     }
  33.                 }
  34.             }
  35.             livello = nuovoLivello;
  36.         }
  37.         return;
  38.     }
  39.    
  40.     public static <V> void sssp(Graph<V> g, Node<V> source) {
  41.  
  42.     }
  43.    
  44.     public static <V> void mst(Graph<V> G) {
  45.        
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement