All postsGraph PatternAugust 29, 202645 min readgraphpatternSolve any problem in graph patternIndexA Pattern-Based Guide to Solving Graph Problems1. The Graph Mental ModelUndirectedDirectedWeightedUnweighted2. The Most Important Question3. The Graph Pattern Decision TreePattern 1: Graph RepresentationWeighted graphThe patternPractice ProblemsPattern 2: DFS — Explore Everything ConnectedWhen should you think DFS?Practice ProblemsExample: Number of IslandsPattern 3: BFS — Level-by-Level ExplorationWhy BFS is so importantPractice ProblemsExample: Word LadderPattern 4: BFS on a GridThe critical grid insightPractice ProblemsPattern 5: Connected ComponentsPractice ProblemsExample: Number of ProvincesPattern 6: Cycle Detection — Undirected GraphPractice ProblemsAlternative: Union-Find for Undirected Cycle DetectionPattern 7: Union-Find / DSUDSU TemplateWhen should you think DSU?Practice ProblemsDFS vs BFS vs DSUDFS/BFSDSUPattern 8: Bipartite GraphThe AlgorithmRecognition PatternPractice ProblemsPattern 9: Directed Graph Cycle DetectionPractice ProblemsCourse SchedulePattern 10: Topological SortThe Most Important Recognition PatternPractice ProblemsKahn's AlgorithmKahn TemplateTopological Sort as a General DP PatternPattern 11: Shortest PathPractice ProblemsUnweighted Shortest PathGeneral Shortest PathPattern 12: DijkstraDijkstra TemplateWhy the stale-entry check?Recognition PatternPractice ProblemsPattern 13: 0-1 BFSPractice ProblemsPattern 14: Bellman-FordBellman-Ford PatternPractice ProblemsPattern 15: Floyd-WarshallPractice ProblemsPattern 16: Minimum Spanning TreeShortest PathMinimum Spanning TreeMST PatternPractice ProblemsPattern 17: Kruskal's AlgorithmKruskal TemplatePractice ProblemsPattern 18: Prim's AlgorithmPractice ProblemsPattern 19: Backtracking on GraphsTemplateWhy path[:]?Recognition PatternPractice ProblemsPattern 20: Grid BacktrackingPractice ProblemsPattern 21: Multi-Source BFSExample: Rotting OrangesOther Multi-Source BFS ProblemsPractice ProblemsPattern 22: Distance / Nearest SourcePractice ProblemsPattern 23: State-Space BFSWhy?General State-Space PatternPractice ProblemsPattern 24: Graph + BitmaskPractice ProblemsPattern 25: Eulerian Path / CircuitEulerian RecognitionPractice ProblemsPattern 26: Strongly Connected ComponentsPractice ProblemsPattern 27: DAG + Dynamic ProgrammingPractice ProblemsPattern 28: Implicit GraphsWord LadderSliding puzzleChess problemsGridPractice ProblemsPattern 29: The "State Transition" Way of ThinkingPractice ProblemsPattern 30: Choosing the Algorithm31. The Most Important Pattern: Read the Question Semantically"Can I reach X?""How many groups are there?""What is the minimum number of moves?""What is the minimum cost?""Can I finish all tasks?""Give me a valid ordering""Connect everything as cheaply as possible""Are these two things already connected?""Can I split nodes into two groups?""Find every possible path"32. The Universal DFS TemplateDFS Practice Set33. The Universal BFS TemplateBFS Practice Set34. BFS Level PatternPractice Problems35. The Universal Dijkstra TemplateDijkstra Practice Set36. The Universal DSU TemplateDSU Practice Set37. Graph Complexity38. Common MistakesMistake 1: Forgetting visitedMistake 2: Marking visited too late in BFSMistake 3: Using Dijkstra for unweighted graphsMistake 4: Using BFS for weighted shortest pathMistake 5: Confusing shortest path with MSTMistake 6: Forgetting that grids are graphsMistake 7: Using permanent visited in backtracking39. A Practical LeetCode Recognition Cheat Sheet"Reach""Connected""Components""Minimum number of moves""Minimum time""Cheapest""Prerequisite""Dependency""Ordering""Cycle""Two groups""Every edge""All paths""Keys / state / remaining moves"40. A Better Way to Practice GraphsStage 1 — TraversalStage 2 — Graph PropertiesStage 3 — Directed GraphsStage 4 — Shortest PathStage 5 — ConnectivityStage 6 — MSTStage 7 — Advanced Graphs41. The Real Skill: Pattern Recognition42. The Graph Problem-Solving FrameworkStep 1 — Identify the nodesStep 2 — Identify the edgesStep 3 — Determine directionStep 4 — Determine edge weightsStep 5 — Determine the objectiveStep 6 — Choose the pattern43. The Most Important Graph Templates to MemorizeDFSBFSMulti-source BFSTopological SortDijkstraDSU44. Final Mental Model45. The One-Page Graph Decision TreeConclusionRelatedSliding Window Pattern19 min readsliding-windowpatternDynamic Programming Pattern39 min readpatterndynamic programming