Find a course

Module 5: Number Theory and GraphsLesson 11 of 1214 min

Graphs, Degrees and Euler Paths

Model networks as graphs, use degrees and the handshake lemma, recognize complete and bipartite graphs, and decide when an Euler path or circuit exists.

Step 1 of 12Learn

Dots and lines

In the 1700s, the city of Königsberg had seven bridges connecting two riverbanks and two islands. Residents wondered whether they could walk through the city crossing every bridge exactly once. In 1736, Leonhard Euler proved it was impossible, and in doing so invented graph theory.

A graph G = (V, E) consists of a set V of vertices (also called nodes) and a set E of edges, each connecting two vertices. Euler's insight was to throw away everything except which land masses were connected: four vertices, seven edges.

Graphs model anything made of connections:

  • Social networks: people are vertices, friendships are edges.
  • Maps: intersections are vertices, roads are edges, often with weights for distance or travel time.
  • The internet: routers and the links between them.
  • Dependencies: software packages, with an edge when one needs another. These edges have a direction, making a directed graph.

Some vocabulary:

  • Two vertices joined by an edge are adjacent.
  • The degree of a vertex is the number of edges touching it.
  • A path is a sequence of vertices with each consecutive pair joined by an edge; a cycle is a path that returns to its start without repeating edges.
  • A graph is connected if there's a path between every pair of vertices.
  • A simple graph has no loops (edges from a vertex to itself) and no repeated edges between the same pair. Königsberg needed a multigraph, because some land masses were joined by two bridges.

Step 2 of 12Practice · Chart

Bridges touching each land mass in Königsberg

Tap the land mass with the highest degree.

Tap the bar that answers the question, then press Check.

Bridges touching each land mass in KönigsbergBar chart with 4 categories: Kneiphof island: 5; North bank: 3; South bank: 3; Lomse island: 3.012345Bridges (degree)Kneiphof islandNorth bankSouth bankLomse island5333
7 bridges in total; the degrees add up to 14 = 2 × 7.
Bridges touching each land mass in Königsberg
ItemDegree
Kneiphof island5
North bank3
South bank3
Lomse island3

Step 3 of 12Learn

The handshake lemma and special graphs

At a party, everyone shakes some hands. Add up the number of handshakes each person made. Each handshake gets counted twice, once by each person. That's the handshake lemma:

The sum of all vertex degrees equals 2 × (number of edges).

Two consequences follow immediately:

  • The degree sum is always even.
  • The number of vertices with odd degree is always even. You can't have a party where exactly three people shook an odd number of hands.

Special graphs.

  • The complete graph Kₙ has n vertices with every pair joined. Each vertex has degree n − 1, so by the handshake lemma it has n(n − 1)/2 edges. K₅ has 10 edges, and a round-robin tournament among 10 teams needs 45 games.
  • A cycle graph Cₙ is n vertices in a ring; every vertex has degree 2.
  • A graph is bipartite if its vertices split into two groups with every edge going between the groups. Job applicants and jobs, students and courses, or users and movies form bipartite graphs. A graph is bipartite exactly when it has no cycle of odd length.

Representing graphs in code. An adjacency list stores, for each vertex, the list of its neighbors; it's compact for sparse graphs like road maps. An adjacency matrix is an n × n table with a 1 where an edge exists; it answers "are u and v adjacent?" instantly but takes n² space. A social network with a billion users uses adjacency lists: the matrix would have 10¹⁸ entries, almost all zero.

Step 4 of 12Explore

Edges in a complete graph K_n

Every vertex has degree n − 1, so by the handshake lemma |E| = n(n − 1)/2.

0100200300400 102030
Edges in K_n
66
Sum of degrees
132

Step 5 of 12Practice · Work it out

A simple graph has 8 vertices, each of degree 3. How many edges does it have?

Step 6 of 12Practice · True or false

There is a simple graph with 5 vertices whose degrees are 3, 3, 3, 3 and 3.

Step 7 of 12Learn

Euler paths and circuits

An Euler path uses every edge exactly once. An Euler circuit is an Euler path that starts and ends at the same vertex. Euler found a simple test.

Think about passing through a vertex in the middle of a walk: you arrive on one edge and leave on another, using up edges in pairs. So every vertex you pass through needs even degree. Only the start and end of a path can have odd degree, because there you leave without arriving, or arrive without leaving.

Euler's theorem. For a connected graph:

  • An Euler circuit exists exactly when every vertex has even degree.
  • An Euler path (not a circuit) exists exactly when exactly two vertices have odd degree; the path must start at one and end at the other.
  • With more than two odd-degree vertices, neither exists.

Königsberg: the four land masses had degrees 5, 3, 3 and 3. Four odd vertices, so no Euler path. The walk was impossible, and no amount of trying could change that.

The test is applied every day: snowplow and street-sweeper routes, mail delivery, and the drawing puzzles where you trace a figure without lifting your pencil. If a route must cover every street and the street graph has odd vertices, some streets must be traveled twice, and choosing which ones as cheaply as possible is called the Chinese postman problem.

Contrast this with a Hamilton path, which visits every vertex exactly once. Despite the similar wording, no simple test is known for Hamilton paths; deciding whether one exists is among the famous hard (NP-complete) problems.

Step 8 of 12Practice · Sort into groups

Based on the vertex degrees of a connected graph, what does it have?

Tap an item, then tap its group (or drag it there).

Step 9 of 12Practice · Diagram

Euler's test for a connected graph

Königsberg had four odd-degree land masses. Which branch is it?

Tap the shape that answers the question, then press Check.

Euler's test for a connected graphHierarchy diagram: Count odd-degree vertices, 0 odd → Euler circuit, 2 odd → Euler path, 4 or more → neither.Count odd-degreevertices0 odd → Euler circuit2 odd → Euler path4 or more → neither
Count odd-degree vertices
0 odd → Euler circuit

Start anywhere; you will end where you started.

2 odd → Euler path

Start at one odd vertex and finish at the other.

4 or more → neither

Every pass through a vertex uses two edges, so odd vertices can only be endpoints.

Step 10 of 12Practice · Select all that apply

Which graphs are bipartite? Select all that apply.

Select all that apply.

Step 11 of 12Note

Step 12 of 12Practice · Choose one

How many edges does the complete graph K₁₂ have?