Advertisement
Guest User

Prim

a guest
Jan 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Prim {
  4.     private static final class GNode implements Comparable<GNode> {
  5.         // unique id of the node
  6.         int id;
  7.         // map of child nodes and their respective distance from current node 'id'
  8.         Map<GNode, Integer> children = new HashMap<GNode, Integer>();
  9.         // used in future computation, to store minimal/optimal updated distance
  10.         int distFromParent = 10000;
  11.         GNode parent = null;
  12.  
  13.         public GNode(int i, int dist) {
  14.             this.id=i;
  15.             this.distFromParent = dist;
  16.         }
  17.  
  18.         public GNode(int i) {
  19.             this.id=i;
  20.         }
  21.  
  22.         @Override
  23.         public int compareTo(GNode o) {
  24.             if (this.distFromParent < o.distFromParent) {
  25.                 return -1;
  26.             }
  27.  
  28.             if (this.distFromParent > o.distFromParent) {
  29.                 return 1;
  30.             }
  31.  
  32.             return 0;
  33.         }
  34.  
  35.         @Override
  36.         public String toString() {
  37.             return "GNode [id=" + id + ", distFromParent=" + distFromParent
  38.                     + "]";
  39.         }
  40.     }
  41.  
  42.     public static class GNodeComparator implements Comparator<GNode>
  43.     {
  44.         @Override
  45.         public int compare(GNode x, GNode y)
  46.         {
  47.             //return x.distFromParent - y.distFromParent;
  48.             // Assume neither string is null. Real code should
  49.             // probably be more robust
  50.             // You could also just return x.length() - y.length(),
  51.             // which would be more efficient.
  52.             if (x.distFromParent < y.distFromParent)
  53.             {
  54.                 return -1;
  55.             }
  56.             if (x.distFromParent > y.distFromParent)
  57.             {
  58.                 return 1;
  59.             }
  60.             return 0;
  61.         }
  62.     }
  63.  
  64.  
  65.     static String mstPrim(GNode[] nodes) {
  66.         PriorityQueue<GNode> pq = new PriorityQueue<GNode>();
  67.         //boolean[] visited = new boolean[nodes.length];
  68.         //boolean[] exited = new boolean[nodes.length];
  69.         String res = "";
  70.         nodes[0].distFromParent = 0;
  71.         for (int i = 0; i < nodes.length; i++) {
  72.             pq.add(nodes[i]);
  73.         }
  74.         long sum = 0;
  75.         int count = 0;
  76.         while (!pq.isEmpty()) {
  77.             for (GNode n : pq) {
  78.                 System.out.print(" " + n);
  79.             }
  80.             System.out.println();
  81.             GNode u = pq.poll();
  82.             for (GNode v : u.children.keySet()) {
  83.                 if (pq.contains(v) && u.children.get(v) < v.distFromParent) {
  84.                     v.distFromParent = u.children.get(v);
  85.                     v.parent = u;
  86.                 }
  87.             }
  88.             sum += u.distFromParent;
  89.             if (!res.isEmpty()) res += " + ";
  90.             if (count > 0) res += u.distFromParent;
  91.             count++;
  92.         }
  93.  
  94.         /*visited[0] = true;
  95.         long sum = 0;
  96.         int count = 0;
  97.         while (pq.size() > 0) {
  98.             for (GNode o :
  99.                     pq) {
  100.                 System.out.print(o + " ");
  101.             }
  102.             System.out.println();
  103.             GNode o = pq.poll();
  104.             if (!exited[o.id]) {
  105.                 for (GNode n : o.children.keySet()) {
  106.                     if (!exited[n.id]) {
  107.                         if (visited[n.id]) {
  108.                             if (n.distFromParent > o.children.get(n)) {
  109.                                 n.distFromParent = o.children.get(n);
  110.                             }
  111.                         } else {
  112.                             visited[n.id] = true;
  113.                             n.distFromParent = o.children.get(n);
  114.                             //pq.add(n);
  115.                         }
  116.                     }
  117.                 }
  118.                 sum += o.distFromParent;
  119.                 if (!res.isEmpty()) res += " + ";
  120.                 if (count > 0) res += o.distFromParent;
  121.                 exited[o.id] = true;
  122.                 count++;
  123.             }
  124.            /* if (pq.size() == 0) {
  125.                 for (int i = 0; i < nodes.length; i++) {
  126.                     if (!exited[i]) {
  127.                         System.out.println(nodes[i]);
  128.                         pq.add(nodes[i]);
  129.                     }
  130.                 }
  131.             }
  132.         }*/
  133.         return res + " = " + sum;
  134.     }
  135.  
  136.     public static void start(int[] graph) {
  137.         //Scanner in = new Scanner(System.in);
  138.         //System.out.println("Enter graph:");
  139.         int V = graph[0];
  140.         int E = graph[1];
  141.         int count = 2;
  142.         int u, v, w;
  143.         GNode[] nodes = new GNode[V];
  144.         nodes[0] = new GNode(0);
  145.         for (int i = 0; i < E; i++) {
  146.             u = graph[count];
  147.             count++;
  148.             v = graph[count];
  149.             count++;
  150.             GNode un = nodes[u];
  151.             GNode vn = nodes[v];
  152.             if (un == null) {
  153.                 un = new GNode(u);
  154.                 nodes[u] = un;
  155.             }
  156.             if (vn == null) {
  157.                 vn = new GNode(v);
  158.                 nodes[v] = vn;
  159.             }
  160.  
  161.             w = graph[count];
  162.             count++;
  163.  
  164.             if (!(un.children.containsKey(vn) && un.children.get(vn) <= w))
  165.                 un.children.put(vn, w);
  166.             if (!(vn.children.containsKey(un) && vn.children.get(un) <= w))
  167.                 vn.children.put(un, w);
  168.         }
  169.         String len = mstPrim(nodes);
  170.         System.out.println("Min length (P): " + len);
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement