Table of Contents
IntroductionApplication: Connected ComponentsApplication: Graph Two-ColoringDFSBFSPrerequisite - Queues & DequesQueuesDequesImplementationSolution - Building RoadsDFS SolutionAn Issue With Deep RecursionBFS SolutionConnected Component ProblemsSolution - Building TeamsDFS SolutionBFS SolutionGraph Two-Coloring ProblemsQuizIntroduction
| Resources | |||||
|---|---|---|---|---|---|
| CPH | |||||
Graph traversal algorithms visit all nodes within a graph in a certain order and can compute some information along the way. Two common algorithms for doing this are depth first search (DFS) and breadth first search (BFS).
Application: Connected Components
Focus Problem – try your best to solve this problem before continuing!
A connected component is a maximal set of connected nodes in an undirected graph. In other words, two nodes are in the same connected component if and only if they can reach each other via edges in the graph.
In the above focus problem, the goal is to add the minimum possible number of edges such that the entire graph forms a single connected component.
Application: Graph Two-Coloring
Focus Problem – try your best to solve this problem before continuing!
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.
In the above focus problem, the goal is to assign each node (friend) of the graph to one of two colors (teams), subject to the constraint that edges (friendships) connect two nodes of opposite colors. In other words, we need to check whether the input is a bipartite graph and output a valid coloring if it is.
DFS
| Resources | |||||
|---|---|---|---|---|---|
| CSA | up to but not including "More about DFS" | ||||
| CPH | example diagram + code | ||||
From the second resource:
Depth-first search (DFS) is a straightforward graph traversal technique. The algorithm begins at a starting node, and proceeds to all other nodes that are reachable from the starting node using the edges of the graph.
Depth-first search always follows a single path in the graph as long as it finds new nodes. After this, it returns to previous nodes and begins to explore other parts of the graph. The algorithm keeps track of visited nodes, so that it processes each node only once.
When implementing DFS, we often use a recursive function to visit the vertices and an array to store whether we've seen a vertex before.
import syssys.setrecursionlimit(10**5) # Python has a default recursion limit of 1000n = 6visited = [False] * n"""Define adjacency list and read in problem-specific input here.
BFS
| Resources | |||||
|---|---|---|---|---|---|
| CSA | interactive, implementation | ||||
| PAPS | grid, 8-puzzle examples | ||||
| cp-algo | common applications | ||||
| KA | |||||
| YouTube | If you prefer a video format | ||||
In a breadth-first search, we travel through the vertices in order of their distance from the starting vertex.
Prerequisite - Queues & Deques
| Resources | |||||
|---|---|---|---|---|---|
| CPH | |||||
| PAPS | |||||
Queues
A queue is a First In First Out (FIFO) data structure that supports three operations, all in time.
Python has a builtin queue module.
Queue.put(n): Inserts element to the back of the queue.Queue.get(): Gets and removes the front element. If the queue is empty, this will wait forever, creating a TLE error.Queue.queue[n]: Gets the nth element without removing it. Set n to 0 for the first element.
from queue import Queueq = Queue() # []q.put(1) # [1]q.put(2) # [1, 2]v = q.queue[0] # v = 1, q = [1, 2]v = q.get() # v = 1, q = [2]v = q.get() # v = 2, q = []v = q.get() # Code waits forever, creating TLE error.
Warning!
Python's queue.Queue() uses Locks to maintain a threadsafe synchronization, so it's quite slow.
To avoid TLE, use collections.deque() instead for a faster version of a queue.
Deques
A deque (usually pronounced "deck") stands for double ended queue and is a combination of a stack and a queue, in that it supports insertions and deletions from both the front and the back of the deque. Not very common in Bronze / Silver.
In Python, collections.deque() is used for a deque data structure. The four methods for adding and removing are appendleft, popleft, append, and pop.
d = collections.deque()d.appendleft(3) # [3]d.appendleft(4) # [4, 3]d.append(7) # [4, 3, 7]d.popleft() # [3, 7]d.appendleft(1) # [1, 3, 7]d.pop() # [1, 3]
Implementation
When implementing BFS, we often use a queue to track the next vertex to visit. Like DFS, we'll also keep an array to store whether we've seen a vertex before.
from collections import deque"""Define adjacency list and read in problem-specific inputIn this example, we've provided "dummy input" that'sreflected in the GIF above to help illustrate theorder of the recursive calls."""
Solution - Building Roads
Note that each edge decreases the number of connected components by either zero or one. So you must add at least edges, where is the number of connected components in the input graph.
To compute , iterate through each node. If it has not been visited, visit it and all other nodes in its connected component using DFS or BFS. Then equals the number of times we perform the visiting operation.
There are many valid ways to pick new roads to build. One way is to choose a single representative from each of the components and link them together in a line.
DFS Solution
from collections import dequen, m = map(int, input().split())adj = [[] for _ in range(n)]for _ in range(m):a, b = map(int, input().split())adj[a - 1].append(b - 1)adj[b - 1].append(a - 1)
However, this code causes a runtime error on nearly half the test cases. What could be going wrong?
An Issue With Deep Recursion
If you run the solution code locally on the line graph generated by the following Python code:
n = 100000print(n, n - 1)for i in range(1, n):print(i, i + 1)
then you will observe a RecursionError that looks like this:
Traceback (most recent call last): File "input/code.py", line 28, in <module> solve(n, adj) File "input/code.py", line 14, in solve dfs(start, start) File "input/code.py", line 9, in dfs dfs(start, next) File "input/code.py", line 9, in dfs dfs(start, next) File "input/code.py", line 9, in dfs dfs(start, next) [Previous line repeated 994 more times] File "input/code.py", line 7, in dfs if next in unvisited: RecursionError: maximum recursion depth exceeded in comparison
This will occur for since the recursion limit in Python is set to
1000 by default.
We can fix this by increasing the recursion limit with
sys.setrecursionlimit(10 ** 6), although we still get TLE on two test cases.
To resolve this, we can implement a BFS solution, as shown below.
BFS Solution
from collections import dequen, m = map(int, input().split())adj = [[] for _ in range(n)]for _ in range(m):a, b = map(int, input().split())adj[a - 1].append(b - 1)adj[b - 1].append(a - 1)
Connected Component Problems
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| Silver | Easy | Show TagsConnected Components | ||||
| Silver | Easy | Show TagsBFS, Connected Components, DFS | ||||
| Silver | Easy | Show TagsConnected Components | ||||
| Kattis | Easy | Show TagsConnected Components | ||||
| ACSL | Easy | Show TagsDFS | ||||
| CSES | Normal | Show TagsDFS | ||||
| CSA | Normal | Show TagsBFS, DFS | ||||
| Gold | Normal | Show TagsBinary Search, Connected Components | ||||
| Silver | Normal | Show TagsBinary Search, Connected Components | ||||
| Silver | Normal | Show TagsBFS | ||||
| Silver | Normal | Show Tags2P, Binary Search, Connected Components | ||||
| Silver | Normal | Show TagsDFS | ||||
| CF | Normal | Show TagsConnected Components, DFS | ||||
| CSES | Normal | Show TagsConnected Components, DFS, Prefix Sums | ||||
| CF | Hard | Show TagsDFS, Sorted Set | ||||
| Kattis | Very Hard | Show TagsBinary Search, Connected Components | ||||
| Silver | Very Hard | Show TagsConstructive, Cycles, Spanning Tree | ||||
Solution - Building Teams
| Resources | |||||
|---|---|---|---|---|---|
| CPH | Brief solution sketch with diagrams. | ||||
| IUSACO | |||||
| cp-algo | |||||
| CP2 | |||||
For each connected component, we can arbitrarily label a node and then run DFS or BFS. 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.
DFS Solution
Optional: Adjacency List Without an Array of Vectors
See here.
Warning!
You have to submit with CPython (not PyPy3) to avoid TLE.
import sysinput = sys.stdin.readlinesys.setrecursionlimit(int(1e9)) # disable recursion limitn, m = map(int, input().strip().split())adj = [[] for _ in range(n)]team = [0] * n # 0: not assigned yet, 1: team 1, 2: team 2
BFS Solution
The specifics of the algorithm are almost exactly the same; it's just that we do them in an iterative rather than recursive fashion.
from collections import dequen, m = map(int, input().split())adj = [[] for _ in range(n)]for _ in range(m):a, b = map(int, input().split())adj[a - 1].append(b - 1)adj[b - 1].append(a - 1)
Graph Two-Coloring Problems
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| CF | Easy | Show TagsBipartite | ||||
| Silver | Easy | Show TagsBipartite | ||||
| CF | Easy | Show TagsBipartite | ||||
| Baltic OI | Hard | Show TagsDFS, Median | ||||
| CC | Hard | Show TagsBipartite, DFS | ||||
| CF | Hard | Show TagsBipartite, DFS | ||||
| CF | Hard | Show TagsBipartite, Trees | ||||
| APIO | Very Hard | Show TagsBipartite | ||||
Quiz
What's the main difference between DFS and BFS?
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!