Things and connections
After this: Name the nodes and edges in a problem, and say whether the edges have a direction.
On this page · 8 sections · 3 checks
Here are four friends: Asha, Ben, Cara and Dev. Asha knows Ben and Cara. Ben knows Dev, and Cara knows Dev too. Asha has never met Dev.
Here is the same information as a drawing. Each person is a circle, and a line joins two people who know each other.
You can answer questions by following the lines.
- Who does Ben know? Follow the lines from Ben, and you reach Asha and Dev.
- Could a friend introduce Asha to Dev? Yes. Ben knows both of them, and so does Cara.
This kind of drawing is called a : some things, and the connections between pairs of those things. This is a different meaning from the graph with two axes that you may know from school. That kind of graph shows numbers. This kind shows connections.
Why this shape is useful
Many real questions have this same shape. Only the things and the connections change.
| Question | The things | The connections |
|---|---|---|
| Who can you reach in two introductions? | people | knows |
| Which file has to be built before this one? | files | needs |
| How many flights from here to Lisbon? | cities | direct flight |
Once you see the shape, a few standard methods can answer questions like these. You do not need a new idea for each problem. This tutorial teaches those methods.
Nodes and edges
Each thing in a graph is called a . Each connection is called an . In the drawing, the circles are nodes and the lines are edges, so the friends graph has 4 nodes and 4 edges.
A node's are the nodes joined to it by an edge. Ben's neighbours are Asha and Dev. Dev is not Asha's neighbour, because no edge joins them directly.
A node can have any number of neighbours, including none. If a fifth person, Eli, joined and knew nobody, Eli would be a node with no edges. Eli would still be part of the graph.
A graph has no other rules. No node comes first, and the nodes have no fixed order. A graph is only its nodes and its edges.
The graph used in this tutorial
Names take up space, so most lessons use letters. Here is the graph they use. A, B, C and D are the four friends, and we add two more nodes, E and F.
We will call it the six-node graph. It has six edges: A–B, A–C, B–D, C–D, D–E and E–F. D has three neighbours (B, C and E). F has one neighbour (E).
With or without direction
Friendship works in both directions. If Asha knows Ben, then Ben knows Asha. One line records both facts.
Some connections work in one direction only. On a social network, Asha can follow Ben while Ben does not follow Asha. A plain line cannot show that, so we draw an arrow from Asha to Ben. If Ben also follows Asha, that is a second, separate arrow.
So an edge can be a plain line, which works both ways, or an arrow, which works one way.
| Arrow (one way) | Plain line (both ways) |
|---|---|
| follows, on a social network | is friends with |
| one-way streets | two-way roads |
| "build this first" | "sits next to" |
| links between web pages | borders between countries |
Walking through a graph
A program uses a graph by moving through it. It starts at one node, follows an edge to a neighbour, and carries on from there. We will call this a walk.
Direction matters here, because a walk can follow an arrow only forwards. Suppose Asha follows Ben and Ben follows Cara. A walk from Asha reaches Ben and then Cara. A walk from Cara reaches nobody, because Cara follows nobody.
Watch it happen
This tool shows four accounts, A to D. An arrow means "follows". You will run a walk from A, and then a walk from D.
The tool has a button called BFS. This is the short name for one way of walking through a graph, and a later lesson teaches it. You do not need the details yet, so you can ignore words such as "queue" in the text. Look only at which nodes change colour. The symbol ∞ under a node means that the walk did not reach it.
- BFS from AStart at A. The walk follows the arrows and reaches every account.
- BFS from D
Pick a start node and run an algorithm.
From A, the walk reached every account. From D, it reached no other account, because D follows nobody and an arrow is never followed backwards.
Check yourself
An airline publishes its network: its cities, and the direct flights between them. In a graph of this network, what are the nodes and what are the edges?
Which of these relationships needs arrows, because it can hold in one direction only?
The six-node graph has nodes A to F and edges A–B, A–C, B–D, C–D, D–E and E–F. How many edges does it have, and how many neighbours does D have?
Connect
The next lesson shows how to store a graph in a program.
- Name the nodes and the edges in a social network, a map or a list of dependencies.
- Tell a one-way connection (follows, depends on) from a two-way connection (is friends with).
- Count a node's neighbours, and say that a node with no neighbours is still part of the graph.