In words
What it is, why it matters, and what it is like.
Why am I learning this?
Imagine you need to find the fastest route from your home to a new restaurant, or trace how a piece of misinformation spread through your friend group over just three steps. You cannot do this by guessing; you need a reliable method for checking connections one by one. BFS and DFS are those methods: simple, step-by-step procedures that let you explore every possible connection starting from one point without getting lost. Mastering them gives you the ability to navigate any network of relationships, whether it is streets on a map, links between web pages, or colleagues in an organization.
The idea, in plain terms
A graph is a collection of points (called vertices or nodes) connected by lines (called edges). Think of a map of cities with roads between them, or a social network where people are connected by friendships. Now, imagine you are standing at one city/node and want to explore everything reachable from it. There are two natural ways to do it.
Breadth-first search (BFS) works like ripples in a pond: you first visit all nodes that are one edge away, then all nodes that are two edges away, and so on. It expands outward, level by level, like checking every shop on the same street before moving to the next street.
For example, if node A is connected to B and C, BFS visits A, then immediately checks its neighbors B and C, then looks at who B knows and who C knows. It treats all nodes at the current
An analogy
You are in a large house with many rooms connected by doors. You want to explore every room starting from the entrance.
BFS is like exploring with a group of friends: you all stand at the entrance, then each of you picks a different door, and you all step into those adjacent rooms together. Then, from each of those rooms, you all send people through the doors into the next layer of rooms, and so on. You never go two doors deep from the entrance until you've seen every room just one door away. This way, if you stop at any moment, you know you've seen all rooms within a certain distance from the start.
DFS is like exploring with a single rope tied to the entrance: you walk through a door, then another door, always choosing the first available door you haven't tried, going deeper and deeper. If you hit a room with no new doors, you walk back along the rope to the last room where you had an untried door, and go down that next corridor. You completely exhaust one corridor before trying the next.
Where the analogy breaks down: In a real house, you can see the whole room when you enter it, and you know all its doors. In a graph, you only discover a node's neighbours when you actually visit it. Also, in a physical house you can leave markers (string, chalk) to know you've been somewhere; a computer uses a 'visited' list for the same purpose. And while BFS's 'level' is like rooms at the same number of doors from the entrance, in a graph the same node can be reached at multiple distances—BFS records the first (shortest) distance it finds.
Definition
Graph traversal is the process of systematically visiting every vertex of a graph by following its edges; BFS visits all vertices at distance 1, then 2, then 3, etc., while DFS follows one path to its end before backtracking to try another.
Where this sits
You have not studied any other topics yet, so this page must build from absolute scratch. But know that once you have BFS and DFS, you can move on to topics like: Dijkstra's shortest path (which extends BFS to weighted edges), topological sorting (which uses DFS to order dependencies), PageRank (which can be computed using a form of traversal over the web graph), and social network analysis (which uses BFS to compute degrees of separation). Your library notes also mention that 'every binary relation is a directed graph', so these traversal patterns are the basis for reasoning about any relational data.