2020-06-04 01:42:57 +00:00
---
2020-06-15 23:19:07 +00:00
id: dfs
2020-06-28 16:03:46 +00:00
title: Depth First Search (DFS)
2020-06-04 01:42:57 +00:00
author: Siyong Huang
2020-06-16 17:09:59 +00:00
prerequisites:
2020-07-16 22:51:57 +00:00
- intro-graphs
2020-07-15 03:54:45 +00:00
description: "A way to recursively traverse through a graph."
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: [
2020-07-13 17:40:05 +00:00
new Problem("CSES", "Building Roads", "1666", "Intro|Very Easy", false, ["DFS"]),
2020-06-25 01:38:05 +00:00
],
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"),
2020-07-14 18:21:06 +00:00
new Problem("Silver", "Mootube", "788", "Easy", false, ["Tree", "DFS"]),
new Problem("CF", "PolandBall & Forest", "problemset/problem/755/C", "Easy", false, ["Tree", "DFS"]),
2020-06-25 01:38:05 +00:00
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"]),
],
2020-07-14 18:21:06 +00:00
tree: [
new Problem("CSES", "Subordinates", "1674", "Very Easy", false, ["Tree", "DFS"]),
new Problem("CF", "Journey", "contest/839/problem/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"]),
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"], ""),
new Problem("CSA", "Tree Construction", "contest/860/problem/D", "Hard", false, ["Tree", "DFS"], "several cases"),
],
2020-06-25 01:38:05 +00:00
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"]),
],
}
};
2020-07-13 18:09:14 +00:00
## Resources
2020-06-03 22:50:50 +00:00
2020-06-28 16:03:46 +00:00
<resources>
<resource source="CPH" title="12.1 - DFS, 14 - Tree algorithms" starred></resource>
2020-06-29 20:20:17 +00:00
<resource source="CSA" title="Depth First Search" url="depth_first_search" starred>interactive</resource>
<resource source="IUSACO" title="10.4 - Graph Traversal Algorithms"></resource>
<resource source="PAPS" title="12.2 - Depth-First Search">with an example problem</resource>
<resource source="CPC" title="7 - Graphs 1" url="07_graphs_1">fast-paced</resource>
2020-06-28 16:03:46 +00:00
<resource source="cp-algo" title="Depth First Search" url="graph/depth-first-search.html">hard to parse for a beginner</resource>
<resource source="TC" title="Graphs Section 2" url="introduction-to-graphs-and-their-data-structures-section-2"></resource>
</resources>
2020-06-03 22:50:50 +00:00
2020-07-14 18:21:06 +00:00
## Counting Connected Components
2020-06-25 01:38:05 +00:00
2020-07-14 18:21:06 +00:00
<problems-list problems={metadata.problems.sample} />
### Implementation
2020-06-25 01:38:05 +00:00
2020-07-14 18:21:06 +00:00
(code?)
<IncompleteSection />
### Problems
2020-06-25 01:38:05 +00:00
<problems-list problems={metadata.problems.general} />
2020-06-22 14:26:06 +00:00
2020-07-14 18:21:06 +00:00
## Tree Problems
### Implementation
(code?)
<IncompleteSection />
### Problems
<problems-list problems={metadata.problems.tree} />
2020-06-03 22:50:50 +00:00
## Graph Two-Coloring
2020-06-29 20:20:17 +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-07-13 18:09:14 +00:00
### Resources
2020-06-03 22:50:50 +00:00
2020-06-28 16:03:46 +00:00
<resources>
2020-06-29 20:20:17 +00:00
<resource source="IUSACO" title="10.7 - Bipartite Graphs" starred></resource>
<resource source="CPH" title="12.3 - Graph Traversal: Applications"></resource>
2020-06-28 16:03:46 +00:00
<resource source="cp-algo" title="Bipartite Check" url="graph/bipartite-check.html">Uses BFS, but DFS accomplishes the same task.</resource>
</resources>
2020-06-23 01:00:35 +00:00
2020-07-14 18:21:06 +00:00
### Implementation
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-07-14 18:21:06 +00:00
<LanguageSection>
<CPPSection>
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-07-16 22:39:07 +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
}
```
2020-07-14 18:21:06 +00:00
</CPPSection>
</LanguageSection>
2020-06-03 22:50:50 +00:00
### Problems
2020-06-25 01:38:05 +00:00
<problems-list problems={metadata.problems.bip} />