Graph theory basics
Dots joined by lines, and the handful of ideas that describe them: degree, paths, cycles and connectivity. Then the seven bridges puzzle that started the subject.
In 30 seconds
A graph is a set of dots (vertices) with lines (edges) joining some pairs. It records only who is linked to whom, which is enough to model road maps, friendships or the web.
A vertex’s degree is its number of edges. A path is a route along edges; a cycle is a path that returns to its start; a graph is connected if every vertex can reach every other.
Euler’s rule: a walk that uses every edge exactly once exists if, and only if, the graph is connected and has zero or two vertices of odd degree.
Graph theory basics, step by step
Strictly, a route that may revisit vertices but not edges is called a trail; “Euler path” is the everyday name. With 0 odd vertices the walk ends where it began (an Euler circuit).
- Step 1: Vertices and edges. A graph is a set of things, called vertices, and a set of links between pairs of them, called edges. People and friendships, towns and roads, web pages and links are all graphs. Only who is joined to whom matters, not where the dots are drawn or how long the lines are.
- Step 2: Degree. The degree of a vertex is the number of edges that meet there. C and D have degree 3, the rest have degree 2. Add up all the degrees and you always get twice the number of edges, because every edge has two ends and is counted once at each.
- Step 3: Paths. A path is a route that moves from vertex to vertex along edges without visiting any vertex twice. From A to F the shortest path is A, C, D, F, which uses 3 edges. A, B, C, D, E, F is a longer path between the same two vertices.
- Step 4: Cycles. A cycle is a path that returns to where it started: A, B, C and back to A. The triangle D, E, F is another. A cycle means there are two different ways round between its vertices. The edge C to D lies on no cycle, which will matter in the next step.
- Step 5: Connectivity. A graph is connected when there is a path between every pair of vertices. Ours is, but only just: take away the edge C to D and it falls into two separate pieces, called components. An edge whose removal disconnects a graph is called a bridge. Edges on a cycle are never bridges.
- Step 6: The bridges of Königsberg. Königsberg had seven bridges joining two river banks and two islands. Could a walk cross each bridge exactly once? In 1736 Euler threw away the map and kept only what matters: each land mass becomes a vertex, each bridge an edge. That move is usually counted as the birth of graph theory.
- Step 7: Euler’s rule. Each time a walk passes through a vertex it uses two edge ends, one in and one out. So every vertex except the start and the end must have even degree. Königsberg has four odd vertices: no such walk. Our graph has two, C and D, and the walk C, A, B, C, D, E, F, D uses all seven edges once.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Learn next
- Adjacency list vs adjacency matrixTwo ways to store a graph, and what each costs in memory and lookups. Beginner
- Dijkstra’s shortest pathGrow the cheapest frontier until you reach the goal. Intermediate
- Directed acyclic graphs (DAGs)One-way arrows and no loops: the shape of anything that has dependencies. Beginner
Spotted a mistake on this page?