What is a graph?
In everyday speech a "graph" is usually a chart with axes, like a line showing temperature over a week. In mathematics the word means something different and much simpler.
A graph is a collection of things together with a record of which pairs of things are connected. That's it. We draw each thing as a dot and each connection as a line between two dots.
Here are six friends. A line between two people means they know each other.
From the picture you can answer questions straight away. Does Ana know Dev? No, there is no line between them. But Ana could be introduced to Dev through Ben, because Ana–Ben and Ben–Dev are both lines. Who has the fewest friends? Fay, with just one.
Definition
A graph G is made of two things:
- a set V of vertices (the dots), and
- a set E of edges, where each edge joins a pair of vertices (the lines).
We write G = (V, E). One dot is a vertex; several are vertices.
Written out in this notation, the friendship graph is:
V = {Ana, Ben, Cara, Dev, Eli, Fay}
E = {Ana–Ben, Ana–Cara, Ben–Cara, Ben–Dev, Cara–Eli, Dev–Eli, Eli–Fay}
So this graph has 6 vertices and 7 edges. The picture and the two lists carry exactly the same information. The picture is easier for people to read; the lists are easier to type into a computer.
Graphs are everywhere
Once you know what to look for, you see graphs all over the place. The trick is always to ask: what are the things, and what counts as a connection?
| Situation | Vertices are… | Edges are… |
|---|---|---|
| Social network | people | friendships |
| Road map | towns and junctions | roads |
| Metro map | stations | track between neighbouring stations |
| The web | web pages | links from one page to another |
| Chemistry | atoms | chemical bonds |
| Electricity grid | power stations, substations, homes | cables |
| A university course | modules | "you must pass this one before that one" |
| Sudoku | the 81 squares | "these two squares can't hold the same digit" |
The last row is a good example of how flexible the idea is. Nothing in a Sudoku puzzle looks like dots and lines, but the rules are a graph in disguise. We'll come back to it in Unit 9.
Try it. Draw the graph with vertices V = {1, 2, 3, 4} and edges E = {1–2, 2–3, 3–4, 4–1}. What shape do you get?
Show answer
A square, or "a 4-cycle": each vertex joined to the next, with the last joined back to the first. You might have drawn it as a diamond or a lopsided loop instead. That's also correct, as the next section explains.
Only the connections matter
A graph is who is connected to whom, nothing more. Where you put the dots, how long the lines are and whether they are straight or curvy are just choices about the drawing. They are not part of the graph.
Look at these two pictures. They look nothing alike, but check the edges one by one: both have exactly A–B, B–C, C–D, D–E and E–A.
Drawing 1
Drawing 2
In drawing 2, lines cross in the middle. Those crossing points are not vertices. They are just places where the ink happens to overlap. Vertices are only the dots we deliberately put there.
Watch out
Beginners often count crossings as extra vertices, or think two graphs are different because they look different. Always go back to the lists: same vertices and same edges means same graph. When two graphs have the same pattern of connections, even if the vertex names differ, mathematicians call them isomorphic, from the Greek for "same shape".
The basic vocabulary
A handful of words do most of the work in graph theory. We'll use the friendship graph from above for every example.
- Endpoints
- The two vertices an edge joins. The edge Ben–Dev has endpoints Ben and Dev.
- Adjacent
- Two vertices are adjacent if an edge joins them. Ben and Cara are adjacent; Ana and Dev are not.
- Neighbours
- All the vertices adjacent to a given vertex. The neighbours of Eli are Cara, Dev and Fay.
- Incident
- An edge is incident to the vertices at its ends. The edge Eli–Fay is incident to Eli and to Fay.
Loops and multiple edges
Sometimes it's useful to allow two odd kinds of edge:
- A loop is an edge that starts and ends at the same vertex. Think of a scenic road that leaves a town and comes back to it.
- Multiple edges (or parallel edges) are two or more edges joining the same pair of vertices. Think of two different bridges between the same two riverbanks.
Simple graph
Multigraph
Definition
A simple graph has no loops and never more than one edge between the same two vertices. A graph that allows these is called a multigraph.
Most of this course is about simple graphs, so "graph" means simple graph unless we say otherwise. Multigraphs will be important in Unit 6, where we solve a famous puzzle about seven bridges.
Try it. In the friendship graph, list the neighbours of Ben. Is Cara adjacent to Dev?
Show answer
Ben's neighbours are Ana, Cara and Dev. Cara is not adjacent to Dev: there is no edge Cara–Dev. They are connected only indirectly, for example through Ben or through Eli.
Degree: counting connections
The single most useful number you can attach to a vertex is how many edges touch it.
Definition
The degree of a vertex v, written deg(v), is the number of edges incident to it. In a simple graph, that's the same as its number of neighbours.
In the friendship graph, degree just means "number of friends". Hover over or tap the people in the first picture to check these values:
| Person | Neighbours | Degree |
|---|---|---|
| Ana | Ben, Cara | 2 |
| Ben | Ana, Cara, Dev | 3 |
| Cara | Ana, Ben, Eli | 3 |
| Dev | Ben, Eli | 2 |
| Eli | Cara, Dev, Fay | 3 |
| Fay | Eli | 1 |
A few related words:
- A vertex of degree 0 is isolated: nobody is connected to it. A new member of a social network who hasn't added anyone yet.
- A vertex of degree 1 is a leaf (or pendant vertex). Fay is a leaf. Leaves will matter a lot when we study trees in Unit 3.
- The degree sequence is the list of all degrees, usually from largest to smallest. Here it is (3, 3, 3, 2, 2, 1).
- The largest degree is written Δ (capital delta) and the smallest δ (small delta). Here Δ = 3 and δ = 1.
Watch out
In a multigraph, a loop adds 2 to the degree of its vertex, not 1, because both of its ends touch that vertex. In the multigraph picture above, Y has degree 5: one each from X, Z and W, plus 2 from the loop.
Example: degree in the real world
In an airline network where airports are vertices and direct routes are edges, a "hub" airport is simply a vertex with very high degree. If a hub closes, many routes vanish at once. That's why network engineers pay close attention to high-degree vertices.
The handshake lemma
Here is our first real theorem. It's simple, but it's used constantly.
Imagine a party. As people arrive, some of them shake hands. At the end, everyone writes down how many hands they shook, and someone adds up all the numbers. Whatever happened at the party, the total is always even. Why?
Because every handshake involves exactly two hands. Each handshake gets counted once by each of the two people, so it adds exactly 2 to the total.
The same is true for any graph. Each edge has two ends, and each end adds 1 to the degree of the vertex it touches. In the picture below, every pink dot is one "end of an edge":
Checking with our friends: 2 + 3 + 3 + 2 + 3 + 1 = 14, and 2 × 7 = 14. ✓
The odd-degree consequence
The sum of all degrees is even. Even-degree vertices add an even amount, so the odd-degree vertices together must also add up to an even number. A sum of odd numbers is only even when there's an even count of them. So:
Corollary
In every graph, the number of vertices with odd degree is even.
In the friendship graph, Ben, Cara, Eli and Fay have odd degree. That's four people, an even number, as promised.
Example: an impossible club
Can 7 people each be friends with exactly 3 of the others?
If so, the degrees would add up to 7 × 3 = 21. But the sum of degrees must be even. So no matter how cleverly you arrange the friendships, it can't be done. We proved something is impossible without trying a single arrangement. That's the power of a good theorem.
Example: counting edges without drawing
A network has 10 computers, and each one is cabled directly to exactly 4 others. How many cables are there?
The sum of degrees is 10 × 4 = 40, so the number of edges is 40 ÷ 2 = 20 cables.
Try it. Answer each question using the handshake lemma.
- A graph has degree sequence (4, 3, 3, 2, 2). How many edges does it have?
- Can a graph have 5 vertices, each of degree 3?
- Can a simple graph have degree sequence (3, 3, 3, 1)? (Careful.)
Show answers
- The sum is 4 + 3 + 3 + 2 + 2 = 14, so there are 14 ÷ 2 = 7 edges.
- No. The sum would be 5 × 3 = 15, which is odd.
- No, even though the sum (10) is even. With only 4 vertices, a vertex of degree 3 must be joined to all three others. There are three such vertices, so each of them is joined to the fourth vertex. That gives the fourth vertex degree 3, not 1. The handshake lemma tells you when something is impossible, but an even sum alone doesn't guarantee a graph exists.
Directed graphs
Friendship goes both ways: if Ana knows Ben, Ben knows Ana. But many connections are one-way. On Instagram, you can follow someone who doesn't follow you back. A one-way street lets traffic go in only one direction. A web page can link to another that doesn't link back.
For these we use a directed graph (or digraph), where each edge has a direction, drawn as an arrow. Directed edges are often called arcs.
Now each vertex has two kinds of degree:
- The in-degree is the number of arrows coming in: here, the number of followers.
- The out-degree is the number of arrows going out: the number of accounts that person follows.
| Account | Followed by | In-degree | Follows | Out-degree |
|---|---|---|---|---|
| Maya | Leo, Zara | 2 | Leo | 1 |
| Leo | Maya, Omar | 2 | Maya, Zara | 2 |
| Zara | Leo, Omar | 2 | Maya | 1 |
| Omar | nobody | 0 | Leo, Zara | 2 |
The handshake idea still works, with a twist. Every arrow has exactly one tail (where it leaves) and one head (where it arrives). So:
Try it. You see posts from the accounts you follow. If Omar shares a post, who sees it?
Show answer
Nobody. Omar's post reaches his followers, the people with an arrow pointing into Omar. There are none: his in-degree is 0. Omar sees Leo's and Zara's posts, but nobody sees his. Direction matters.
Weighted graphs
Sometimes a connection isn't just there or not there; it has a size. A road has a length, a flight has a price, a cable has a capacity. A weighted graph attaches a number, called a weight, to each edge.
Weights let us ask much richer questions. "Can I walk from Home to the Library?" is easy: yes. "What's the quickest way?" needs the weights. Here are three possible routes:
| Route | Time |
|---|---|
| Home → School → Library | 12 + 3 = 15 minutes |
| Home → Park → Shop → Library | 5 + 4 + 7 = 16 minutes |
| Home → Park → School → Library | 5 + 6 + 3 = 14 minutes |
The direct-looking route through School isn't the fastest; the detour through the Park wins. Checking every route by hand works for a tiny town but becomes hopeless for a whole country. In Unit 5 you'll learn Dijkstra's algorithm, the idea behind every map app's "fastest route" button.
Try it. What is the quickest walk from Home to the Shop? And from School to the Shop?
Show answer
Home to Shop: Home → Park → Shop, 5 + 4 = 9 minutes.
School to Shop: School → Park → Shop, 6 + 4 = 10 minutes. The other option, School → Library → Shop, takes 3 + 7 = 10 minutes too, so there are two equally quick routes.
Weights and directions can combine. A map of one-way streets with travel times is a weighted directed graph. So is a table of flight prices, since flying A → B often doesn't cost the same as B → A.
Graphs worth knowing by name
Some patterns turn up so often that they have their own names and symbols. Knowing them is like knowing "square" and "triangle" in geometry.
Complete graphs: everyone connected to everyone
The complete graph Kn has n vertices with every possible edge between them.
K10 edges
K21 edge
K33 edges
K46 edges
K510 edges
How many edges does Kn have? Use the handshake lemma. Each of the n vertices is joined to the other n − 1, so the degrees add up to n(n − 1). Halving gives:
Example: a round-robin tournament
In a league where every team plays every other team once, the teams are vertices and the matches are the edges of a complete graph. With 6 teams there are 6 × 5 ÷ 2 = 15 matches. With 20 teams, as in many football leagues, it's 20 × 19 ÷ 2 = 190 pairings, or 380 matches when each pair plays home and away.
Other families
Path P5vertices in a line
Cycle C6a closed ring
Starone centre, many leaves
Bipartite K2,3two groups, edges only across
Empty graphvertices, no edges
Complete K5every edge present
- A path Pn is n vertices in a row, each joined to the next. Like stops along a single bus route. It has n − 1 edges.
- A cycle Cn is a path whose ends are joined up into a ring, like seats around a round table. It has n edges and every vertex has degree 2.
- A star has one central vertex joined to all the others, and no other edges. Old-style office networks wired every computer to one central switch.
- A graph is bipartite if its vertices split into two groups so that every edge goes between the groups, never inside one. Students and the clubs they belong to form a bipartite graph: a student joins a club, but students don't "join" students. In the complete bipartite graph Km,n every one of the m vertices on one side is joined to every one of the n on the other, giving m × n edges. Unit 7 is all about bipartite graphs.
- The empty graph has vertices but no edges at all. Not very exciting, but useful as a starting point.
Try it.
- A chess club of 8 players holds a round robin. How many games are played?
- How many edges does K3,4 have?
- Is the cycle C6 bipartite? Is C5?
Show answers
- 8 × 7 ÷ 2 = 28 games.
- 3 × 4 = 12 edges.
- C6 is bipartite: colour the vertices alternately red, blue, red, blue, red, blue around the ring, and every edge joins a red to a blue. C5 is not: if you alternate around five vertices, the first and last end up the same colour, and they're joined by an edge. In general, a cycle is bipartite exactly when it has an even number of vertices.
Subgraphs
Often we care about only part of a graph. A subgraph is what you get by keeping some of the vertices and some of the edges, with one rule: you can only keep an edge if you also keep both its endpoints.
A group where everyone knows everyone else, like Ana, Ben and Cara, is a complete graph sitting inside a bigger graph. These groups are called cliques, just like in everyday speech. Social networks search for them to suggest groups and communities.
Try it. Is there another triangle (3 people who all know each other) anywhere in the friendship graph?
Show answer
No. Ana–Ben–Cara is the only one. For example, Ben, Dev and Eli would need the edge Ben–Eli, which doesn't exist. Ben, Cara, Dev, Eli form a loop of four (Ben–Cara–Eli–Dev–Ben), but a loop of four isn't a triangle.
Writing a graph down
Pictures are great for people, but a computer needs the graph as data. There are three common ways to write one down. We'll use this small graph:
1. Edge list
Just list the edges: A–B, A–C, B–C, C–D. It's simple and compact, but slow to answer "who are C's neighbours?" because you have to read the whole list.
2. Adjacency matrix
| A | B | C | D | sum | |
|---|---|---|---|---|---|
| A | 0 | 1 | 1 | 0 | 2 |
| B | 1 | 0 | 1 | 0 | 2 |
| C | 1 | 1 | 0 | 1 | 3 |
| D | 0 | 0 | 1 | 0 | 1 |
3. Adjacency list
- A BC
- B AC
- C ABD
- D C
The adjacency matrix is a grid with one row and one column per vertex. Put a 1 where the row's vertex and the column's vertex are adjacent, and a 0 otherwise. It works like the distance chart in a road atlas, where you look up a row and a column to find the answer. Three things to notice:
- Each row adds up to the degree of that vertex (pink numbers). That's the handshake lemma hiding in a table.
- For an undirected graph the grid is symmetric: the entry for row A, column B equals the entry for row B, column A, because A–B and B–A are the same edge.
- The diagonal is all 0s, because a simple graph has no loops.
The adjacency list gives each vertex its own list of neighbours, like each person's phone contacts. It's the most common choice in real software.
| Adjacency matrix | Adjacency list | |
|---|---|---|
| "Are X and Y adjacent?" | Instant: look up one cell | Scan X's list |
| "List X's neighbours" | Scan a whole row | Instant: read X's list |
| Storage needed | Always n × n cells | Grows with the number of edges |
| Best for | Small or dense graphs | Large graphs with few edges per vertex |
Example: why it matters
A social network with 1 billion users would need a matrix with 1018 cells, far more than any computer can hold. Most of those cells would be 0, since nobody has a billion friends. With adjacency lists, a user with 300 friends just stores those 300 names.
Try it.
- Write the adjacency list of the friendship graph.
- For a directed graph, the matrix has a 1 in row X, column Y when there's an arrow X → Y. Why might this matrix not be symmetric?
Show answers
- Ana: Ben, Cara. Ben: Ana, Cara, Dev. Cara: Ana, Ben, Eli. Dev: Ben, Eli. Eli: Cara, Dev, Fay. Fay: Eli.
- Because an arrow can go one way without a matching arrow back. In the follow graph, Omar → Leo exists but Leo → Omar doesn't, so the row Omar, column Leo cell is 1 while the row Leo, column Omar cell is 0. Row sums now give out-degrees and column sums give in-degrees.
Practice set
Mixed questions covering the whole unit. Try each one before opening the answer.
1. A graph has 12 edges. What is the sum of its degrees?
Show answer
2 × 12 = 24.
2. At a meeting, 9 people each shake hands with exactly 5 others. Is this possible?
Show answer
No. The total would be 9 × 5 = 45, which is odd, but a sum of degrees is always even.
3. What is the largest degree any vertex can have in a simple graph with 7 vertices?
Show answer
6. With no loops or repeated edges, a vertex can be joined at most once to each of the other 6 vertices.
4. A graph has 6 vertices, and every vertex has degree 2. How many edges does it have? Draw two different graphs like this.
Show answer
6 × 2 ÷ 2 = 6 edges. One example is a single ring, the cycle C6. Another is two separate triangles side by side. Both have every degree equal to 2, but they are different graphs: one is in one piece and the other is in two. So the degree sequence doesn't always pin down the graph.
5. A tournament has 45 matches, and every team plays every other team exactly once. How many teams are there?
Show answer
We need n(n − 1) ÷ 2 = 45, so n(n − 1) = 90 = 10 × 9. There are 10 teams.
6. In a directed graph with 5 vertices, the out-degrees are 2, 1, 3, 0 and 2. What is the sum of the in-degrees?
Show answer
The sum of in-degrees always equals the sum of out-degrees: 2 + 1 + 3 + 0 + 2 = 8.
Summary and glossary
- A graph is a set of vertices plus a set of edges joining pairs of them. Only the connections matter, not the drawing.
- The degree of a vertex is the number of edges touching it.
- Handshake lemma: the degrees add up to twice the number of edges. So the number of odd-degree vertices is always even.
- Directed graphs have one-way arcs, with in-degree and out-degree. Weighted graphs put numbers on edges.
- Learn the families by name: complete Kn (with n(n − 1)/2 edges), path, cycle, star, bipartite and empty graphs.
- Computers store graphs as edge lists, adjacency matrices or adjacency lists. Each one makes different questions fast.
- Graph
- A set of vertices with edges joining pairs of them.
- Vertex (node)
- One of the "things" in a graph, drawn as a dot.
- Edge (link)
- A connection between two vertices, drawn as a line.
- Adjacent
- Joined by an edge.
- Degree
- The number of edges at a vertex. A loop counts twice.
- Simple graph
- A graph with no loops and no repeated edges.
- Isomorphic
- Having exactly the same pattern of connections.
- Directed graph
- A graph whose edges (arcs) point one way.
- Weighted graph
- A graph with a number on each edge.
- Complete graph Kn
- n vertices, every pair joined.
- Bipartite
- Vertices split into two groups, with edges only between the groups.
- Subgraph
- Some of a graph's vertices and edges, kept together.
- Adjacency matrix
- A grid of 0s and 1s recording which pairs are adjacent.