Programming foundations

Stage 4 · Lesson 10 of 10

Graphs at a first look

Data structures and Big-O

A is a set of nodes joined by edges (links), with no single root. Examples are cities and roads, people and friendships, or web pages and links. Edges can be one-way (directed) or two-way, and they can carry a weight, such as a distance.

Most code stores a graph as an adjacency list: a map from each node to its neighbours. There are two basic ways to walk a graph. Breadth-first search uses a and explores the nearest nodes first.

Depth-first search uses or a and goes as deep as it can first. The classic bug is forgetting to mark visited nodes, which forever when the graph has a cycle. The DSA roadmap goes much deeper; here, learn to recognise a graph and walk one.

A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • Step through linked lists, hash tables and sorts with your own input

  • Chapter 5 on lists, dicts, sets and deque: the built-ins you will use from now on

Also worth your time

Mark this stage as not done?