2

I need to implement a method/function to determine if a number is present in a matrix. The given matrix is sorted in descending order both in rows and columns. Here there are three examples of the defined matrix:

private static int[][] matrix1= {

    {69, 57, 43, 28, 14},
    {68, 56, 35, 25, 13},
    {66, 55, 34, 22, 8},
    {64, 52, 32, 21, 7},
    {62, 47, 31, 17, 5}
};

private static int[][] matrix2 = {
    {62, 47, 31, 17, 5}
};

private static int[][] matrix3 = {
    {14},
    {13},
    {9},
    {7},
    {5}
};

The function, should follow the Divide and Conquer principle. My idea is to look for an element of a matrix that acts as a pivot, and from it, divide the matrix. If the number we are looking for is larger than the pivot, we would keep the columns on the left of the pivot and its own but with the previous rows of the pivot. If the pivot is larger, it would be the other way around (the next columns and its own with the lower rows). In addition, to optimize the code, first I would need to search for the number in pivot´s column and then keep looking in the rest of the columns (but I don't know how to implement this in my code).

My code is the following but when running, this error keeps popping up java.lang.StackOverflowError.I understand that I am saturating the buffer's memory but I shouldn’t need to modify it to make the function work. I would like to avoid it unless it is the only solution.

public boolean contains(int[][] matrix, int num) {
    int rowMin = 0;
    int rowMax = matrix.length-1;
    int colMin = 0;
    int colMax = matrix[0].length-1; 
    return containsAux(matrix, rowMin, colMin, rowMax, colMax, num);

}
private boolean containsAux(int[][]matrix, int rowMin, int colMin, int rowMax, int colMax, int num) {
    boolean resul=false;
    int col= (colMax+colMin)/2;
    int row=(rowMax+rowMin)/2;
    int pivote= matrix[row][col];

    if(pivote==num) {
        resul=true;
    }else if(pivote<num && rowMin>=0 && colMin>=0) {
        containsAux(matrix,row-1,col,rowMin,col,num);
        containsAux(matrix,row-1,col-1,rowMin,col-1,num);
        fila-=1;
        containsAux(matrix,row+1,col-1,rowMax,col-1,num);
    }else if (pivote> num && rowMax<matrix.length && rowMax<matrix[0].length) {
        containsAux(matrix,row+1,col,rowMax,col,num);
        rowMin-=1;
        containsAux(matrix,rowMin+1,col+1,row,col+1,num);
        containsAux(matrix,row+1,col+1,rowMax,col+1,num);
    }

    return resul;
}

My test code is:

public class Prueba {

    private static int[][] matrix = {
        {69, 57, 43, 28, 14},
        {68, 56, 35, 25, 13},
        {66, 55, 34, 22, 8},
        {64, 52, 32, 21, 7},
        {62, 47, 31, 17, 5}
    };

    public static void main(String[] args) {
        CartonBingo cb= new CartonBingo();

        System.out.println(cb.contains(matrix, 35));
    }
}

My question is: Is there another approach to do this following the Divide and Conquer principle, or should I change the code to run it smoothly? Thank you in advance.

clemens
  • 16,716
  • 11
  • 50
  • 65
Kat
  • 23
  • 1
  • 5
  • 3
    Possible duplicate of [What is a StackOverflowError?](https://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror) – Joe C Oct 22 '17 at 17:48
  • 1
    No, I "understand" this error, but I don't know how do this program with correct Divide and Conquer. – Kat Oct 22 '17 at 17:52
  • 2
    Use binary search iteratively. No need of recursion. – Sachin Aggarwal Oct 22 '17 at 17:55
  • 1
    @SachinAggarwal I need to use a Divide and Conquer for this program, it is a requirement. – Kat Oct 22 '17 at 18:10
  • 1
    You are using binary search recursively. But binary search is NOT divided and conquer algorithm whether you implement it recursively or iteratively. Refer here - https://stackoverflow.com/questions/8850447/why-is-binary-search-a-divide-and-conquer-algorithm – Sachin Aggarwal Oct 22 '17 at 18:20
  • @SachinAggarwal Thank you very much. But I don't quite understand correctly the Divide and Conquer algorithm so other way to do this program would be divide the matrix by rows and find the number in each one in recursive mode? (for example) – Kat Oct 22 '17 at 18:36
  • This problem can be solved recursively like you were doing. However iteratively is easier. For solving this, consider running input manually. You are sending wrong arguments while making recursive calls. – Sachin Aggarwal Oct 22 '17 at 18:47
  • For example in this, containsAux(matrix,row-1,col,rowMin,col,num); inside 2nd elseif statement. Why are you changing rowMin. It should remain same as before when you are calling search on upper left part. – Sachin Aggarwal Oct 22 '17 at 18:51
  • @SachinAggarwal I do that so I can see the first row and be able to recursively call the next rows with the "+1". If I remove that, I would not look at row 0. – Kat Oct 22 '17 at 19:26

1 Answers1

2

I think the following link analyzes what you are trying to do with code example and complexity analysis:

http://www.geeksforgeeks.org/divide-conquer-set-6-search-row-wise-column-wise-sorted-2d-array/

Anyway I think you have some error in your code, updating parameters in recursive calls or something similar. The program continues doing the same call to the function until the stackoverflow happens.