Editing Depth-first search

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 106: Line 106:
 
<pre>
 
<pre>
 
function DFS(u,v)
 
function DFS(u,v)
     if id[v] = -1  
+
     if id[v] = -1
 
           id[v] = cnt
 
           id[v] = cnt
 
           let pred[v] = u
 
           let pred[v] = u
Line 121: Line 121:
 
</pre>
 
</pre>
 
After the termination of this algorithm, ''cnt'' will contain the total number of connected components, ''id[u]'' will contain an ID number indicating the connected component to which vertex ''u'' belongs (an integer in the range [0,''cnt'')) and ''pred[u]'' will contain the parent vertex of ''u'' in some spanning tree of the connected component to which ''u'' belongs, unless ''u'' is the first vertex visited in its component, in which case ''pred[u]'' will be ''u'' itself.
 
After the termination of this algorithm, ''cnt'' will contain the total number of connected components, ''id[u]'' will contain an ID number indicating the connected component to which vertex ''u'' belongs (an integer in the range [0,''cnt'')) and ''pred[u]'' will contain the parent vertex of ''u'' in some spanning tree of the connected component to which ''u'' belongs, unless ''u'' is the first vertex visited in its component, in which case ''pred[u]'' will be ''u'' itself.
 
===Flood fill===
 
The [[flood fill]] problem is often used as an example application of DFS. It avoids some of the implementational complexity associated with graph-theoretic algorithms by using an implicitly defined graph; there is no need to construct an adjacency matrix or adjacency list or any other such structure. Instead, we can just identify each vertex&mdash;a square in the grid&mdash;by its row and column indices; and it is very easy to find the adjacent squares. This is what a typical recursive implementation looks like (C++):
 
<syntaxhighlight lang="cpp">
 
// (dx[i], dy[i]) represents one of the cardinal directions
 
const int dx[4] = {0, 1, 0, -1};
 
const int dy[4] = {1, 0, -1, 0};
 
void do_flood_fill(vector<vector<int> >& grid, int r, int c, int oldval, int newval)
 
{
 
    // invalid coordinates
 
    if (r < 0 || c < 0 || r >= grid.size() || c >= grid[0].size())
 
        return;
 
    // outside the blob
 
    if (grid[r][c] != oldval)
 
        return;
 
    grid[r][c] = newval;
 
    for (int i = 0; i < 4; i++)
 
        do_flood_fill(grid, r + dx[i], c + dy[i], oldval, newval);
 
}
 
void flood_fill(vector<vector<int> >& grid, int r, int c, int newval)
 
{
 
    if (grid[r][c] != newval)
 
        do_flood_fill(grid, r, c, grid[r][c], newval);
 
}
 
</syntaxhighlight>
 
  
 
==References==
 
==References==

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)