Advertisement
teleias

Prims with PriortyQueue and Without

Apr 30th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Prims {
  4.     public static void main(String[] args)
  5.     {
  6.         new Prims();
  7.     }
  8.     int[] pointsArr;
  9.     public Prims()
  10.     {
  11.         List<Node> allNodes = new ArrayList<Node>();
  12.         ///Set up the tree
  13.         {
  14.             int nPoints = Integer.parseInt(input[0]);
  15.             int nVertices = Integer.parseInt(input[1]);
  16.             for(int i = 0; i < nPoints; i++)
  17.                 allNodes.add(new Node(i));
  18.             for(int i = 2; i < input.length; i++)
  19.             {
  20.                 String[] arr = input[i].split(" ");
  21.                 int a = Integer.parseInt(arr[0]);
  22.                 int b = Integer.parseInt(arr[1]);
  23.                 float weight = Float.parseFloat(arr[2]);
  24.                 {
  25.                     Node aNode = allNodes.get(a);
  26.                     Node bNode = allNodes.get(b);
  27.                     aNode.addVertex(new Vertex(aNode, bNode, weight));
  28.                     bNode.addVertex(new Vertex(bNode, aNode, weight));
  29.                 }
  30.             }
  31.         }
  32.         ///Set up the tree with scanner
  33.         {
  34.             int nPoints = Integer.parseInt(scan.nextLine());
  35.             int nVertices = Integer.parseInt(scan.nextLine());
  36.             for(int i = 0; i < nPoints; i++)
  37.                 allNodes.add(new Node(i));
  38.             for(int i = 2; i < nVertices+2; i++)
  39.             {
  40.                 String[] arr = scan.nextLine().split(" ");
  41.                 int a = Integer.parseInt(arr[0]);
  42.                 int b = Integer.parseInt(arr[1]);
  43.                 float weight = Float.parseFloat(arr[2]);
  44.                 {
  45.                     Node aNode = allNodes.get(a);
  46.                     Node bNode = allNodes.get(b);
  47.                     aNode.addVertex(new Vertex(aNode, bNode, weight));
  48.                     bNode.addVertex(new Vertex(bNode, aNode, weight));
  49.                 }
  50.             }
  51.         }
  52.         ///
  53.        
  54.         {
  55.             List<Node> openNodes = new ArrayList<Node>();
  56.             List<Node> adjNodes = new ArrayList<Node>();
  57.             openNodes.add(allNodes.get(0));
  58.             while(openNodes.size() != allNodes.size())
  59.             {
  60.                 Vertex smallestAdjVertex = null;
  61.                 ///Of all the openNodes
  62.                 for(Node openNode : openNodes)
  63.                 {
  64.                     ///Find the smallest vertex
  65.                     for(Vertex adjVertex : openNode.vertices)
  66.                     {
  67.                         ///If openNodes doesn't contain the TO of this adjVertex
  68.                         if(!openNodes.contains(adjVertex.to))
  69.                         {
  70.                             ///AND if the adjVertex is smaller in weight than the currently found smallest adjVertex
  71.                             if(smallestAdjVertex == null || adjVertex.weight < smallestAdjVertex.weight)
  72.                             {
  73.                                 smallestAdjVertex = adjVertex;
  74.                             }
  75.                         }
  76.                     }
  77.                 }
  78.                 if(smallestAdjVertex != null)
  79.                 {
  80.                     System.out.println(smallestAdjVertex);
  81.                     openNodes.add(smallestAdjVertex.to);
  82.                 }
  83.             }
  84.         }
  85.         System.out.println("===");
  86.         {
  87.             Comparator<Vertex> c = new VertexComparator();
  88.             PriorityQueue<Vertex> pq = new PriorityQueue<Vertex>(c);
  89.             List<Node> openNodes = new ArrayList<Node>();
  90.            
  91.             openNodes.add(allNodes.get(0));
  92.             for(Vertex v : openNodes.get(0).vertices)
  93.                 pq.add(v);
  94.            
  95.             while(openNodes.size() != allNodes.size())
  96.             {
  97.                 Vertex smallestAdjVertex = pq.remove();
  98.                 pq.clear();
  99.                 openNodes.add(smallestAdjVertex.to);
  100.                 System.out.println(smallestAdjVertex);
  101.                 for(Node openNode : openNodes)
  102.                 {
  103.                     for(Vertex v : openNode.vertices)
  104.                         if(!openNodes.contains(v.to))
  105.                             pq.add(v);
  106.                 }
  107.             }
  108.            
  109.         }
  110.     }
  111.    
  112.     class Node {
  113.         public int id;
  114.         public List<Vertex> vertices;
  115.         public Node(int _id)
  116.         {
  117.             id = _id;
  118.             vertices = new ArrayList<Vertex>();
  119.         }
  120.         public void addVertex(Vertex vertex)
  121.         {
  122.             vertices.add(vertex);
  123.         }
  124.         public String toString()
  125.         {
  126.             return "[node id: "+id+"]";
  127.         }
  128.     }
  129.     class Vertex
  130.     {
  131.         public Node from;
  132.         public Node to;
  133.         public float weight;
  134.         public Vertex(Node _from, Node _to, float _weight)
  135.         {
  136.             from = _from;
  137.             to = _to;
  138.             weight = _weight;
  139.         }
  140.         public String toString()
  141.         {
  142.             return "[ from: "+from.toString()+" to: "+to.toString()+" ]";
  143.         }
  144.     }
  145.     class VertexComparator implements Comparator<Vertex>
  146.     {
  147.         public int compare(Vertex o1, Vertex o2) {
  148.             if(o1.weight < o2.weight)
  149.                 return -1;
  150.             else if(o1.weight > o2.weight)
  151.                 return  1;
  152.             else
  153.                 return 0;
  154.         }
  155.     }
  156.    
  157.    
  158.    
  159.    
  160.    
  161.    
  162.    
  163.    
  164.    
  165.    
  166.    
  167.    
  168.    
  169.    
  170.    
  171.    
  172.    
  173.    
  174.    
  175.    
  176.    
  177.    
  178.    
  179.    
  180.    
  181.    
  182.    
  183.    
  184.    
  185.    
  186.    
  187.     final String[] input = new String[]{  
  188.             "8",
  189.             "16",
  190.             "4 5 0.35",
  191.             "4 7 0.37",
  192.             "5 7 0.28",
  193.             "0 7 0.16",
  194.             "1 5 0.32",
  195.             "0 4 0.38",
  196.             "2 3 0.17",
  197.             "1 7 0.19",
  198.             "0 2 0.26",
  199.             "1 2 0.36",
  200.             "1 3 0.29",
  201.             "2 7 0.34",
  202.             "6 2 0.40",
  203.             "3 6 0.52",
  204.             "6 0 0.58",
  205.             "6 4 0.93"
  206.             };
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement