public static Point[] smallestSeam(double[][] costMap){
    Point[] p=new Point[costMap.length]; //array to store the least costly seam
    Point currentPoint=null;

    //find the starting point of the seam (the lowest cost point)
    double firstmin=Double.MAX_VALUE;
    for(int j=0;j<costMap[costMap.length-1].length;j++){
      if(costMap[costMap.length-1][j]<firstmin){
        firstmin=costMap[costMap.length-1][j];
        currentPoint=new Point(costMap.length-1,j);
      }
    }

    p[p.length-1]=currentPoint;
    //iterate over the rest of the paths
    for(int i=costMap.length-1;i>0;i--){
      double min=Double.MAX_VALUE;
      if(isValidPoint(costMap,currentPoint.x-1,currentPoint.y) && costMap[currentPoint.x-1][currentPoint.y]<min){
        min=costMap[currentPoint.x-1][currentPoint.y];
        currentPoint=new Point(currentPoint.x-1,currentPoint.y);
      }
      if(isValidPoint(costMap,currentPoint.x-1,currentPoint.y-1) && costMap[currentPoint.x-1][currentPoint.y-1]<min){
        min=costMap[currentPoint.x-1][currentPoint.y-1];
        currentPoint=new Point(currentPoint.x-1,currentPoint.y-1);
      }
      if(isValidPoint(costMap,currentPoint.x-1,currentPoint.y+1) && costMap[currentPoint.x-1][currentPoint.y+1]<min){
        min=costMap[currentPoint.x-1][currentPoint.y+1];
        currentPoint=new Point(currentPoint.x-1,currentPoint.y+1);
      }
      p[i-1]=currentPoint; //the next point from which to continue on the path
    }
    return p;
  }