DSA sheet · Graph
Graph DSA Notebook
My study notes from the Graph playlist. Each page is written so that a beginner (or me, after forgetting everything) can read it once and understand how the graph is built from the input, why BFS or DFS fits, the Python code line by line, and a full dry run with the queue, stack or heap drawn after every step.
The one idea behind every page
A graph is just nodes and the edges between them. Build an adjacency list (each node → its neighbours), keep a visited set so you never loop forever, then walk it: BFS (a queue, level by level, gives shortest paths when every step costs 1) or DFS (recursion or a stack, goes deep first).
A grid is a graph too: each cell is a node and its 4 neighbours are the edges. When edges have different costs, swap the queue for a min-heap (Dijkstra).
A grid is a graph too: each cell is a node and its 4 neighbours are the edges. When edges have different costs, swap the queue for a min-heap (Dijkstra).
Every page follows the same order:
① question in simple words → ② constraints → ③ intuition → ④ building the logic from examples → ⑤ approach steps → ⑥ code → ⑦ line by line → ⑧ dry run with the queue / stack / heap drawn → ⑨ complexity & remember
Start here
- 1Graph Patterns Overviewconcept
- 2Introduction to Graphsterms · storing a graph
- 3BFS Traversalqueue
- 5DFS Traversalrecursion · stack
Connected components & grid traversal
- 4Number of Provincescomponents
- 6Number of Islandsgrid · DFS · BFS
- 16Flood Fillgrid · DFS · BFS
- 18Keys and Roomsreachability
- 7All Paths From Source to TargetDFS · backtracking
Start from the border
Colouring & cycles
- 10Is Graph Bipartite?2-colouring
- 11Cycle in Undirected Graph (DFS)parent check
- 13Cycle in Undirected Graph (BFS)parent check
- 12Cycle in Directed Graph (DFS)path set
Shortest path with BFS (every step costs 1)
- 14Shortest Path in Binary Matrix8 directions
- 1501 Matrixmulti-source BFS
- 17Rotting Orangesmulti-source BFS
- 19Word Ladderwords as nodes
- 20Snakes and Laddersboard as graph
Topological sort
- 21Course ScheduleKahn's algorithm
- 22Course Schedule IItopo order
- 23Find Eventual Safe Statesreversed graph
- 24Alien Dictionaryorder from words
Weighted shortest path (Dijkstra)
- 25Dijkstra's Algorithmmin-heap
- 26Network Delay TimeDijkstra
- 27Cheapest Flights Within K Stopsstops limit
- 28Path With Minimum Effortgrid Dijkstra
Numbers are the order in the playlist. "Next →" at the top of each page follows that order.