Lesson 8 of 73 1 min

DSA Masterclass Module 8: Graphs (BFS, DFS, TopoSort)

Master non-linear data structures. Learn how to represent graphs using adjacency lists and solve complex connectivity and dependency problems.

Introduction to Graphs

A Graph is a collection of nodes (vertices) connected by edges. Unlike trees, graphs can have cycles, multiple paths between nodes, and unconnected components.

1. Real-World Intuition: Social Networks

Think of LinkedIn or Twitter:

  • Vertices: Users.
  • Edges: "Follows" or "Connections."
  • Direction: Twitter is a Directed Graph (A follows B doesn't mean B follows A). LinkedIn is an Undirected Graph.

2. Curriculum in this Module

  1. Theory & Intuition (Current Page)
  2. Problem: Number of Islands - Mastering Grid BFS/DFS.
  3. Lesson: Topological Sort - Handling dependencies.
  4. Problem: Course Schedule - Detecting cycles in graphs.
  5. Lesson: Dijkstra vs Bellman-Ford - Shortest Path mastery.
  6. Curated Practice Problems - 10 essential challenges.

3. Graph Representation (Java)

List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
adj.get(u).add(v); // Edge from u to v

Final Takeaway

Graph problems are often just Tree problems with a visited set. To prevent infinite loops in cycles, always track where you've been.

Want to track your progress?

Sign in to save your progress, track completed lessons, and pick up where you left off.