Breadth First Search (BFS)
Authors: Benjamin Qi, Michael Cao
Prerequisites
Traversing a graph in a way such that vertices closer to the starting vertex are processed first.
Queues & Deques
Resources | |||
---|---|---|---|
CPH | |||
PAPS |
Queues
A queue is a First In First Out (FIFO) data structure that supports three operations, all in time.
C++
C++
push
: insertion at the back of the queuepop
: deletion from the front of the queuefront
: which retrieves the element at the front without removing it.
queue<int> q;q.push(1); // [1]q.push(3); // [3, 1]q.push(4); // [4, 3, 1]q.pop(); // [4, 3]cout << q.front() << endl; // 3
Java
Java
add
: insertion at the back of the queuepoll
: deletion from the front of the queuepeek
: which retrieves the element at the front without removing it
Java doesn't actually have a Queue
class; it's only an interface. The most commonly used implementation is the LinkedList
, declared as follows:
Queue<Integer> q = new LinkedList<Integer>();q.add(1); // [1]q.add(3); // [3, 1]q.add(4); // [4, 3, 1]q.poll(); // [4, 3]System.out.println(q.peek()); // 3
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.
C++
C++
The four methods for adding and removing are push_back
, pop_back
, push_front
, and pop_front
.
deque<int> d;d.push_front(3); // [3]d.push_front(4); // [4, 3]d.push_back(7); // [4, 3, 7]d.pop_front(); // [3, 7]d.push_front(1); // [1, 3, 7]d.pop_back(); // [1, 3]
You can also access deques in constant time like an array in constant time with the []
operator. For example, to access the element for some deque , do .
Java
Java
In Java, the deque class is called ArrayDeque
. The four methods for adding and removing are addFirst
, removeFirst
, addLast
, and removeLast
.
ArrayDeque<Integer> deque = new ArrayDeque<Integer>();deque.addFirst(3); // [3]deque.addFirst(4); // [4, 3]deque.addLast(7); // [4, 3, 7]deque.removeFirst(); // [3, 7]deque.addFirst(1); // [1, 3, 7]deque.removeLast(); // [1, 3]
Breadth First Search
Focus Problem – read through this problem before continuing!
Resources
Resources | |||
---|---|---|---|
CSA | interactive, implementation | ||
PAPS | grid, 8-puzzle examples | ||
cp-algo | common applications | ||
KA |
Implementation
C++
From the CSA article:
#include <algorithm>#include <fstream>#include <iostream>#include <queue>using namespace std;const int MAX_N = 100005;vector<int> graph[MAX_N];int dist[MAX_N];bool visited[MAX_N];
Java
Implementation of the CSAcademy article's problem in Java:import java.util.*;import java.io.*;class Main {static ArrayList<Integer> edges[];static int dist[];static boolean visited[];static void bfs(int startNode) {
Pro Tip
In the gold division, the problem statement will almost never directly be, "Given an unweighted graph, find the shortest path between node and ." Instead, the difficulty in many BFS problems are converting the problem into a graph on which we can run BFS and get the answer.
0/1 BFS
A 0/1 BFS finds the shortest path in a graph where the weights on the edges can only be 0 or 1, and runs in using a deque. Read the resource below for an explanation of how the algorithm works.
Resources | |||
---|---|---|---|
cp-algo | common applications |
This section is not complete.
straightforward example
Problems
Status | Source | Problem Name | Difficulty | Tags | Solution |
---|---|---|---|---|---|
CSES | Easy | Show TagsBFS | |||
CSES | Easy | Show TagsBFS | View Solution | ||
CSES | Normal | Show TagsCycle | |||
CSA | Normal | Show TagsBFS, DFS | Check CSA | ||
Silver | Easy | Show TagsBFS | External Sol | ||
Gold | Normal | Show TagsBFS | External Sol | ||
Gold | Normal | External Sol | |||
Old Silver | Normal | Show TagsBFS | External Sol | ||
Gold | Hard | Show TagsBFS | |||
Gold | Hard | Show TagsBFS | External Sol | ||
Gold | Very Hard | External Sol |