Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.97 KB | None | 0 0
  1. package ML;
  2.  
  3. import javafx.util.Pair;
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) throws FileNotFoundException {
  11.     // write your code here
  12.         int K;
  13.         System.out.println("Dati numarul de clustere : ");
  14.         Scanner s1 = new Scanner(System.in);
  15.         K = s1.nextInt();
  16.  
  17.         BufferedReader br = new BufferedReader(new FileReader("dset500.csv"));
  18.  
  19.         try {
  20.             BufferedWriter bw = new BufferedWriter(new FileWriter("KMeansOut.txt"));
  21.         } catch (IOException e) {
  22.             e.printStackTrace();
  23.         }
  24.  
  25.         //adding points - reading csv
  26.         ArrayList<Point> points = new ArrayList<Point>();
  27.         String line;
  28.         ArrayList<ArrayList<Point>> adjList= new ArrayList<>();
  29.         ArrayList<Boolean> vizList = new ArrayList<Boolean>();
  30.         double x, y;
  31.         try {
  32.             while((line = br.readLine()) != null)
  33.             {
  34.                 String[] lines = line.split(",");
  35.                 x = Double.parseDouble(lines[0]);
  36.                 y = Double.parseDouble(lines[1]);
  37.                 Point pointy = new Point(x,y);
  38.                 //System.out.println("(" + x + ", " + y + ")");
  39.                 points.add(pointy);
  40.                 adjList.add(new ArrayList<Point>());
  41.                 vizList.add(false);
  42.             }
  43.         }
  44.         catch (IOException e) {
  45.             e.printStackTrace();
  46.         }
  47.  
  48.         System.out.println("Avem "+points.size()+" puncte.");
  49.         System.out.println();
  50.         System.out.println();
  51.  
  52.         Collections.sort(points, new PointComparator());
  53.         //System.out.println(vizList.size());
  54.         //Acum avem o ordine pentru a ne putea referi la adjList si vizList
  55.  
  56.  
  57.         //let's make some edges
  58.  
  59.         ArrayList<Pair<Edge, Pair<Integer, Integer>>> allEdges =  new ArrayList<>();
  60.         for(int i=0; i<points.size()-1; ++i){
  61.             for(int j=i+1; j<points.size(); ++j){
  62.                 Pair<Integer, Integer> auxPair = new Pair<>(i, j);
  63.                 Pair<Edge, Pair<Integer, Integer>> auxEdgePair = new Pair<>(new Edge(points.get(i), points.get(j)), auxPair);
  64.                 allEdges.add(auxEdgePair);
  65.                // System.out.println("("+auxEdgePair.getKey().getSquareLength() + ", " + points.get(auxEdgePair.getValue().getKey()).getX() + ", " + points.get(auxEdgePair.getValue().getKey()).getY() + ", " + points.get(auxEdgePair.getValue().getValue()).getX() + ", " + points.get(auxEdgePair.getValue().getValue()).getY());
  66.             }
  67.         }
  68.      //   System.out.println(new Point(4.63982706567831,2.65349724511111).getSqDist(new Point(4.75194435915857,3.00555353562793)));
  69.             Collections.sort(allEdges, new Comparator<Pair<Edge, Pair<Integer, Integer>>> () {
  70.             @Override
  71.             public int compare(final Pair<Edge, Pair<Integer, Integer>> o1, final Pair<Edge, Pair<Integer, Integer>> o2) {
  72.                 // TODO: implement your logic here
  73.                 double i1 = o1.getKey().getSquareLength();
  74.                 double i2 = o2.getKey().getSquareLength();
  75.                 if(i1>i2) return 1;
  76.                 else if (i2>i1) return -1;
  77.                 else return 0;
  78.             }
  79.         });
  80.  
  81.         /*for(int i=0; i<allEdges.size();++i){
  82.             System.out.println("("+allEdges.get(i).getKey().getSquareLength() + ", " + points.get(allEdges.get(i).getValue().getKey()).getX() + ", " + points.get(allEdges.get(i).getValue().getKey()).getY() + ", " + points.get(allEdges.get(i).getValue().getValue()).getX() + ", " + points.get(allEdges.get(i).getValue().getValue()).getY());
  83.         }*/
  84.  
  85.         //Sorted edges. Time to Kruskal.
  86.  
  87.  
  88.  
  89.         ArrayList<Pair<Edge, Pair<Integer, Integer>>> MST =  new ArrayList<>();
  90.  
  91.         MST.clear();
  92.         for(int i=0; i<vizList.size(); ++i){
  93.             vizList.set(i, false);
  94.         }
  95.         ArrayList<Set<Integer>> disjointSets = new ArrayList<>();
  96.         for(int i=0; i<allEdges.size();++i){
  97.             //easy add
  98.  
  99.             //if 2 points are not visited, we add the edge; since they are not visited, they are in no disjoint set
  100.             if(!vizList.get(allEdges.get(i).getValue().getKey()) && !vizList.get(allEdges.get(i).getValue().getValue())){
  101.                 MST.add(allEdges.get(i));
  102.                 vizList.set(allEdges.get(i).getValue().getKey(), true);
  103.                 vizList.set(allEdges.get(i).getValue().getValue(), true);
  104.                 Set<Integer> newSet = new HashSet<>();
  105.                 newSet.add(allEdges.get(i).getValue().getValue());
  106.                 newSet.add(allEdges.get(i).getValue().getValue());
  107.                 disjointSets.add(newSet);
  108.             }
  109.             //if 1 point is visited, we have to find its disjoint set and add the next; since an if clause can't tell which is visited, we need 2 \
  110.             else if(vizList.get(allEdges.get(i).getValue().getKey()) && !vizList.get(allEdges.get(i).getValue().getValue())){
  111.                 for(int j = 0; j<disjointSets.size(); ++j){
  112.                     if(disjointSets.get(j).contains(allEdges.get(i).getValue().getKey())){
  113.                         disjointSets.get(j).add(allEdges.get(i).getValue().getValue());
  114.                         MST.add(allEdges.get(i));
  115.                         vizList.set(allEdges.get(i).getValue().getValue(), true);
  116.                         break;
  117.                     }
  118.                 }
  119.             }
  120.             else if(vizList.get(allEdges.get(i).getValue().getValue()) && !vizList.get(allEdges.get(i).getValue().getKey())){
  121.                 for(int j = 0; j<disjointSets.size(); ++j){
  122.                     if(disjointSets.get(j).contains(allEdges.get(i).getValue().getValue())){
  123.                         disjointSets.get(j).add(allEdges.get(i).getValue().getKey());
  124.                         MST.add(allEdges.get(i));
  125.                         vizList.set(allEdges.get(i).getValue().getKey(), true);
  126.                         break;
  127.                     }
  128.                 }
  129.             }
  130.             //both visited remain; we must either merge the 2 sets or continue if both are in the same set
  131.             else{
  132.                 int foundFirst = -1;
  133.                 int foundSecond = -1;
  134.                 for(int j=0; j<disjointSets.size(); ++j){
  135.                     if(disjointSets.get(j).contains(allEdges.get(i).getValue().getValue())){
  136.                         foundSecond=j;
  137.                     }
  138.                     if(disjointSets.get(j).contains(allEdges.get(i).getValue().getKey())){
  139.                         foundFirst=j;
  140.                     }
  141.                 }
  142.                /* if (foundFirst == foundSecond) {
  143.                     System.out.println(foundFirst + " == " + foundSecond);
  144.                 }
  145.                 else{
  146.                     System.out.println(foundFirst + " != " + foundSecond);
  147.                 }*/
  148.                if(foundFirst!=-1 && foundSecond!=-1){
  149.                    Set<Integer> aux = disjointSets.get(foundFirst);
  150.                    aux.addAll(disjointSets.get(foundSecond));
  151.  
  152.                    disjointSets.remove(foundFirst);
  153.                    disjointSets.remove(foundSecond);
  154.                    disjointSets.add(aux);
  155.                }
  156.             }
  157.             }
  158.  
  159.  
  160.         for(int i=0; i<MST.size(); ++i){
  161.         //    System.out.println("("+MST.get(i).getKey().getSquareLength() + ", " + points.get(MST.get(i).getValue().getKey()).getX() + ", " + points.get(MST.get(i).getValue().getKey()).getY() + ", " + points.get(MST.get(i).getValue().getValue()).getX() + ", " + points.get(MST.get(i).getValue().getValue()).getY() + ")");
  162.         }
  163.  
  164.         for(int i=0; i<disjointSets.size();++i){
  165.                 System.out.println(disjointSets.get(i));
  166.         }
  167.         System.out.println(MST.size() + " " + allEdges.size());
  168.  
  169.     }
  170.  
  171.  
  172. }
  173.  
  174. public class Point{
  175.     private double x;
  176.     private double y;
  177.     public Point(double px, double py) {
  178.         this.x = px;
  179.         this.y = py;
  180.     }
  181.  
  182.     public double getX() {
  183.         return x;
  184.     }
  185.     public double getY(){
  186.         return y;
  187.     }
  188.  
  189.     public double getSqDist(Point p){
  190.         return (this.y-p.y)*(this.y-p.y)+(this.x-p.x)*(this.x-p.x);
  191.     }
  192.  
  193.  
  194.     public void addPoint(Point p){
  195.         this.x+=p.x;
  196.         this.y+=p.y;
  197.     }
  198.  
  199.     public void divCoord(int n){
  200.         this.x = x/(double)n;
  201.         this.y = y/(double)n;
  202.     }
  203. }
  204.  
  205. public class Edge {
  206.     private Point x;
  207.     private Point y;
  208.     private double squareLength;
  209.  
  210.     public Edge(Point px, Point py){
  211.         this.x=px;
  212.         this.y=py;
  213.         this.squareLength = x.getSqDist(y);
  214.     }
  215.  
  216.     public double getSquareLength(){
  217.         return this.squareLength;
  218.     }
  219. }
  220.  
  221. import java.util.Comparator;
  222.  
  223. public class PointComparator implements Comparator<Point> {
  224.     @Override
  225.     public int compare(Point o1, Point o2) {
  226.         double i1 = o1.getSqDist(new Point(0,0));
  227.         double i2 = o2.getSqDist(new Point(0,0));
  228.         if(i1>i2) return 1;
  229.         else if (i2>i1) return -1;
  230.         else return 0;
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement