2020-06-04 01:42:57 +00:00
---
2020-06-15 23:19:07 +00:00
id: dfs
title: Depth First Search
2020-06-04 01:42:57 +00:00
author: Siyong Huang
2020-06-16 17:09:59 +00:00
prerequisites:
2020-06-22 20:51:12 +00:00
- Bronze - Introduction to Graphs
2020-06-22 19:59:16 +00:00
description: A way to traverse a graph using recursion.
2020-06-04 01:42:57 +00:00
---
2020-06-22 14:26:06 +00:00
## Depth First Search
2020-06-17 22:18:07 +00:00
2020-06-03 23:46:28 +00:00
- [CSES Building Roads ](https://cses.fi/problemset/task/1666 )
2020-06-03 22:50:50 +00:00
### Tutorial
- Recommended:
2020-06-04 13:54:18 +00:00
- CPH 12.1
2020-06-03 23:46:28 +00:00
- [CSAcademy DFS ](https://csacademy.com/lesson/depth_first_search/ )
2020-06-03 22:50:50 +00:00
- Additional:
2020-06-16 17:09:59 +00:00
- [CPC.7 ](https://github.com/SuprDewd/T-414-AFLV/tree/master/07_graphs_1 )
2020-06-03 22:54:18 +00:00
- [cp-algo DFS ](https://cp-algorithms.com/graph/depth-first-search.html )
2020-06-03 23:46:28 +00:00
- hard to parse if this is your first time learning about DFS
2020-06-16 17:09:59 +00:00
- [Topcoder Graphs Pt 2 ](https://www.topcoder.com/community/data-science/data-science-tutorials/introduction-to-graphs-and-their-data-structures-section-2/ )
2020-06-03 22:50:50 +00:00
### Problems
2020-06-22 01:45:24 +00:00
- CSES Trees
- [Subordinates ](https://cses.fi/problemset/task/1674 )
- [Tree Distances I ](https://cses.fi/problemset/task/1132 )
- [Tree Distances II ](https://cses.fi/problemset/task/1133 )
2020-06-04 21:42:30 +00:00
- CF
- [PolandBall & Forest ](http://codeforces.com/problemset/problem/755/C ) [](56)
- [Bear & Friendship ](http://codeforces.com/problemset/problem/771/A )
- [Journey ](http://codeforces.com/contest/839/problem/C ) [](54)
- DFS on Tree
- [Wizard's Tour ](http://codeforces.com/contest/860/problem/D ) [](59)
- USACO
- [Mootube, Silver (Easy) ](http://usaco.org/index.php?page=viewproblem2&cpid=788 )
- [Closing the Barn, Silver (Easy) ](http://usaco.org/index.php?page=viewproblem2&cpid=644 )
- [Moocast, Silver (Easy) ](http://usaco.org/index.php?page=viewproblem2&cpid=668 )
- [Pails (Normal) ](http://usaco.org/index.php?page=viewproblem2&cpid=620 )
- [Milk Visits (Normal) ](http://www.usaco.org/index.php?page=viewproblem2&cpid=968 )
2020-06-16 17:09:59 +00:00
- [Count Cross ](http://usaco.org/index.php?page=viewproblem2&cpid=716 )
- [Wormhole Sort (Normal) ](http://www.usaco.org/index.php?page=viewproblem2&cpid=992 )
- also binary search on the answer
- [Fence Planning ](http://usaco.org/index.php?page=viewproblem2&cpid=944 )
- [Moo Particle ](http://www.usaco.org/index.php?page=viewproblem2&cpid=1040 )
2020-06-04 21:42:30 +00:00
- Other
- [POI Hotels ](https://szkopul.edu.pl/problemset/problem/gDw3iFkeVm7ZA3j_16-XR7jI/site/?key=statement ) [](61)
- [Kattis Birthday Party (Easy) ](https://open.kattis.com/problems/birthday )
- DFS with each edge removed
2020-06-03 22:50:50 +00:00
2020-06-22 14:26:06 +00:00
2020-06-03 22:50:50 +00:00
## Graph Two-Coloring
2020-06-16 17:09:59 +00:00
*Graph two-coloring* refers to assigning a boolean value to each node of the graph, dictated by the edge configuration
2020-06-03 23:52:51 +00:00
The most common example of a two-colored graph is a *bipartite graph* , in which each edge connects two nodes of opposite colors.
2020-06-03 22:50:50 +00:00
2020-06-03 23:46:28 +00:00
- [CSES Building Teams ](https://cses.fi/problemset/task/1668 )
2020-06-03 22:50:50 +00:00
### 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.
2020-06-03 22:55:05 +00:00
```cpp
2020-06-03 23:42:22 +00:00
//UNTESTED
2020-06-03 22:50:50 +00:00
bool is_bipartite = true;
void dfs(int node)
{
2020-06-22 01:45:24 +00:00
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);
}
2020-06-03 22:50:50 +00:00
}
```
- Additional:
2020-06-03 22:54:18 +00:00
- [Bipartite Graphs: cp-alg bipartite check ](https://cp-algorithms.com/graph/bipartite-check.html )
2020-06-03 23:52:51 +00:00
- Note: CP-Algorithms uses BFS, but DFS accomplishes the same task
2020-06-03 22:50:50 +00:00
### Problems
2020-06-04 21:42:30 +00:00
- [CF Bipartiteness ](http://codeforces.com/contest/862/problem/B ) [](49)
2020-06-22 14:26:06 +00:00
- [The Great Revegetation (Normal) ](http://usaco.org/index.php?page=viewproblem2&cpid=920 )