BeginnerCoder

Lesson 8 — Graph as adjacency list

Graphs as adjacency lists

A graph is a set of nodes joined by edges — friends in a social network, cities joined by roads. The simplest way to store it is an adjacency list: a dict mapping each node to the list of its neighbours. For an undirected graph (roads work both ways) each edge a–b is added twice: b to a's list and a to b's.

graph = {"A": ["B"], "B": ["A"]}

The number of neighbours a node has is its degree.

Why it matters: this dict-of-lists is the starting point for every graph algorithm (BFS, DFS, Dijkstra).

Want it another way?
Task

For each edge add both directions. Expected: each node with its neighbours and degree.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.