Codele / Data structures
Graphs
Friends, towns joined by roads, web pages with links: lots of things are a set of things plus the connections between them. That picture is a graph.
- A graph is things (nodes) plus the connections between them (edges).
- Neighbours are the nodes joined to one node by a single edge.
- An adjacency list is a dictionary: each node maps to the list of its neighbours.
- A two-way connection (friends) appears in both lists. A one-way road appears only in the list it starts from.
- Unlike a tree, a graph can have loops and no single top.
Loading the playable lesson…