【问题标题】:What is the time complexity for these two programs?这两个程序的时间复杂度是多少?
【发布时间】:2021-06-14 01:27:09
【问题描述】:

有人可以向我解释这三种方法的时间复杂度,并帮助我理解为什么是这种时间复杂度。两种方法都接受一个矩阵并找到它周围的所有邻居。第一种方法是通过递归完成的,第二种方法是通过迭代和堆栈完成的,第三种方法是通过尾递归完成的。

这里是第一种方法:

public static int ExploreAndLabelColony(char[][] grid, int i, int j, char c) { 
        grid[i][j] = c;
        int count = 1;
            
        if ((i>0 && i<grid.length && j<grid[0].length) && (grid[i-1][j] == '1')) { //vertical top
            count += ExploreAndLabelColony(grid, i-1,j,c); 
            }
        if (i+1<grid.length && j<grid[0].length && grid[i+1][j] == '1') { //vertical bottom
            count +=ExploreAndLabelColony(grid, i+1,j,c); 
            }
        if (j>0 && i<grid.length && j<grid[0].length && grid[i][j-1] == '1') { //horizontal left
            count +=ExploreAndLabelColony(grid, i,j-1,c); 
            }
        if (i<grid.length && j+1<grid[0].length && grid[i][j+1] == '1') { //horizontal right
            count +=ExploreAndLabelColony(grid, i,j+1,c); 
            }
        if (i+1<grid.length && j+1<grid[0].length && grid[i+1][j+1] == '1') { //diagonal bottom right
            count +=ExploreAndLabelColony(grid, i+1,j+1,c);
            } 
        if (j>0 && i+1<grid.length && j<grid[0].length && grid[i+1][j-1] == '1') { //diagonal bottom left
            count +=ExploreAndLabelColony(grid, i+1,j-1,c);
            } 
        if (i>0 && i<grid.length && j+1<grid[0].length && grid[i-1][j+1] == '1') { //diagonal top right
            count += ExploreAndLabelColony(grid, i-1,j+1,c);
            } 
        if (i>0 && j>0 && i<grid.length && j<grid[0].length && grid[i-1][j-1] == '1') { //diagonal top left
            count +=ExploreAndLabelColony(grid, i-1,j-1,c);
            }
            
        return count;
    }
}

这是第二种方法:

public static int ExploreAndLabelColony(char[][] grid, int i, int j, char c) { 
        Stack<String> strStack = new Stack<>();
        strStack.push(i + "," + j);

        while (!strStack.empty()) {
            String x = strStack.pop();
            
            int row = Integer.parseInt(x.split(",")[0]);
            int col = Integer.parseInt(x.split(",")[1]);

            if(row<0 || col<0 || row>=grid.length || col>=grid[0].length || visited[row][col] || grid[row][col]!='1')
                continue;
            
            visited[row][col]=true;
            grid[row][col]=c;
            count++;
            strStack.push(row + "," + (col-1)); //left
            strStack.push(row + "," + (col+1)); //right
            strStack.push((row-1) + "," + col); //up
            strStack.push((row+1) + "," + col); //down
            strStack.push((row-1) + "," + (col-1)); //left & up
            strStack.push((row-1) + "," + (col+1)); //right & up
            strStack.push((row+1) + "," + (col-1)); //left & down
            strStack.push((row+1) + "," + (col+1)); //right & down  
        }    
    return count;
    }

这是第三种方法:

static int ExploreAndLabelColony(Point point, char[][] grid, char c, int count) {
    if (point == null) {
        return count; // no more work
    }
    int i = point.i;
    int j = point.j;
    point = point.next;
    if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length) {
        
    } else if (grid[i][j] != '1') {
        
    } else {
        grid[i][j] = c;  // label
        count++;
        point = new Point(i - 1, j - 1, point);
        point = new Point(i - 1, j, point);
        point = new Point(i - 1, j + 1, point);
        point = new Point(i, j - 1, point);
        point = new Point(i, j + 1, point);
        point = new Point(i + 1, j - 1, point);
        point = new Point(i + 1, j, point);
        point = new Point(i + 1, j + 1, point);
    }
    return ExploreAndLabelColony(point, grid, c, count);
} 

【问题讨论】:

    标签: java recursion stack iteration time-complexity


    【解决方案1】:

    给定 n 行和 m 列,两者的时间复杂度都应该是 O(mn)。由于您是通过引用传递网格而不是重新访问已访问的单元格,因此这是深度优先搜索。第二种方法与第一种方法相同,只是将第一种方法中的调用堆栈替换为您自己的堆栈。

    【讨论】:

    • 第三种方法的时间复杂度是多少?
    • “寻找周围的邻居”应该可以在 O(n + m) 内完成,因为这是一个边界操作。
    • 不一定取决于边界的形状,它可能像螺旋形,在这种情况下应该是 O(mn)
    • 第三种方法的时间复杂度应该是O(mn)。
    猜你喜欢
    • 2021-02-25
    • 2022-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    相关资源
    最近更新 更多