pyPython Practice LabFrom first print to final round
TOPIC 16 OF 18

Graphs and Connectivity Python lessons

Graph representation, BFS, DFS, shortest paths, topological order, and union-find.

14 exercises8+ conceptsBeginner → interview context
Start this topic
WHAT YOU WILL LEARN

Core concepts

Traverse a graph with BFSFind an unweighted shortest pathCount islands in a gridTopologically sort tasksCount connected components with union-findFind a redundant graph edgeCheck if courses can be completedClone an undirected graph
REUSABLE THINKING

Problem-solving patterns

  • graph · BFS
  • shortest path · parent map
  • DFS · grid
  • topological sort · Kahn
  • union-find · graph · company screen
LESSON OUTCOME

What this topic builds

Learn the idea once, trace it through two verified cases, then explain why your approach works.

ORDERED LESSONS

All 14 lessons in this topic

Work from top to bottom to build each concept gradually.

  1. #150
    Traverse a graph with BFS

    Visit a connected graph from vertex zero in deterministic neighbor order.

    BeginnerPremium
  2. #151
    Find an unweighted shortest path

    Find a shortest route in a small unweighted network.

    IntermediatePremium
  3. #152
    Count islands in a grid

    Count separate land regions in a small map.

    IntermediatePremium
  4. #153
    Topologically sort tasks

    Produce one valid order for tasks with prerequisites.

    IntermediatePremium
  5. #154
    Count connected components with union-find

    Count groups of vertices connected by undirected edges.

    AdvancedPremium
  6. #155
    Find a redundant graph edge

    Find the final edge that closes a cycle in an almost-tree graph.

    AdvancedPremium
  7. #156
    Check if courses can be completed

    Determine that every course can be completed in some valid order.

    AdvancedPremium
  8. #157
    Clone an undirected graph

    Deep-copy an undirected graph and print the clone's adjacency list.

    Interview-LevelPremium
  9. #158
    Merge accounts by shared email

    Combine account rows that belong to the same person through a shared email.

    Interview-LevelPremium
  10. #159
    Find a word-ladder length

    Find the shortest valid sequence from hit to cog.

    Interview-LevelPremium
  11. #160
    Evaluate division queries

    Evaluate direct, inverse, chained, and unknown division queries.

    Interview-LevelPremium
  12. #161
    Find network delay time

    Find how long a signal takes to reach every node in a weighted network.

    Interview-LevelPremium
  13. #162
    Find cheapest flight with stop limit

    Choose a cheaper route that uses one allowed stop.

    Interview-LevelPremium
  14. #163
    Infer an alien alphabet

    Infer one deterministic character order from a sorted alien dictionary.

    Interview-LevelPremium