Finding sub matrix of a given matrix

一曲冷凌霜 提交于 2019-12-19 10:16:56

问题


i am trying to write an algorithm for finding a sub matrix in a given sub matrix. To solve this problem i had written the following code:

public class SubMatTry {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int a[][] = { { 2, 3, 5, 7 }, { 5, 8, 3, 5 }, { 7, 6, 9, 2 },
            { 3, 8, 5, 9 } };
    int b[][] = { { 9, 2 }, { 5, 9 } };
    int k = 0;
    int l = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            System.out.println("Element of a= " + a[i][j]);
            if (b[k][l] == a[i][j]) {
                System.out.println(b[k][l] + " = " + a[i][j]);
                if (b[k][l + 1] == a[i][j + 1]) {
                    System.out.println(b[k][l + 1] + " = " + a[i][j + 1]);
                    if (b[k + 1][l] == a[i + 1][j]) {
                        System.out.println(b[k + 1][l] + " = "
                                + a[i + 1][j]);
                        if (b[k + 1][l + 1] == a[i + 1][j + 1]) {
                            System.out.println(b[k + 1][l + 1] + " = "
                                    + a[i + 1][j + 1]);
                            System.out.println("Array found at" + i + " ,"
                                    + j);
                            System.exit(0);
                        }
                    }
                }
            }
        }

    }

}}

This code is working fine but i am not sure it is the exact solution of the problem or its just a work around. Please provide your expert comments. Thanks in advance.


回答1:


The algorithm is hard-coded for a 4×4 matrix and a 2×2 submatrix. Otherwise it looks fine as a brute-force algorithm.

I would have expressed it as follows:

outerRow:
for (int or = 0; or <= a.length - b.length; or++) {
    outerCol:
    for (int oc = 0; oc <= a[or].length - b[0].length; oc++) {
        for (int ir = 0; ir < b.length; ir++)
            for (int ic = 0; ic < b[ir].length; ic++)
                if (a[or + ir][oc + ic] != b[ir][ic])
                    continue outerCol;
        System.out.println("Submatrix found at row " + or + ", col " + oc);
        break outerRow;
    }
}

If you want something more efficient, I suggest you flatten them out, like this:

{ 2,3,5,7, 5,8,3,5, 7,6,9,2, 3,8,5,9 }

and search this sequence for the following pattern:

{ 9,2, _, _, 5, 9}

using standard find-substring techniques such as Aho-Corasick or Knuth-Morris-Pratt algorithm. (Note that you would have to skip some indexes to avoid false positives where there's a new row in the middle of the sequence.)




回答2:


First of all, i and j should not iterate up to 3 (if you are on a[3][3] you know it can't be a start of a submatrix, cause basically you're at the end of the matrix).

Secondly, don't use fixed numbers, like 4 - use a.length instead (this gives you length of array a - number of columns, while a[0].length would give you a length of first column - effectively, number of rows).

Thirdly, I'd change the quadruple if (sic) into a double for iterating on k and l, like that:

for (int i = 0; i < a.length - b.length + 1; i++) {
        for (int j = 0; j < a[0].length - b[0].length + 1; j++) {
            boolean submatrix = true; // at start we assume we have a submatrix
            for (int k = 0; k < b.length; ++k) {
                for (int l = 0; l < b[0].length; ++l) {
                    if (a[i + k][j + l] == b[k][l]) {
                        System.out.println("a[" + (i + k) + "][" + (j + l) + "] = b[" + k + "][" + l + "]");
                    } else {
                        submatrix = false; // we found inequality, so it's not a submatrix
                    }
                }
            }
            if (submatrix) {
                System.out.println("Found subatrix at " + i + "," + j + ".");
            }
        }
    }

(Not sure if it exactly works, didn't put it through compiler ;) )

Other than that if you use java, you should try to get used to objects, classes and methods (Matrix class with boolean isSubmatrix(Matrix b) method) - but for starters that should do.

Hope my answer helps.




回答3:


What follows is a solution I wrote based off the described strategy by @aioobe

public static boolean matrixContainsPattern(int[][] data, int[][] pattern) {
    int[] flatData = flattenMatrix(data);
    int[] flatPattern = flattenMatrix(pattern);

    //If the # of rows of data is less than the rows of pattern, we have a problem since we can match at most only a partial amount of the pattern into data
    if (flatData.length < flatPattern.length) {
        throw new IllegalArgumentException();
    }

    int dataRowLen = data[0].length;
    int patternRowLen = pattern[0].length;
    for (int i = 0; i < flatData.length - flatPattern.length + 1; i++) {
        //We found a potential match for the pattern
        if (flatData[i] == flatPattern[0]) {
            //k can keep track of indexes inside flatData
            int k = i;
            //l can keep track of indexes inside flatPattern
            int l = 0;
            //dataRowOffset will help us keep track of WHERE we found a match in flatPatterns' imaginary rows
            int dataRowOffset = (i % dataRowLen);
            //count to keep track of when we've reached the end of an imaginary row in data
            int count = 1;
            boolean patternFound = true;
            while (k < flatData.length && l < flatPattern.length) {
                if (flatData[k] != flatPattern[l]) {
                    patternFound = false;
                    break;
                }
                //if we reached the end of an imaginary row, we need to skip our pointer k to the next rows offset location
                //we also need to reset count to the offset, so we can again find the end of this new imaginary row
                if (count == patternRowLen) {
                    //To get to the position in the next row of where we first found our match, we add to k: the length of whats remaining in our current row,
                    //plus the offset from where we first found in the match in the current row
                    if (dataRowLen == patternRowLen) {
                        k++;
                    } else {
                        k += (dataRowLen - patternRowLen) + dataRowOffset;
                    }
                    count = 1;
                } else {
                    k++;
                    count++;
                }
                l++;
            }
            if (patternFound) {
                return true;
            }
        }
    }
    return false;
}

And the method to flatten a matrix into an array is as follows:

private static int[] flattenMatrix(int[][] matrix) {
        if (matrix == null || matrix[0] == null || matrix[0].length < 1) {
            throw new IllegalArgumentException();
        }
        int[] flattened = new int[matrix.length * matrix[0].length];

        int k = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                flattened[k] = matrix[i][j];
                k++;
            }
        }
        return flattened;
    }


来源:https://stackoverflow.com/questions/9885147/finding-sub-matrix-of-a-given-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!