Search code examples
pythonbreadth-first-search

Leetcode 417 BFS Time Limit Exceeded


I am working on Leetcode 417 Pacific Atlantic Water Flow:

There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges.

The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).

The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.

Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.

My solution is below. I am having Time Limit Exceeded error for a very large test case like

[[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19],[72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,20],[71,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,90,21],[70,135,192,193,194,195,196,197,198,199,200,201,202,203,204,205,152,91,22], ...]

I cannot figure out why my BFS did not work within a reasonable time. What am I missing?

class Solution:
    def pacificAtlantic(self, heights):
        rows, cols = len(heights), len(heights[0])

        # define directions
        directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
        def bfs(node, visited):
            Q = [node]
            while Q:
                x, y = Q.pop(0)
                visited[x][y] = True
                for dx, dy in directions:
                    next_x, next_y = x + dx, y + dy
                    if next_x < 0 or next_x >= rows:
                        continue
                    if next_y < 0 or next_y >= cols:
                        continue
                    if visited[next_x][next_y]:
                        continue
                    if heights[x][y] > heights[next_x][next_y]:
                        continue

                    Q.append((next_x, next_y))
        
        # pacific
        pacific_start = [[0, i] for i in range(cols)] + [[i, 0] for i in range(1, rows)]
        pacific_visited = [[False for _ in range(cols)] for _ in range(rows)]
        for row, col in pacific_start:
            if not pacific_visited[row][col]:
                bfs((row, col), pacific_visited, 0)

        # atlantic
        atlantic_start = [[rows - 1, i] for i in range(cols)] + [[i, cols - 1] for i in range(0, rows - 1)]
        atlantic_visited = [[False for _ in range(cols)] for _ in range(rows)]
        for row, col in atlantic_start:
            if not atlantic_visited[row][col]:
                bfs((row, col), atlantic_visited, 0)
  
        # find the common land
        ans = []
        for i in range(rows):
            for j in range(cols):
                if pacific_visited[i][j] and atlantic_visited[i][j]:
                    ans.append((i, j))
        return ans

Solution

  • There are two issues in your bfs function that negatively affect performance:

    1. pop(0) is not efficient on a list. Instead use a deque
    2. You mark a node as visited, after having taken it from the queue, but that means you can have several copies of the same cell in the queue, increasing the number of iterations the BFS loop will make. Instead mark a node as visited at the time you push it on the queue.

    Here is the code of your bfs function, with minimal changes needed to resolve those two issues:

    def bfs(node, visited, test):
        visited[node[0]][node[1]] = True # mark node when it enters the queue
        Q = deque([node])  # Use a deque, not a list
        while Q:
            x, y = Q.popleft()  # now it's an efficient operation
            for dx, dy in directions:
                next_x, next_y = x + dx, y + dy
                if next_x < 0 or next_x >= rows:
                    continue
                if next_y < 0 or next_y >= cols:
                    continue
                if visited[next_x][next_y]:
                    continue
                if heights[x][y] > heights[next_x][next_y]:
                    continue
                visited[next_x][next_y] = True # mark node when it enters the queue
                Q.append((next_x, next_y))