Graphs and Connectivity Python practice problems
Graph representation, BFS, DFS, shortest paths, topological order, and union-find.
Core concepts
Problem-solving patterns
- graph · BFS
- shortest path · parent map
- DFS · grid
- topological sort · Kahn
- union-find · graph · company screen
What this topic builds
Learn the idea once, use it across the exercises, and explain why your approach works.
All 28 problems in this topic
Work from top to bottom to build the concept gradually.
- #299Traverse a graph with BFS: Connected graphBeginnerPremium
Visit a connected graph from vertex zero in deterministic neighbor order.
- #300Traverse a graph with BFS: Disconnected graphBeginnerPremium
Visit only the component reachable from the chosen start.
- #301Find an unweighted shortest path: Route existsIntermediatePremium
Find a shortest route in a small unweighted network.
- #302Find an unweighted shortest path: Unreachable destinationIntermediatePremium
Report when start and target lie in disconnected components.
- #303Count islands in a grid: Three islandsIntermediatePremium
Count separate land regions in a small map.
- #304Count islands in a grid: All waterIntermediatePremium
Return zero when the grid contains no land.
- #305Topologically sort tasks: Valid build orderIntermediatePremium
Produce one valid order for tasks with prerequisites.
- #306Topologically sort tasks: Cyclic dependenciesAdvancedPremium
Report Cycle when no complete topological order exists.
- #307Count connected components with union-find: Two graph componentsAdvancedPremium
Count groups of vertices connected by undirected edges.
- #308Count connected components with union-find: No edgesAdvancedPremium
Every isolated vertex forms its own component.
- #309Find a redundant graph edge: Triangle cycleAdvancedPremium
Find the final edge that closes a cycle in an almost-tree graph.
- #310Find a redundant graph edge: Longer cycleAdvancedPremium
Detect the edge that reconnects vertices already joined through another route.
- #311Check if courses can be completed: Acyclic prerequisitesAdvancedPremium
Determine that every course can be completed in some valid order.
- #312Check if courses can be completed: Circular prerequisitesAdvancedPremium
Reject a plan where two courses depend on each other.
- #313Clone an undirected graph: Connected graph cloneInterview-LevelPremium
Deep-copy an undirected graph and print the clone's adjacency list.
- #314Clone an undirected graph: Include isolated nodeInterview-LevelPremium
Preserve a vertex even when it has no neighbors.
- #315Merge accounts by shared email: Shared email mergeInterview-LevelPremium
Combine account rows that belong to the same person through a shared email.
- #316Merge accounts by shared email: No shared emailsInterview-LevelPremium
Keep every account separate when no email appears twice.
- #317Find a word-ladder length: Reachable transformationInterview-LevelPremium
Find the shortest valid sequence from hit to cog.
- #318Find a word-ladder length: Missing destinationInterview-LevelPremium
Return zero when the destination is absent from the dictionary.
- #319Evaluate division queries: Connected ratiosInterview-LevelPremium
Evaluate direct, inverse, chained, and unknown division queries.
- #320Evaluate division queries: Disconnected variablesInterview-LevelPremium
Return -1 when no path connects two known variables.
- #321Find network delay time: All nodes reachedInterview-LevelPremium
Find how long a signal takes to reach every node in a weighted network.
- #322Find network delay time: Unreachable nodeInterview-LevelPremium
Return -1 when at least one node cannot receive the signal.
- #323Find cheapest flight with stop limit: Cheaper one-stop routeInterview-LevelPremium
Choose a cheaper route that uses one allowed stop.
- #324Find cheapest flight with stop limit: Stop limit blocks routeInterview-LevelPremium
Return the direct price when a cheaper multi-edge route uses too many stops.
- #325Infer an alien alphabet: Valid alien orderInterview-LevelPremium
Infer one deterministic character order from a sorted alien dictionary.
- #326Infer an alien alphabet: Invalid prefix orderInterview-LevelPremium
Reject a longer word that incorrectly appears before its exact prefix.