Advertisement
alexarcan

ADA_LAB4!!!!

Mar 12th, 2015
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package graphalgo;
  2.  
  3. import graphs.Edge;
  4. import graphs.ISimpleGraph;
  5. import graphs.UndirectedGraph;
  6.  
  7. import java.util.HashSet;
  8. import java.util.Set;
  9.  
  10. import priorityqueues.MyPriorityQueue;
  11. import priorityqueues.PriorityQueueImpl;
  12.  
  13. public class PrimMSTFinderWithPriorityQueue implements IMSTFinder {
  14.     private double Q[];
  15.     private boolean mark[];
  16.     private Edge[] parent;
  17.     private int N;
  18.  
  19.    
  20.     public Set<Edge> FindMST(UndirectedGraph g) {
  21.        
  22. //      System.out.println("PrimMSTFinderWithPriorityQueue - not yet implemented");
  23.             //return null;
  24.             N = g.getNrNodes();
  25.        
  26.             mark = new boolean[N];
  27.             PriorityQueueImpl Q = new PriorityQueueImpl(N);
  28.            
  29.  
  30.  
  31.             parent = new Edge[N];
  32.             for (int v = 0; v < N; v++) {
  33.                 parent[v] = null;
  34.                 //Q[v] = Double.POSITIVE_INFINITY;
  35.                 mark[v] = false;
  36.             }
  37.  
  38.             int root=0; // any node can be picked as root      
  39.             doPrim(g, root);
  40.  
  41.             Set<Edge> result = new HashSet<Edge>();
  42.             for (int v = 0; v < N; v++) {
  43.                 if (parent[v] != null) {
  44.                     result.add(parent[v]);
  45.                     }
  46.         }
  47.             return result;
  48.     }
  49.    
  50.     private int extractMin() {     
  51.         double valmin = Double.POSITIVE_INFINITY;
  52.         int indmin = -1;
  53.         for (int i = 0; i < N; i++)
  54.             if (!mark[i]){  // select the minimum distance node, from the
  55.                             // nodes that are still not marked
  56.                 if (Q[i] <= valmin) { // compares for smaller OR EQUAL
  57.                                          // to handle unconnected graphs
  58.                     valmin = Q[i];
  59.                     indmin = i;
  60.                 }
  61.             }
  62.         mark[indmin]=true;
  63.         Q[indmin]=0;
  64.         return indmin;
  65.     }  
  66. private void doPrim(ISimpleGraph g1, Integer root){
  67.    
  68.     Q[root] = 0;
  69.     int iter=0;
  70.     while  (iter<N){ // iterate until all N nodes are added to the MST
  71.         iter++;
  72.        
  73.         int u = extractMin();
  74.        
  75.         for (Edge e : g1.edgesOutgoingFrom(u)) {
  76.             int v = e.other(u);
  77.             if (Q[v] > e.weight()) {
  78.                 parent[v] = e;
  79.                 Q[v]= e.weight();
  80. }
  81.    
  82.  
  83.             }}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement