Graph Theory, Step by Step

Free course

Chapter 1, lesson 2 of 5

Subtitles

Arrows, Weights and Loops

The main kinds of graphs, and how to pick the right one.

Written lesson

6 min read

In the last lesson we proved that nobody can cross the seven bridges of Königsberg exactly once. Now ask a computer. A typical program stores the map as a set of pairs, "this piece of land connects to that one", and a set keeps only one copy of each pair. Two bridges between the same banks become one, so seven bridges become five.

The program then counts the degrees, finds only two odd vertices, and announces that the walk exists. The code has no bug. It used the wrong kind of graph. This lesson introduces the main kinds of graphs and how to pick the right one for a problem.

Repeated edges and loops

In lesson one, every edge joined two different vertices, and each pair was joined at most once. A graph like that is called a simple graph. Real data often breaks both rules: two flights between the same pair of airports, two bridges between the same two banks.

  • Edges that join the same pair of vertices are parallel edges. A graph that allows them is a multigraph.
  • An edge can even start and end at the same vertex. That edge is a loop, and a graph that allows loops (and parallel edges) is often called a pseudograph.

A loop touches its vertex at both ends, so a loop adds two to the degree. That keeps the handshake lemma true. The multigraph in the video has five edges (AB twice, BC, CD and AC), and its degrees 3, 3, 3 and 1 add up to 10. Add a loop at D and there are six edges, D's degree becomes 1 + 2 = 3, and the degrees add up to 12.

A loop touches its vertex twice, so it adds two to the degree.A loop touches its vertex twice, so it adds two to the degree.

Now look again at the program from the introduction. By merging parallel bridges, it changed the degrees: the banks went from 3 to 2, so four odd vertices became two. It answered a question about a different city.

Merging the parallel bridges turns four odd vertices into two: a different city.Merging the parallel bridges turns four odd vertices into two: a different city.

Directed graphs

Some connections only go one way: a one-way street, or following someone online, where you can follow them without them following you back. In a directed graph every edge is an arrow from one vertex to another. The arrows are also called arcs.

The degree now splits in two:

  • the out-degree counts the arrows leaving a vertex,
  • the in-degree counts the arrows arriving.

The example has six arrows: A→B, B→C, C→A, C→D, D→E and E→D.

VertexABCDE
Out-degree11211
In-degree11121

Out-degrees and in-degrees each add up to the number of arrows.Out-degrees and in-degrees each add up to the number of arrows.

The handshake idea still works, in a sharper form. Every arrow leaves exactly one vertex and arrives at exactly one vertex, so the out-degrees add up to the number of arrows, and so do the in-degrees: 6 and 6.

Direction also changes where you can go. From A you can reach every vertex. From D you can never get back to A, because the only arrows out of D and E lead to each other. In a directed graph, "can I get there?" and "can I get back?" are different questions, and lesson 9 is all about the second one.

Weighted graphs

Often an edge carries a number: a distance, a travel time, a price. A graph with a number on every edge is a weighted graph, and the number is the edge's weight.

Weights change even simple questions. What is the shortest route from A to D in this graph?

EdgeABBCCDADDE
Weight11172

If you count edges, the answer is the direct road AD: one edge. But its weight is 7. The long way round, A, B, C, D, uses three edges of weight 1, for a total of only 3.

Fewest edges and smallest total weight give different routes.Fewest edges and smallest total weight give different routes.

So "shortest" can mean the fewest edges or the smallest total weight. They are different questions and they need different algorithms: breadth-first search for the first (lesson 6), Dijkstra's algorithm for the second.

Two special graphs

Two more graphs are worth knowing by name.

A complete graph joins every pair of vertices. With five vertices that makes ten edges, and it is called K₅. In general, n vertices give n(n − 1)/2 edges: each of the n vertices is joined to the other n − 1, and that counts every edge twice, once from each end (the handshake lemma again). No simple graph on n vertices can have more edges than that.

A bipartite graph splits its vertices into two groups so that every edge goes from one group to the other, never inside a group. Students and the courses they take form a bipartite graph: an edge always joins a student to a course.

Students on one side, courses on the other: every edge crosses between the groups.Students on one side, courses on the other: every edge crosses between the groups.

There is a handy test. A graph is bipartite exactly when every cycle in it has an even length. Walking around a cycle, each step switches sides, so you can only get back to your start after an even number of steps. A triangle is a cycle of length three, so a graph that contains a triangle can never be bipartite.

Choosing the right graph

When you turn a real problem into a graph, ask four questions:

  1. Can two things be connected more than once? Then you need a multigraph.
  2. Does each connection have a direction? Then it is directed.
  3. Does each connection carry a number? Then it is weighted.
  4. Do the things split into two groups, with connections only between them? Then it is bipartite.

Some examples: a road map with one-way streets and distances is directed and weighted. A follower network is directed. A molecule with double bonds is a multigraph. Students and their courses form a bipartite graph.

Get these answers right and the right algorithms follow. Get one wrong and you get the Königsberg bug: correct code, wrong graph.

Key ideas

  • A simple graph has no parallel edges and no loops. Parallel edges make a multigraph, and a loop adds two to its vertex's degree.
  • A directed graph uses arrows; each vertex has an in-degree and an out-degree, and each kind adds up to the number of arrows.
  • A weighted graph puts a number on every edge, and "shortest" then usually means smallest total weight.
  • A complete graph on n vertices has n(n − 1)/2 edges. A graph is bipartite exactly when all its cycles are even.

Check yourself

1. A multigraph has two parallel edges between A and B, one edge BC and one loop at C. What are the degrees, and do they satisfy the handshake lemma?

2. How many edges does the complete graph K₆ have?

3. Is a cycle of five vertices bipartite? And a cycle of six?

Practice on the platform

Three short guides go deeper: directed vs undirected graphs, weighted vs unweighted graphs and simple graphs vs multigraphs. Then open the Dijkstra visualizer, build the graph from this lesson, give the direct road a weight of 7 and press play. Dijkstra skips the direct road and finds A, B, C, D with a total weight of 3.

Up next

Course content

0 of 20 watched

Chapter 1Graph Theory Foundations

Chapter 2Exploring a GraphChapter 1 quiz first

Chapter 3Shortest PathsChapter 2 quiz first

Chapter 4Connecting CheaplyChapter 3 quiz first

Chapter 5Hard ProblemsChapter 4 quiz first

Chapter 6Network FlowsChapter 5 quiz first