计算矩阵中的区域数量

时间:2015-02-18 21:25:37

标签: matrix connectivity

我有阵列:

1 1 1 1
2 2 1 1
1 1 2 1
1 1 2 2

假设阵列只有1s和2s,我想计算2s的区域。对于上面的例子,答案是2个区域。

我的算法如下:

I have another array of the same size holding boolean values, true for visited, false for un-visted.

1) Loop through the array
2) If is 1 and un-visted mark it as visited
3) If it is 2 and unvisited then I have to check cross-style the neighbours of this 2, that is Left, Right, Top, Bottom.
4) If in 3 the neighbour is 2 and unvisited then mark it as visited and continue the cross-style searching.
5) If at any given point, the left, right, top and bottom are seen it means that it is a region?

我不确定这个算法是否会找到计算2s区域的所有可能方法,如果使用简单的循环,这将会起作用,我可以移动十字形状来找到邻居。

有人可以给我一个提示,或者如果我的逻辑不正确,我怎么能让它运作?

4 个答案:

答案 0 :(得分:1)

m成为n行和m列的矩阵。以下是算法的伪代码,该算法计算在问题中描述的区域中受启发的所需区域数量。请注意,我使用了unvisited条目的集合,而不是visited条目的集合。

Let unvisited be the collection of all pairs (i, j); 1<=i<= n, 1<=j<=m.
Initialize the region counter := 0.
While unvisited is not empty do:
    Pick any unvisited pair (i, j).
    If matrix[i, j] = 2
      then
        counter := counter + 1
        visit(i, j)
      else remove (i, j) from unvisited.
Return counter

其中visit(i, j)是递归例程:

If unvisited contains (i, j) then
  Remove (i, j) from unvisited
  If matrix[i, j] = 2 then
    If j > 1 then visit(i, j - 1)
    If j < m then visit(i, j + 1)
    If i > 1 then visit(i - 1, j)
    If i < n then visit(i + 1, j)

答案 1 :(得分:0)

不完全是,因为搜索次数会给你一些区域。如下例所示:

    int region=0;

    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            if(array[i][j]==1)
                ar[i][j]=true;
            else if(ar[i][j]==false){
                check(array,ar,4,i,j);
                region++;
            }

函数检查用于交叉搜索,它带来两个数组并修改它们。它当然是递归函数。

答案 2 :(得分:0)

初始化一个面具。然后遍历每个像素。如果未标记,则将其用作种子以查找连接的组件,例如将所有连接的像素标签设置为1.然后标记该区域中的所有像素。最后,在面具中找到最大的数字。

答案 3 :(得分:0)

我编写了一个通用代码,用于计算矩阵中区域的数量,该矩阵符合时间复杂度O(M * N)

rows, cols = 0, 0
def traverse_matrix(A, directions, i, j, visited):
    global rows, cols
    def isSafe(A, row, col, visited, current):
        return ( row >=0 and row < rows and col >=0 and col < cols and \
            not visited[row][col] and (current == A[row][col]))
    visited[i][j] = True
    #print i, j
    for k in range(len(directions)):
        if isSafe(A, i+directions[k][0], j+directions[k][1], visited, A[i][j]):
            traverse_matrix(A, directions, i+directions[k][0], j+directions[k][1], visited)

def solution(A):
    global rows, cols
    rows, cols = len(A), len(A[0])
    print A
    if(rows is 1 and cols is 1):
        return 1
    directions = [[1, 0], [0, -1], [-1, 0], [0, 1]]
    visited = []
    for i in range(rows):
        l = []
        for j in range(cols):
            l.append(False)
        visited.append(l)

    count = 0
    for i in range(rows):
        for j in range(cols):
            if not visited[i][j]:
                traverse_matrix(A, directions, i, j, visited)
                count += 1
    print "Regions count: {0}".format(count)

Output:
[[5, 4, 4], [4, 3, 4], [3, 2, 4], [2, 2, 2], [3, 3, 4], [1, 4, 4], [4, 1, 1]]
Regions count: 11
[[2, 3, 3], [4, 4, 1], [2, 1, 1], [5, 2, 3], [5, 2, 2], [1, 4, 1], [3, 4, 1]]
Regions count: 12
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Regions count: 9
[[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Regions count: 3
[[1, 1], [2, 2], [3, 3]]
Regions count: 3
[[1, 2], [1, 2]]
Regions count: 2
[[1, 2], [3, 4]]
Regions count: 4
[[1, 1], [1, 1]]
Regions count: 1