Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Prims {
- public static void main(String[] args)
- {
- new Prims();
- }
- int[] pointsArr;
- public Prims()
- {
- List<Node> allNodes = new ArrayList<Node>();
- ///Set up the tree
- {
- int nPoints = Integer.parseInt(input[0]);
- int nVertices = Integer.parseInt(input[1]);
- for(int i = 0; i < nPoints; i++)
- allNodes.add(new Node(i));
- for(int i = 2; i < input.length; i++)
- {
- String[] arr = input[i].split(" ");
- int a = Integer.parseInt(arr[0]);
- int b = Integer.parseInt(arr[1]);
- float weight = Float.parseFloat(arr[2]);
- {
- Node aNode = allNodes.get(a);
- Node bNode = allNodes.get(b);
- aNode.addVertex(new Vertex(aNode, bNode, weight));
- bNode.addVertex(new Vertex(bNode, aNode, weight));
- }
- }
- }
- ///Set up the tree with scanner
- {
- int nPoints = Integer.parseInt(scan.nextLine());
- int nVertices = Integer.parseInt(scan.nextLine());
- for(int i = 0; i < nPoints; i++)
- allNodes.add(new Node(i));
- for(int i = 2; i < nVertices+2; i++)
- {
- String[] arr = scan.nextLine().split(" ");
- int a = Integer.parseInt(arr[0]);
- int b = Integer.parseInt(arr[1]);
- float weight = Float.parseFloat(arr[2]);
- {
- Node aNode = allNodes.get(a);
- Node bNode = allNodes.get(b);
- aNode.addVertex(new Vertex(aNode, bNode, weight));
- bNode.addVertex(new Vertex(bNode, aNode, weight));
- }
- }
- }
- ///
- {
- List<Node> openNodes = new ArrayList<Node>();
- List<Node> adjNodes = new ArrayList<Node>();
- openNodes.add(allNodes.get(0));
- while(openNodes.size() != allNodes.size())
- {
- Vertex smallestAdjVertex = null;
- ///Of all the openNodes
- for(Node openNode : openNodes)
- {
- ///Find the smallest vertex
- for(Vertex adjVertex : openNode.vertices)
- {
- ///If openNodes doesn't contain the TO of this adjVertex
- if(!openNodes.contains(adjVertex.to))
- {
- ///AND if the adjVertex is smaller in weight than the currently found smallest adjVertex
- if(smallestAdjVertex == null || adjVertex.weight < smallestAdjVertex.weight)
- {
- smallestAdjVertex = adjVertex;
- }
- }
- }
- }
- if(smallestAdjVertex != null)
- {
- System.out.println(smallestAdjVertex);
- openNodes.add(smallestAdjVertex.to);
- }
- }
- }
- System.out.println("===");
- {
- Comparator<Vertex> c = new VertexComparator();
- PriorityQueue<Vertex> pq = new PriorityQueue<Vertex>(c);
- List<Node> openNodes = new ArrayList<Node>();
- openNodes.add(allNodes.get(0));
- for(Vertex v : openNodes.get(0).vertices)
- pq.add(v);
- while(openNodes.size() != allNodes.size())
- {
- Vertex smallestAdjVertex = pq.remove();
- pq.clear();
- openNodes.add(smallestAdjVertex.to);
- System.out.println(smallestAdjVertex);
- for(Node openNode : openNodes)
- {
- for(Vertex v : openNode.vertices)
- if(!openNodes.contains(v.to))
- pq.add(v);
- }
- }
- }
- }
- class Node {
- public int id;
- public List<Vertex> vertices;
- public Node(int _id)
- {
- id = _id;
- vertices = new ArrayList<Vertex>();
- }
- public void addVertex(Vertex vertex)
- {
- vertices.add(vertex);
- }
- public String toString()
- {
- return "[node id: "+id+"]";
- }
- }
- class Vertex
- {
- public Node from;
- public Node to;
- public float weight;
- public Vertex(Node _from, Node _to, float _weight)
- {
- from = _from;
- to = _to;
- weight = _weight;
- }
- public String toString()
- {
- return "[ from: "+from.toString()+" to: "+to.toString()+" ]";
- }
- }
- class VertexComparator implements Comparator<Vertex>
- {
- public int compare(Vertex o1, Vertex o2) {
- if(o1.weight < o2.weight)
- return -1;
- else if(o1.weight > o2.weight)
- return 1;
- else
- return 0;
- }
- }
- final String[] input = new String[]{
- "8",
- "16",
- "4 5 0.35",
- "4 7 0.37",
- "5 7 0.28",
- "0 7 0.16",
- "1 5 0.32",
- "0 4 0.38",
- "2 3 0.17",
- "1 7 0.19",
- "0 2 0.26",
- "1 2 0.36",
- "1 3 0.29",
- "2 7 0.34",
- "6 2 0.40",
- "3 6 0.52",
- "6 0 0.58",
- "6 4 0.93"
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement