Week 30 Module

Week 30 of 43
Advanced DSA
WEEK 30 LEARNING MODULE

Depth-First Search (DFS) & Graph Traversals

Covers graph DFS traversal using recursion and explicit stacks, visited set tracking, connected component counting, flood fill, and 2D grid graph traversal.

5 Practice Problems4 Core ConceptsVerified Curriculum: high Confidence

What You'll Learn

  • Master Depth-First Search (DFS) on graphs using recursion and explicit stacks.
  • Maintain a `visited` set to prevent infinite loops in cyclic graphs.
  • Model 2D grid matrices as implicit graphs with 4-directional or 8-directional neighbor transitions.
  • Count connected components (Number of Islands, Number of Provinces) and solve Flood Fill.
  • Analyze DFS complexities: $O(V + E)$ time and $O(V)$ space.

Core Concepts

Graph DFS Paradigm

O(V + E) time, O(V) space

Traversing as deep as possible along each branch before backtracking.

Visited Set Management

O(1) lookup

Tracking visited vertices to avoid cycles and redundant processing.

Implicit 2D Grid Graphs

O(R * C)

Treating matrix cells as vertices with edges between adjacent cells (r±1, c) and (r, c±1).

Connected Components Counting

O(V + E)

Iterating over all vertices, triggering a new DFS whenever an unvisited vertex is encountered.

Algorithms Covered
Graph DFS ($O(V + E)$)Flood Fill AlgorithmNumber of Islands (Grid DFS)Max Area of IslandCycle Detection in Undirected Graphs
Data Structures Applied
GraphsCall StackVisited Hash Set

Turn your face to the sun and the shadows fall behind you.

Maori Proverb

Lecture Materials (1)

Official lecture slide decks hosted on Google Drive
Official Slide Deck2.6 MB

DFS Lecture .pdf

Topic: DFS Lecture

Format: PDF

Curated Practice Problems (5)

Track problem completions synced across your curriculum