3.6 KiB
3.6 KiB
id | title | author | prerequisites | description | ||
---|---|---|---|---|---|---|
dfs | Depth First Search | Siyong Huang |
|
A way to traverse a graph using recursion. |
Depth First Search
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
- CSES Trees
- 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
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