BFS
bfs
Data Structures
Queue & Deque
BFS visits a graph one frontier at a time, and the trick that makes that work is a queue: nodes are added in the order they are discovered and pulled out in the same order, level by level. **Queue & Deque** captures that First In, First Out discipline as a reusable abstraction, then extends it to a double-ended variant that pops up surprisingly often in sliding-window problems. In this lesson, you will implement `enqueue`, `dequeue`, `front`, and `isEmpty` using both a linked list and an array, then see why a naive array queue wastes space and how a circular queue with two index pointers fixes that with `O(1)` operations. You will also build a deque that supports inserts and removes at both ends, the structure underneath the monotonic-deque pattern that solves sliding-window-maximum in linear time. In **Stack (LIFO)**, you implemented `push` and `pop` from the same end and saw how the choice of array versus linked list affected the worst-case bound. A queue uses the same two storage options but pulls from the opposite end, which is what forces the circular buffer trick when you back it with an array. With a queue in your toolkit, **Hash Map (Dictionary) Basics** introduces the most-used data structure in real-world code: a key-value store that answers lookups in expected `O(1)`.
Not Started
0%
Algorithms
BFS & DFS (Intro)
Swap a queue for a stack inside a graph traversal and you flip from breadth-first to depth-first behavior. The visited-vertex tracking is identical, the neighbor expansion is identical, and yet one explores level by level while the other dives down the longest path first. That single data-structure swap is the entire difference between the two most-used graph algorithms in the field. **BFS & DFS (Intro)** walks through both. For BFS you will implement queue-based level-by-level exploration and use it to find shortest paths in unweighted graphs. For DFS you will see both the recursive form (which uses the call stack implicitly) and the iterative form with an explicit stack, and apply them to connectivity checks, path finding, and cycle detection. The lesson also covers the visited set that prevents infinite loops on cyclic graphs and contrasts graph traversal with tree traversal as a special case. This lesson assumes you are comfortable with three structures from the data-structures track: **Stack (LIFO)** powers iterative DFS, **Queue & Deque** powers BFS, and **Graphs: Representation Basics** gives you the adjacency list or matrix that both algorithms read from. Next, **Hashing Techniques** turns to a different building block, the hash map, which underpins many of the lookup-heavy patterns you will combine with traversal in later lessons.
Not Started
0%
Advanced Graph Algorithms (Network Flow)
Two seemingly different problems, the maximum amount of water you can push from a source to a sink through a network of capacitated pipes, and the minimum total capacity you must remove to disconnect the sink from the source, turn out to have the same answer for every graph. The max-flow min-cut theorem ties them together, and once you accept it a surprising number of optimization problems (image segmentation, project selection, bipartite matching) collapse into max-flow instances. **Advanced Graph Algorithms (Network Flow)** introduces the algorithms that compute that maximum flow. You will work through the Ford-Fulkerson method built on augmenting paths and the residual graph, the BFS-based Edmonds-Karp variant with its `O(V * E^2)` guarantee, and Dinic's algorithm which uses level graphs and blocking flows to reach `O(V^2 * E)`. The lesson then turns to maximum bipartite matching (Hungarian and Hopcroft-Karp), applications like airline scheduling and image segmentation, and a first look at minimum-cost flow. In **Graph Algorithms (Advanced)**, you handled MST, all-pairs shortest paths, and SCCs on edge-weighted graphs. Network flow keeps the weighted-edge model but flips the question from "shortest" to "highest throughput." Next, **String Algorithms** turns to pattern matching, the implicit automaton of a regular expression.
Not Started
0%
Practice Problems
Flood Fill
Given a 2D grid representing an image, a starting pixel, and a new color, perform a flood fill (like a paint bucket tool) to recolor the connected region.
Invert Binary Tree
Given the root of a binary tree, invert the tree (mirror it) and return its root.
Symmetric Tree
Given the root of a binary tree, check whether it is a mirror of itself (symmetric around its center).
Alien Dictionary
Given a sorted list of words in an alien language, determine the order of characters in the alien alphabet using topological sort.
Serialize and Deserialize Binary Tree
Design an algorithm to serialize a binary tree to a string and deserialize the string back to the original tree.
Swim in Rising Water
Given an n x n grid of elevations, find the minimum time t at which you can swim from the top-left to the bottom-right corner.
Word Ladder
Given a begin word, end word, and a dictionary, find the length of the shortest transformation sequence where each step changes exactly one letter.
Average of Levels in Binary Tree
Given the root of a binary tree, return the average value of the nodes on each level as an array.
Binary Tree Level Order Traversal
Given the root of a binary tree, return the level order traversal of its nodes' values (from left to right, level by level).
Binary Tree Right Side View
Given the root of a binary tree, return the values of the nodes you can see when looking at the tree from the right side, ordered from top to bottom.
Binary Tree Zigzag Level Order Traversal
Given the root of a binary tree, return the zigzag level order traversal of its nodes' values (alternating left-to-right and right-to-left for each level).
Cheapest Flights Within K Stops
Find the cheapest price to fly from a source to a destination with at most k intermediate stops, given a list of flights with prices.
Clone Graph
Given a reference to a node in a connected undirected graph, return a deep copy (clone) of the entire graph.
Course Schedule II
Given courses and prerequisites, return a valid order to take all courses (topological ordering), or an empty array if impossible.
Course Schedule
Given a number of courses and their prerequisites, determine if it is possible to finish all courses (detect if the dependency graph has a cycle).
Evaluate Division
Given equations like a/b = k, answer queries asking for the result of x/y using graph traversal on a weighted directed graph.
Jump Game II
Given an integer array where each element represents the maximum jump length at that position, find the minimum number of jumps needed to reach the last index.
Max Area of Island
Given a binary grid, find the maximum area of an island (connected group of 1s connected 4-directionally).
Minimum Height Trees
Given a tree of n nodes, find all roots that would minimize the tree's height when the tree is rooted at them.
Number of Islands
Given a 2D grid of '1's (land) and '0's (water), count the number of distinct islands formed by connected land cells.
Pacific Atlantic Water Flow
Given a matrix of heights, find all cells from which water can flow to both the Pacific and Atlantic oceans.
Populating Next Right Pointers II
Given a binary tree, populate each node's next pointer to point to its next right node. If there is no next right node, set it to null.
Rotting Oranges
Given a grid where cells contain fresh oranges, rotten oranges, or are empty, determine the minimum time for all fresh oranges to rot via adjacency spreading.
Snakes and Ladders
Given a Snakes and Ladders board, find the minimum number of dice rolls to reach the final square from square 1.
Surrounded Regions
Given a board of 'X' and 'O', capture all regions of 'O' that are completely surrounded by 'X' (not touching the border).
Walls and Gates
Given a grid with walls, gates, and empty rooms, fill each empty room with the distance to its nearest gate using multi-source BFS.
01 Matrix
Given a binary matrix, find the distance of each cell to the nearest 0 using multi-source BFS.
Code Snippets
BFS Traversal Template
Breadth-first search visits every reachable node in non-decreasing distance from the start, which makes it the right algorithm for shortest-path-in-unweighted-graph, level-order tree traversal, and grid-flood problems. This snippet covers the canonical queue + visited skeleton, the level-by-level form for tree-style problems, and the multi-source variant for problems like 'rotting oranges' where many start points share a frontier.
Topological Sort (Kahn's Algorithm)
Topological sort orders the nodes of a DAG so that every edge points from earlier to later. It is the algorithm behind build-system dependency resolution, course-prerequisite scheduling, and pipelined computation. This snippet covers Kahn's BFS-based algorithm with indegree tracking, a DFS-based variant that produces the same order via post-order, and a cycle-detection variant that returns null when the graph is not acyclic.
Python BFS Template
Breadth-first search visits nodes in order of distance from the start, which makes it the right tool for shortest-path-by-edge-count, level-order traversal, and 'fewest steps' search problems. Python's `collections.deque` gives you O(1) `popleft`, which is the difference between BFS and an O(n^2) accident. This entry covers the standard template, distance tracking, and a level-by-level variant.
Question Banks
BFS vs DFS Fundamentals
Pin down the queue-vs-stack mechanics behind BFS and DFS, plus when level-by-level versus deep-first traversal is the right tool.
Grid Search Patterns
Implement and trace classic grid problems: counting islands, flood fill, and multi-source distance maps. Code stems are mostly Python.
Community
Shortest Path in Binary Matrix
BFS through 0-cells in 8 directions to find the shortest path from top-left to bottom-right.
Graph Algorithms 101
Why most production graph work is modeling, not algorithm; the two traversals you actually use; and the representation table that drives every memory tradeoff.
Cousins in Binary Tree
Decide whether two values in a binary tree are cousins (same depth, different parents).
Is Graph Bipartite?
Decide whether an undirected graph admits a 2-coloring with no edge sharing a color, using BFS over each component.
Open the Lock
Find the minimum wheel turns to open a 4-digit combination lock while avoiding deadend states.
Minimum Genetic Mutation
Transform a gene string to a target one mutation at a time, where each intermediate must be in the bank.
