Introduction to Graphs
Historical Motivation
Seven Bridges of Königsberg Königsberg (now Kaliningrad, Russia) around 1735 Problem: Find a walk through the city that would cross each bridge once and only once. 3 / 22
Definitions
Graph Graph A graph G = (V, E) is a set V of vertices connected by an edge set E. 5 / 22
Variations Multi-Graph: Multiple edges between two vertices. Directed: Edges have a direction. Weighted: Vertices and/or edges have weights. Simple: No multiple edges, no loops. Simple Undirected Graph A simple undirected graph G = (V, E) is a set V of vertices connected by an edge set E {{u, v} u, v V, u v}. An edge {u, v} is also written as uv. 6 / 22
Adjacency Adjacency Two vertices u and v are adjacent if there is an edge connecting them. This is sometimes written as u v. v a b c v is adjacent with b and c but not with a. 7 / 22
Neighbourhood Neighbourhood The open neighbourhood N(v) = {u V u v, u v} of a vertex v is the set of vertices adjacent to v (not including v). The closed neighbourhood N[v] = N(v) {v} includes v. v a b c N(v) = {b, c} N[v] = {v, b, c} 8 / 22
Degree Degree The degree deg(v) of a vertex v is the number of incident edges. Note that the degree is not necessarily equal to the cardinality of neighbours. v a b c deg(v) = 3 deg(a) = 1 deg(b) = 5 deg(c) = 1 9 / 22
Degree (directed) Indegree, Outdegree For a vertex v, the indegree deg (v) is the number of incoming edges, and the outdegree deg + (v) the number of incoming edges. v a b c deg + (v) = 2 deg + (a) = 0 deg + (b) = 3 deg + (c) = 1 deg (v) = 1 deg (a) = 1 deg (b) = 2 deg (c) = 0 10 / 22
Degree Let G = (V, E) be a graph. Then Lemma deg(v) = 2 E v V If G is directed Lemma deg + (v) = deg (v) = E v V v V It follows that the number of odd degree vertices is even. 11 / 22
Path Path In a graph a path p of length l is sequence of l +1 adjacent vertices, i. e. p = (v 0,..., v l ) with v i v i+1 E. A path is simple if all vertices are different, i. e. v i v j i j. v v a c a c b b u u path (b, a, v, c, u) simple path (a, v, b, u) 12 / 22
Cycle Cycle A cycle is a path that begins and ends with same vertex. A cycle is simple, if it doesn t cross itself. v v a c a c b b u u cycle (a, v, b, c, u, b, a) simple cycle (a, v, c, u, b, a) 13 / 22
Eulerian Cycles
Recall: Seven Bridges of Königsberg Königsberg (now Kaliningrad, Russia) around 1735 Problem: Find a walk through the city that would cross each bridge once and only once. 15 / 22
Euler s Solution Represent problem as graph: Original problem: Find a walk through the city that would cross each bridge once and only once. Graph problem: Does G contain a path using each edge exactly once? 16 / 22
Euler s Solution Given a graph G = (V, E) with E = m and index arithmetic modulo m. Eulerian Cycle An Eulerian cycle in a graph G is a sequence of vertices and edges v 1, e 1,..., v m, e m, v 1 such that e i = v i v i+1 and e i e j i j. Eulerian Path An Eulerian path in a graph G is a sequence of vertices and edges v 1, e 1,..., v m, e m, v m+1 such that e i = v i v i+1 and e i e j i j. In an Eulerian cycle the start and end vertex hast to be equal. In an Eulerian path they can be different. 17 / 22
Euler s Solution Theorem A connected graph has an Eulerian cycle if and only if the degree of all vertices is even, i. e. v V k N: deg(v) = 2k. Theorem A graph has an Eulerian path if and only if it is connected and has at most two vertices with an odd degree. 18 / 22
Seven bridges of Königsberg Degree of all vertices is odd. 3 5 3 3 Thus, there is no walk through the city that would cross each bridge once and only once. 19 / 22
Algorithm
Algorithm Given a graph G. Does G have an Eulerian path? Algorithm based on Hierholzer s algorithm from 1837. a b c d e f 21 / 22
Algorithm 1. Determine the degree of all vertices. 3 4 5 3 3 4 21 / 22
Algorithm 2. If There are more than two vertices with an odd degree Then Stop, G has no Eulerian path. 3 4 5 3 3 4 21 / 22
Algorithm 3. Choose any starting vertex v. Select a vertex with odd degree if possible. 3 4 4 2 3 4 21 / 22
Algorithm 4. Follow a trail T of edges from v until getting stuck. a b c d e f T = a, e, f, d, b, c, e 21 / 22
Algorithm 5. While There is a vertex v belonging to the current trail T but has adjacent edges not part of T. a b c d e f T = a, e, f, d, b, c, e 21 / 22
Algorithm 5.1 Start another trail T from v, following unused edges until returning to v. a b c d e f T = a, e, f, d, b, c, e T = b, f, c, a, b 21 / 22
Algorithm 5.1 Start another trail T from v, following unused edges until returning to v. a b c d e f T = a, e, f, d, b, c, e T = b, f, c, a, b 21 / 22
Algorithm 5.2 Join the tour T formed in this way to the previous trail T. a b c d e f T = a, e, f, d, b, f, c, a, b, c, e 21 / 22
Complete Algorithm Given a graph G = (V, E). 1. Determine the degree of all vertices. 2. If There are more than two vertices with an odd degree Then Stop, G has no Eulerian path. 3. Choose any starting vertex v. Select a vertex with odd degree if possible. 4. Follow a trail T of edges from v until getting stuck. 5. While There is a vertex v belonging to the current trail T but has adjacent edges not part of t. 5.1 Start another trail T from v, following unused edges until returning to v. 5.2 Join the tour T formed in this way to the previous trail T. This algorithm can be implemented in O( E ) time. 22 / 22