LESSON 70 / 75·2 min readIntermediate

Graphs in Python

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Graph representation

Python
graph = {
    'A': ['B', 'C'],
    'B': ['D'],
    'C': ['E']
}

Problems to practice

  • Number of Islands
  • Clone Graph
  • Course Schedule
  • Topological Sort
  • BFS and DFS
  • Shortest path in unweighted graph
  • Union-Find problems

Example: BFS

Python
from collections import deque

def bfs(graph, start):
    visited = {start}
    queue = deque([start])

    while queue:
        node = queue.popleft()
        print(node)
        for neighbor in graph.get(node, []):
            if neighbor not in visited:
                visited.add(neighbor)
                queue.append(neighbor)

PAUSE & RECALL

Can you explain it without the notes?

Graphs in Python

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Graph representation

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Dynamic programming in Python

Adapted from 18_DSA_PYTHON.md · “9. Graphs in Python”. Examples are study fragments; framework behavior can depend on your version.