4.9 KiB
id | title | author | prerequisites | ||
---|---|---|---|---|---|
dfs | Depth First Search | Siyong Huang |
|
- Depth First Search (DFS)
- Flood Fill
- Graph Two-Coloring
Depth First Search (DFS)
Depth First Search, more commonly DFS, is a fundamental graph algorithm that traverses an entire connected component. The rest of this document describes various applications of DFS. Of course, it is one possible way to implement flood fill. Breadth first search (BFS) is not required for silver.
Tutorial
- Recommended:
- CPH 12.1
- CSAcademy DFS
- Additional:
- CPC.7
- cp-algo DFS
- hard to parse if this is your first time learning about DFS
- Topcoder Graphs Pt 2
Problems
- CF
- PolandBall & Forest
- Bear & Friendship
- Journey
- DFS on Tree
- Wizard's Tour
- USACO
- Other
- POI Hotels
- Kattis Birthday Party (Easy)
- DFS with each edge removed
Flood Fill
Flood Fill refers to finding the number of connected components in a graph, usually when the graph is a grid.
Tutorial
- Recommended:
Problems
- Ice Perimeter (Easy)
- Switching on the Lights (Normal)
- Build Gates (Normal)
- Milk Pails (Normal)
- Where's Bessie?
- Why Did the Cow Cross the Road III, Silver (Normal)
- Multiplayer Moo (Hard)
- Snow Boots (Hard)
- Mooyo Mooyo
Graph Two-Coloring
Graph two-coloring refers to assigning a boolean value to each node of the graph, dictated by the edge configuration The most common example of a two-colored graph is a bipartite graph, in which each edge connects two nodes of opposite colors.
Tutorial
The idea is that we can arbitrarily label a node and then run DFS. Every time we visit a new (unvisited) node, we set its color based on the edge rule. When we visit a previously visited node, check to see whether its color matches the edge rule. For example, an implementation of coloring a bipartite graph is shown below.
//UNTESTED
bool is_bipartite = true;
void dfs(int node)
{
visited[node] = true;
for(int u:adj_list[node])
if(visited[u])
{
if(color[u] == color[node])
is_bipartite = false;
}
else
{
color[u] = !color[node];
dfs(u);
}
}
- Additional:
- Bipartite Graphs: cp-alg bipartite check
- Note: CP-Algorithms uses BFS, but DFS accomplishes the same task
- Bipartite Graphs: cp-alg bipartite check