Unit 1: Foundations

Dots, lines and the language of graphs

Friendships, road maps, the internet and molecules all have the same hidden shape: some things, and connections between them. This unit gives you the words and the first few facts for talking about that shape.

1234567891011
In this unit
  1. What is a graph?
  2. Only the connections matter
  3. The basic vocabulary
  4. Degree: counting connections
  5. The handshake lemma
  6. Directed graphs
  7. Weighted graphs
  8. Graphs worth knowing by name
  9. Subgraphs
  10. Writing a graph down
  11. Practice set
  12. Summary and glossary

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.

A friendship networkSix people: Ana, Ben, Cara, Dev, Eli and Fay. Lines join Ana and Ben, Ana and Cara, Ben and Cara, Ben and Dev, Cara and Eli, Dev and Eli, and Eli and Fay.Ana2Ben3Cara3Dev2Eli3Fay1
Hover over or tap a person. Their connections light up, and the small number shows how many friends they have.

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?

SituationVertices are…Edges are…
Social networkpeoplefriendships
Road maptowns and junctionsroads
Metro mapstationstrack between neighbouring stations
The webweb pageslinks from one page to another
Chemistryatomschemical bonds
Electricity gridpower stations, substations, homescables
A university coursemodules"you must pass this one before that one"
Sudokuthe 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 4-cycleFour vertices in a square, each joined to the next.1234

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: a pentagonFive vertices A to E in a ring: A-B, B-C, C-D, D-E, E-A.ABCDE

Drawing 1

Drawing 2: a starThe same five edges A-B, B-C, C-D, D-E, E-A, drawn as a five-pointed star.ABCDE

Drawing 2

One graph, two drawings. Follow A → B → C → D → E → A in each picture and you trace the same loop.

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 simple graphFour vertices, no loops, at most one edge between any two vertices.XYZW

Simple graph

A multigraphThe same four vertices, plus a loop at Y and two separate edges between X and Z.XYZWlooptwo edges, same pair

Multigraph

Left: a simple graph. Right: the same graph with a loop at Y and a second edge between X and Z added.

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:

PersonNeighboursDegree
AnaBen, Cara2
BenAna, Cara, Dev3
CaraAna, Ben, Eli3
DevBen, Eli2
EliCara, Dev, Fay3
FayEli1

A few related words:

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":

Every edge has two endsThe friendship network with a pink dot at each end of every line. There are 7 lines and 14 dots.AnaBenCaraDevEliFay
7 edges, 14 ends. Count the pink dots around each person and you get their degree. Count all the dots and you get twice the number of edges.
sum of all degrees = 2 × (number of edges) The handshake lemma. In symbols: Σ deg(v) = 2|E|

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.

  1. A graph has degree sequence (4, 3, 3, 2, 2). How many edges does it have?
  2. Can a graph have 5 vertices, each of degree 3?
  3. Can a simple graph have degree sequence (3, 3, 3, 1)? (Careful.)
Show answers
  1. The sum is 4 + 3 + 3 + 2 + 2 = 14, so there are 14 ÷ 2 = 7 edges.
  2. No. The sum would be 5 × 3 = 15, which is odd.
  3. 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.

Who follows whomArrows: Maya follows Leo, Leo follows Maya, Zara follows Maya, Leo follows Zara, Omar follows Leo, Omar follows Zara.MayaLeoZaraOmar
An arrow from X to Y means "X follows Y". Maya and Leo follow each other, so there are two arrows between them, one each way.

Now each vertex has two kinds of degree:

AccountFollowed byIn-degreeFollowsOut-degree
MayaLeo, Zara2Leo1
LeoMaya, Omar2Maya, Zara2
ZaraLeo, Omar2Maya1
Omarnobody0Leo, Zara2

The handshake idea still works, with a twist. Every arrow has exactly one tail (where it leaves) and one head (where it arrives). So:

sum of in-degrees = sum of out-degrees = number of arcs Here: 2 + 2 + 2 + 0 = 1 + 2 + 1 + 2 = 6 arcs

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.

Walking times in minutesHome to School 12, Home to Park 5, Park to School 6, Park to Shop 4, Shop to Library 7, School to Library 3.1256473HomeSchoolParkShopLibrary
Walking times between places in a small town, in minutes. The drawing isn't to scale; only the numbers matter.

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:

RouteTime
Home → School → Library12 + 3 = 15 minutes
Home → Park → Shop → Library5 + 4 + 7 = 16 minutes
Home → Park → School → Library5 + 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.

Complete graphs grow fast. Each new vertex must connect to every vertex already there.

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:

edges in Kn = n(n − 1) ÷ 2 Check: K5 has 5 × 4 ÷ 2 = 10 edges

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

Try it.

  1. A chess club of 8 players holds a round robin. How many games are played?
  2. How many edges does K3,4 have?
  3. Is the cycle C6 bipartite? Is C5?
Show answers
  1. 8 × 7 ÷ 2 = 28 games.
  2. 3 × 4 = 12 edges.
  3. 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 subgraphThe friendship network with only the triangle Ana, Ben, Cara highlighted; everything else is faded.AnaBenCaraDevEliFay
A subgraph of the friendship network: the triangle formed by Ana, Ben and Cara, who all know each other.

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:

A small graphVertices A, B, C, D. Edges A-B, A-C, B-C, C-D.ABCD

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

ABCDsum
A01102
B10102
C11013
D00101

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:

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 matrixAdjacency list
"Are X and Y adjacent?"Instant: look up one cellScan X's list
"List X's neighbours"Scan a whole rowInstant: read X's list
Storage neededAlways n × n cellsGrows with the number of edges
Best forSmall or dense graphsLarge 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.

  1. Write the adjacency list of the friendship graph.
  2. 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
  1. Ana: Ben, Cara. Ben: Ana, Cara, Dev. Cara: Ana, Ben, Eli. Dev: Ben, Eli. Eli: Cara, Dev, Fay. Fay: Eli.
  2. 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

  1. A graph is a set of vertices plus a set of edges joining pairs of them. Only the connections matter, not the drawing.
  2. The degree of a vertex is the number of edges touching it.
  3. Handshake lemma: the degrees add up to twice the number of edges. So the number of odd-degree vertices is always even.
  4. Directed graphs have one-way arcs, with in-degree and out-degree. Weighted graphs put numbers on edges.
  5. Learn the families by name: complete Kn (with n(n − 1)/2 edges), path, cycle, star, bipartite and empty graphs.
  6. 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.
Powered by Webloyt | Report abuse