Skip to content

Data structures and algorithms · Graphs

Graphs

Neighbours, a to-do list, and a set of places already seen. · 12 minutes

A graph is things (nodes) and connections between them (edges): cities and roads, people and friendships, web pages and links. In Python the simplest form is a dictionary from each node to a list of its neighbours.

Graphs can loop back on themselves, so every walk keeps a set of places already seen. Without it, a loop is walked forever.

Predict first

A walk takes places off a to-do list and adds their neighbours. Which kind of list makes it visit the nearest places first?