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-26 18:00:32 +00:00
frequency: 4
2020-06-04 01:42:57 +00:00
---
2020-06-25 01:38:05 +00:00
import { Problem } from "../models";
export const metadata = {
problems: {
sample: [
new Problem("CSES", "Building Roads", "1666", "Intro", false, ["DFS"]),
],
tree: [
new Problem("CSES", "Subordinates", "1674", "Intro", false, ["Tree", "DFS"]),
new Problem("Silver", "Mootube", "788", "Easy", false, ["Tree", "DFS"]),
new Problem("CF", "Journey", "contest/839/problem/C", "Easy", false, ["Tree", "DFS"]),
new Problem("CF", "PolandBall & Forest", "problemset/problem/755/C", "Easy", false, ["Tree", "DFS"]),
new Problem("CSES", "Tree Diameter", "1131", "Normal", false, ["Tree", "DFS"]),
new Problem("CSES", "Tree Distances I", "1132", "Normal", false, ["Tree", "DFS"]),
new Problem("CSES", "Tree Distances II", "1133", "Normal", false, ["Tree", "DFS"]),
new Problem("CF", "Wizard's Tour", "contest/860/problem/D", "Normal", false, ["Tree", "DFS"]),
new Problem("POI", "Hotels", "https://szkopul.edu.pl/problemset/problem/gDw3iFkeVm7ZA3j_16-XR7jI/site/?key=statement", "Normal", false, ["Tree", "DFS"]),
2020-06-27 03:07:31 +00:00
new Problem("HE", "Birthday Gifts", "https://www.hackerearth.com/practice/math/combinatorics/inclusion-exclusion/practice-problems/algorithm/mancunian-and-birthday-gifts-d44faa15/description/", "Normal", false, ["Tree", "PIE"], ""),
2020-06-25 01:38:05 +00:00
new Problem("CSA", "Tree Construction", "contest/860/problem/D", "Hard", false, ["Tree", "DFS"], "several cases"),
],
general: [
new Problem("CF", "Bear & Friendship", "problemset/problem/771/A", "Easy", false, ["DFS"]),
new Problem("Silver", "Closing the Farm", "644", "Easy", false, ["DFS"]),
new Problem("Silver", "Moocast", "668", "Easy", false, ["DFS"]),
new Problem("Silver", "Fence Planning", "944", "Easy", false, ["DFS"]),
new Problem("Kattis", "Birthday Party", "birthday", "Easy", false, ["DFS"], "DFS with each edge removed"),
new Problem("Silver", "Milk Visits", "968", "Normal", false, ["DFS"]),
new Problem("Silver", "Milk Pails", "620", "Normal", false, ["DFS"]),
new Problem("Silver", "Wormhole Sort", "992", "Normal", false, ["DFS", "Binary Search"]),
new Problem("Silver", "Moo Particle", "1040", "Normal", false, ["Sorting"]),
],
bipsample: [
new Problem("CSES", "Building Teams", "1668", "Easy", false, ["Bipartite"]),
],
bip: [
new Problem("CF", "Bipartiteness", "contest/862/problem/B", "Easy", false, ["Bipartite"]),
new Problem("Silver", "The Great Revegetation", "920", "Easy", false, ["Bipartite"]),
],
}
};
<problems-list problems={metadata.problems.sample} />
## Tutorial
2020-06-03 22:50:50 +00:00
- Recommended:
2020-06-23 01:00:35 +00:00
- CPH 12.1 (DFS), 14 (Tree algorithms)
- [PAPS 12.2](https://www.csc.kth.se/~jsannemo/slask/main.pdf)
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-25 01:38:05 +00:00
- Trees
<problems-list problems={metadata.problems.tree} />
- General
<problems-list problems={metadata.problems.general} />
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-25 01:38:05 +00:00
<problems-list problems={metadata.problems.bipsample} />
2020-06-03 23:46:28 +00:00
2020-06-03 22:50:50 +00:00
### Tutorial
2020-06-25 01:38:05 +00:00
- CPH 12.3
- [cp-algo - bipartite check](https://cp-algorithms.com/graph/bipartite-check.html)
- Uses BFS, but DFS accomplishes the same task
2020-06-23 01:00:35 +00:00
2020-06-03 22:50:50 +00:00
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
}
```
### Problems
2020-06-25 01:38:05 +00:00
<problems-list problems={metadata.problems.bip} />