title: Java DBSCAN Pseudocode Implementation
body: <p>I feel my logic is correct but I think I'm missing up with the syntax on it. It's my first time ever using the List or Vectors but I felt this would be the best way to go with the implementation as I'm having to read data into the algorithm. I'm trying to implement it just following the pseudocode. I have incorporated the expandCluster into the DBSCAN part as I felt it went better that way.</p>
  
  <p>Pseudocode
  <img src="https://i.stack.imgur.com/zzq5m.png" alt="DBSCAN"></p>
  
  <p>Here's my implementation. Ignore all my test print statements.</p>
  
  <pre><code>package dbscanAlgo;
  
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Vector;
  
  
  
  public class dbscanAlgo{
      public static List &lt;Vector&gt;Data;
      public static List &lt;Clusters&gt; Clusters;
      public static List &lt;Boolean &gt; Pvisited;
      public static List &lt;Boolean &gt; clustered;
      public static List  neighborpts;
      public static List  neighbors;
      public static List  noise;
  
  
  public static void read(int String){
  
      String[] values;
       //System.out.println("Working Directory = " +
               // System.getProperty("user.dir"));
  
      Data= new ArrayList();
      Pvisited= new ArrayList();
      Double ve;
  
      try
      {
      BufferedReader in = new BufferedReader(new FileReader("wholesale.csv"));    
          String line;
          Vector&lt;Double&gt; v= null;
          int j =0;
          while ((line = in.readLine()) != null)  
          {
              values = line.split(",");
              v = new Vector();
              for (int i =0; i&lt;values.length; i++)
                  {
                  ve = Double.parseDouble(values[i]);
  
                  v.add(ve);
  
                  }
              Data.add(v);
              //System.out.println(Data);
              Pvisited.add(false);
              //System.out.println(Pvisited.get(j));
              v =null;
              j++;
              }
  
          in.close();
  
      }catch( IOException ioException ) {}
  }
  
  public static void DBSCAN (int esp, int minPts){
      int c=0;
      //System.out.println(c);
      Clusters = new &lt;Clusters&gt; ArrayList();
      neighborpts= new ArrayList();
      for(int i=0 ; i&lt;Data.size(); i++){
          neighborpts.add(null);
      }
      noise= new ArrayList();
      for(int i =0 ; i&lt; Data.size(); i++){
          //System.out.println("The size of the file is: "+ Data.size());
          if(!Pvisited.get(i)){
              Pvisited.set(i,true);
              neighborpts.set(i,regionQuery(Data.get(i),esp));
              //System.out.println(neighborpts);
              int size=neighborpts.size();
              //System.out.println(minPts);
              if(size&lt; minPts)
                  //System.out.println(noise);
                  noise.add(i);
                  //System.out.println(noise.get(i));
          else{
              //System.out.println("HEy");
              Clusters.addAll(Data.get(i));
              //System.out.println(c);
              c++;
              //System.out.println("This is c" + c);
              Clusters C= new Clusters(c);
              C.setPoint(Data.get(c));
              Clusters.add(C);
              //System.out.println(size);
              //C.printC().toString();//System.out.print(C.printC());
              for(int j= 0; j&lt;size;j++){
                  //if P' is not visited
                      if(!Pvisited.get(neighborpts.indexOf(j))){
                          Pvisited.set(j,true);
                          neighbors.add(regionQuery((Vector) neighborpts.get(j),esp));
                          //System.out.println(neighbors);
                          int nSize=neighbors.size();
                          //System.out.println(nSize);
                          if (nSize&gt;= minPts){
                              neighborpts.add(neighbors);
                          }
                      }
                      // if P' is not yet a member of any cluster
                      // add P' to cluster c
                      if(!clustered.get(neighborpts.indexOf(j))){
                          int x=(int) neighborpts.get(j);
                          Clusters f= Clusters.get(c);
                          ((List&lt;Integer&gt;) f).add(x);
                      }
              }
          }
          }
      }//end of the main for loop
  }
  
  
  public static double ecluediean (Vector center, Vector L){
      Double result = (double) 0;
      for(int i =0; i&lt; center.size(); i++){
          result +=Math.pow(((double) center.get(i))-(double)(L.get(i)), 2);
      }
  
      return Math.sqrt(result);
  }
  
  public static List regionQuery(Vector p, int eps){
      List &lt;Vector&gt; n = new ArrayList();
      for(int i=0 ; i&lt;Data.size(); i++){
           n.add(null);
      }
      double dis=0;
      for(int i =0; i&lt;Data.size();i++){
          dis=ecluediean(p,Data.get(i));
  
          if(dis&lt;= eps){
  
              n.set(i,Data.get(i));
          }
      }
      //System.out.println(n);
      return n;
  }
  
  public static void main(String[]args){
      int N =3;
      read(1);
  
      DBSCAN(3,5);
  
  
      //System.out.println(((List&lt;Vector&gt;) Clusters.get(0)).get(1));
  }   
  }
  </code></pre>