Return to the Lecture Notes Index

15-111 Lecture 12 (February 11, 2009)

The Blobs problem

Assume we have a two-demensional grid of cells. Each cell may be empty or filled. Any group of cells that are connected (horizontally, vertically, or diagonally) constitutes a "blob." The goal is to count the number of cells in a blob, given the location of the blob. You might imagine that the cells have been created by scanning a microscope slide of a bacterial culture, and that the purpose is the estimate the degree of infection. (This problem comes from McCraken's classic textbook -- see the assignment handout for the full citation). This has also been refered to as a forest problem, what part of the forest was burned by the fire.

So, how should you go about solving this problem? The basic idea is this. The recursive method is invoked to determine the number of cells in its blob that are infected. If the cell is not infected or is not in the grid, it should return 0 -- these are the cases that will break the recursion and allow the count to unwind. Otherwise, it should return the sum of 1 for itself, plus whatever is counted by a recursive search of the cells around it. To accomplish this, it should call itself on those cells. We also have to account for the fact that once we count a cell it should have a marker that is changed so that we don't double count cells.
REMEMBER THIS IS PSEUDO CODE AND NOT ACTUAL CODE

  int blobCount( int row, int col)
  {


    // Cases that end our search -- cell can't be counted.

    if ( (row<0)|| (col<0) || (row >= marked.length) || (col >= marked.length) )
	 return 0;

    if (counted[row][col])
        return 0;
    else
        counted[row][col] = true;

    if( !marked[row][col])
        return 0;

    return blobCount (row-1, col-1) + blobCount (row-1, col) +  
      + blobCount (row-1, col+1) + 1 + blobCount (row , col+1)  
      + blobCount (row, col-1) + blobCount (row+1, col+1) 
      + blobCount (row+1, col) + blobCount( row+1, col-1);
  }
  
  

Flood-Fill, More Generally

The Blobs Problem is a specific example of a recursive technique generally known as Flood-Fill. It is called flood-fill, because it is the same technique used within paint programs to fill a bounded area. Think about how the "fill" tool works. It grows outward, just as does our solution to the blobs problem. And, the recursion ends when it goes off the canvas or hits a cell of the "border" color. The code looks exactly the same as our Blobs example above, except for the facts that a) it is marking/coloring the same board as it is processing and b) it isn't counting anything.

What makes flood-fill problems good problems for recursion? Well, what is naturally preserved by the runtime stack that we would otherwise want to preserve with an explicit stack? Int he case of the Queens problem, we were concerned about each decision point in the decision tree and the state of our decision making -- so that we could easily "backtrack" andsystematically try other possibilities.

In the case of flood fill, we are concerned with the location from which we are presently flooding outward. Although the interesting parts of our solution look like one line of code, it is important to realize that there are several recursive calls. And, that these calls are made one at a time, not in parallel. The stack keeps our state in the recursion tree, so we can continue to flood in all directions, rather than just following one path away. This is important, because the flood-fill appracoh relies on exploring in all directions, making this state information critical.