Chapter 2. Walks

(Chapters 1.7, 2.1–2.6)

Prof. Tesler

Math 154 Winter 2020

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 1 / 64 Chapter 1.7: Subgraphs

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 2 / 64 Subgraphs 1

5 2 5 2

4 3 4 3 G G’ Let G = (V, E) be a graph. A subgraph of G is G0 = (V 0, E 0) where V 0 ⊆ V, E 0 ⊆ E, and the edges of E 0 only involve vertices of V 0. If we remove a vertex v from G, we also must remove all edges incident with it. We may also remove edges, even if their vertices remain. For multigraphs and directed graphs, it’s similar.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 3 / 64 Set difference

We’ll look at several constructions of subgraphs, based on specifying either which vertices/edges to remove or which to keep.

Let A and B be sets. Their set difference is

A \ B = A ∩ Bc = { x ∈ A : x < B }

{1, 2, 3, 4, 5, 6, 7} \ {3, 5, 7, 9, 11} = {1, 2, 4, 6}

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 4 / 64 Subtracting vertices from a graph 1

5 2 5 2

4 3 3 G G − {1,4} Let X be a subset of the vertices of G. G − X is the subgraph of G obtained by removing those vertices and all edges incident with them: Vertices: V(G) \ X Edges: { e ∈ E(G): e ∩ X = ∅ } = all edges of G not incident to any vertices in X

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 5 / 64 Induced subgraph (only keep certain vertices) 1

5 2 5 2

4 3 4 3 G G[{2,3,4,5}]

Let X be a subset of the vertices of G. The induced subgraph G[X] is G − (V(G) \ X): the vertices are X, and keep the edges that only involve X.

Vertices: X Edges: { e ∈ E(G): e ⊂ X }

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 6 / 64 Subtracting edges from a graph 1 1

5 2 5 2

4 3 4 3 G G − { {1,2}, {4,5} }

Let L be a subset of the edges of G. G − L is the subgraph of G with the same vertices as G, but edges L removed. Vertices: V(G) Edges: E(G) \ L On the right, vertex 1 is an isolated vertex: a vertex of 0.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 7 / 64 Subgraph spanned by edges (only keep those edges) 1

5 2 5 2

4 3 3 G L = { {2,5}, {3,5} }

Let L be a subset of the edges of G. The subgraph of G spanned by L has edge set L, and all vertices incident with edges in L: [ Vertices: e Edges: L e∈L

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 8 / 64 Spanning subgraph

1 1

5 2 5 2

4 3 4 3 G G’

A similar but different term: a spanning subgraph of G is any subgraph that has the same vertices as G.

Vertices: V(G) Edges: Any subset of E(G)

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 9 / 64 Neighborhood of a set of vertices

(figure from Verstraete textbook) Let X be a subset of the vertices of G. The neighborhood of X is all vertices of G that are outside X and adjacent to at least one vertex in X:

N(X) = { y ∈ V(G) \ X : {x, y} ∈ E(G) for some x ∈ X }

N({1, 2, 3}) = {5, 6, 7, 9, 10}

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 10 / 64 Contraction of a graph

GG/ {1, 2, 3, 5, 7} (figure from Verstraete textbook) Let X be a subset of the vertices of G. The contraction G/X is a new graph obtained as follows: Form G − X by removing X and incident edges. Add a new vertex x. Add new edges connecting x to each vertex of N(X). This is also called contracting X to a vertex x. This is not a subgraph of G, but it makes use of subgraphs. Also note the different notations A \ B vs. G/X. Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 11 / 64 Chapter 2: Walks

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 12 / 64 Walks — Example: Transit map

1

ab c Route c from station 1 to station 2 d 6 e 5 2 Route f from station 2 to station 5 f g h Route g from station 5 to station 4

4i 3 Route i from station 4 to station 3

A transit map is shown above, with a route from station #1 to #3. This is called a walk in the graph. It starts at 1 and ends at 3, so it’s also called a 1,3-walk. Represent it by listing the sequence of vertices and/or edges:

vertices & edges: 1, c, 2, f , 5, g, 4, i, 3 (not usually listed this way) vertices: 1, 2, 5, 4, 3 (5 vertices) edges: c, f , g, i (4 edges)

The length of a walk is the number of edges, 4.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 13 / 64 Walks 1

ab c d 6 e 5 2 f g h

4i 3 Trace along edges from vertex x to y, without lifting your pen. A walk from vertex x to y is a sequence of edges, each connected to the next by a vertex:

e1 = {x, v1} e2 = {v1, v2} e3 = {v2, v3} ··· ek = {vk−1, y} In a directed graph, edge directions must be respected:

e1 = (x, v1) e2 = (v1, v2) e3 = (v2, v3) ··· ek = (vk−1, y) The length of the walk is the number of edges, k (not the number of vertices, k + 1).

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 14 / 64 Walks Note: Some authors use these terms differently 1 1 1 1

ab c ab c ab c ab c d 6 e d 6 e d 6 e d 6 e 5f 2 5 2 5 2 5 2 f f f g h g h g h g h i 4i 3 4 3 4i 3 4i 3 Walk Trail Path Tour In a walk, edges and vertices may be re-used. A trail is a walk with all edges distinct. A path is a walk with all vertices and edges distinct. A walk/trail/path is open if the start and end vertices are different, and closed if they’re the same (this is allowed in a closed path, but no other vertices may be repeated). A tour is a closed trail (all edges distinct). A is a closed path (all vertices and edges distinct, apart from start = end).

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 15 / 64 Example: Paths in a computer network

PC1

PC2 ModemISP Remote server

PC3

Printer Hard drive

For PC1 to print a page from a website, first it retrieves the page Remote server → · · · → ISP → Modem → PC1 and then it sends it to the printer PC1 → Modem → Printer PC3 can directly print on the printer w/o going through the Modem. For PC1 to read a file from the hard drive, it goes through a path Hard drive → PC3 → Modem → PC1

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 16 / 64 Example: Graph of friends

Irene Gina Harry Dan Emily Frank Cindy

Amy Bob The length of a walk/trail/path is the number of edges in it. The distance between vertices is the length of the shortest path. Amy’s friends: Bob, Cindy, Dan, Emily Each is distance 1 from Amy. Amy and Frank’s mutual friends: Bob, Cindy They are the middle vertex on a path of length 2 from Amy to Frank. Amy’s friends of friends: Frank, Gina, Irene Each is distance 2 from Amy. “Six degrees of separation”: A popular concept is that everyone is 6 6 steps away from everyone in the world. (It’s probably false.) Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 17 / 64 Bipartite graph

This graph is bipartite. The vertices are split into two parts, A (green) and B (black), and edges are only allowed when the two vertices are in different parts.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 18 / 64 Cycle in a bipartite graph

A cycle in a bipartite graph must have even length:

A cycle has consecutive vertices v0, v1,..., vn with v0 = vn. For a cycle in a bipartite graph, the vertices alternate coming from A, B, A, B,... or B, A, B, A,... .

Since v0 = vn, they’re both in A or both in B, so n is even.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 19 / 64 Connected graph

1 component 2 components 2 components

An undirected graph is connected if for all vertices u, v, there is a path from u to v. Otherwise, it’s disconnected.

The graph on the left is connected. The others are disconnected.

A graph may be split into connected components. Partition the graph into subgraphs. Vertices u, v are in the same connected component iff there is a path from u to v. Each isolated vertex is in its own component.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 20 / 64 Hamiltonian paths / cycles

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 21 / 64 Walks using all vertices or all edges 1 1 1

5 2 52 5 2

4 3 4 3 4 3 Hamiltonian path Hamiltonian cycle Eulerian tour 1,2,3,4,5 1,2,3,4,5,1 1,2,3,4,5,1,3,5,2,4,1 A Hamiltonian path is a path that uses every vertex exactly once. An Eulerian trail is a trail that uses every edge exactly once. Note: Euler is pronounced “oiler.” Closed versions: Hamiltonian cycle and Eulerian tour. A Hamiltonian cycle starts and ends at the same vertex, but all other vertices are used just once. These are used in many in Computer Science. While the descriptions are similar, there are good solutions for the Eulerian problem but not the Hamiltonian problem. Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 22 / 64 Dinner seating arrangements

Sit 8 people at a circular table so that no one knows their neighbors. X indicates people who do not know each other and thus may be seated next to each other.

12345678 1 X X X X 2 X X 3 X X X 4 X X X 5 X X X X 6 X X X 7 X X X 8 X X X X

Form a graph on vertices {1,..., 8}, with an edge {i, j} if i and j may be seated next to each other. The table is essentially the adjacency matrix: X = 1, blank = 0.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 23 / 64 Dinner seating arrangements

12345678 1 2 1 X X X X 2 X X 8 3 3 X X X 4 X X X 5 X X X X 6 X X X 7 4 7 X X X 8 X X X X 6 5 The solutions correspond to Hamiltonian cycles! Start with 1 and successively follow edges to vertices not yet used, until we return to 1 at the end. E.g., try 1, 8, 6, 3, 7, 4; but then we’re stuck because we can’t yet go back to 1!

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 24 / 64 Dinner seating arrangements 1 2 1 5

8 3 4 2

7 4 7 8

6 5 3 6 One Hamiltonian cycle is 1, 5, 2, 8, 6, 3, 7, 4, 1.

If someone gives you a solution, it is easy to verify if it’s correct. But testing all n! possibilities is impractical unless n is small.

There is no known efficient method, guaranteed to work in all graphs, to either find a Hamiltonian cycle or prove there isn’t one. In Computer Science, it is classified as an NP-complete problem (covered in CSE 101).

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 25 / 64 Dinner seating arrangements

A group of n people will have a series of dinners with everyone at the same circular table. How many dinners can be arranged so that no one repeats sitting next to the same person?

After d such dinners, each person will have sat next to 2d people out of the n − 1 people besides themself, so 2d 6 n − 1.  n−1  An upper bound on the number of dinners possible is 2 .

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 26 / 64 Dinner seating arrangements 1 K7 7 2

6 3

5 4

If n is an odd prime, (n − 1)/2 is achievable!

For each x = 1, 2,..., (n − 1)/2, form a Hamiltonian cycle in Kn: 1, 1 + x, 1 + 2x,..., 1 + (n − 1)x, 1 + nx ≡ 1 (mod n)

There are no repeated numbers besides 1 at the start and end: If 1 + ax ≡ 1 + bx mod n then (a − b)x ≡ 0 mod n. Since n is prime, the only solution in this range is a = b.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 27 / 64 Conditions for a Hamiltonian cycle

Currently, there is no efficient way to determine if a graph has a Hamiltonian path/cycle that’s guaranteed to work in all cases.

Since it’s an NP-complete problem, it’s suspected that there will never be such a solution.

But there are partial results covering some cases.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 28 / 64 Dirac’s Theorem on Hamiltonian cycles

Theorem Let G be a simple graph on n > 3 vertices with δ(G) > n/2. Then G is Hamiltonian (has a Hamiltonian cycle).

Note: δ(G) > n/2 means the minimum degree is > n/2; thus, all vertices have degree > n/2.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 29 / 64 Dirac’s Theorem on Hamiltonian cycles

Theorem Let G be a simple graph on n > 3 vertices with δ(G) > n/2. Then G is Hamiltonian (has a Hamiltonian cycle).

We will use proof by contradiction to prove this: Assume there is a non-Hamiltonian simple graph with δ(G) > n/2. Among such graphs, take the one with the highest number of edges possible.

The Kn is Hamiltonian, so G , Kn. Thus, at least one pair of vertices is not adjacent. Call them v1 and vn. 0 Add a new edge {v1, vn} to G, obtaining G . G0 has more edges than the largest counterexample, so it must have a Hamiltonian cycle, C = v1v2 ··· vnv1. (continues . . . )

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 30 / 64 Dirac’s Theorem on Hamiltonian cycles 5 7 5 7

1 6 1 6

4 8 4 8

2 3 2 3 GG0 We will use the above example for G. However, since proof by contradiction assumes something that isn’t actually true, there isn’t an actual example meeting the condition we assumed. Add the red edge; number its vertices 1 & 8; and call the graph G0. By our assumption, G doesn’t have a Hamiltonian cycle, but G0 does. So there must be a Hamiltonian cycle in G0 using {1, 8}. Consecutively number the vertices of the cycle 1,..., 8.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 31 / 64 Dirac’s Theorem on Hamiltonian cycles

5 7

1 6

4 8

2 3

Define A = { vi : vi−1 ∈ N(vn) for some i = 2,..., n } 0 B = N(v1) (neighborhoods are in G, not G ) Here: N(8) = {2, 3, 6, 7} A = {3, 4, 7, 8} B = N(1) = {2, 4, 5, 7} Compute A ∩ B = {4, 7}. Pick any element of A ∩ B. We’ll use 4: Since 4 ∈ A, we have a path 8 − 3 − 4 in G (and G0). Since 4 ∈ B, we have a path 1 − 4 in G (and G0). The red edge 8 − 1 is in G0 only (not in G). Combined, we have a cycle 8, 3, 4, 1, 8 in G0.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 32 / 64 Dirac’s Theorem on Hamiltonian cycles 5 7 5 7

1 6 1 6

4 8 4 8

2 3 2 3 GG0 Left: Hamiltonian cycle C in G0 in yellow 4-cycle 8,3,4,1,8 in green. They overlap in edges 34 and 18. Right: In cycle C, remove edges 34 and 18, and add in 41 and 83. The result is a Hamiltonian cycle 1,2,3, 8,7,6,5,4, 1 in G, contradicting the assumption that there isn’t one. We did this with an example; we will now go through it in general.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 33 / 64 Dirac’s Theorem on Hamiltonian cycles

Theorem Let G be a simple graph on n > 3 vertices with δ(G) > n/2. Then G is Hamiltonian (has a Hamiltonian cycle).

Proof, continued: 0 We have graph G with Hamiltonian cycle C = v1v2 ... vnv1.

Define A = { vi : vi−1 ∈ N(vn) for some i = 2,..., n } 0 B = N(v1) (neighborhoods are in G, not G )

We’ll show that A ∩ B , ∅ (next slide). So pick vi ∈ A ∩ B.

Then v1v2 ··· vi−1vnvn−1 ··· viv1 is a Hamiltonian cycle in G! 0 Edge {v1, vn} that’s in G but not in G has been removed. The edges we added are {vi−1, vn} and {vi, v1}. Both of these are in G due to how we chose vi. Contradiction! Thus, G has a Hamiltonian cycle. (continues . . . )

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 34 / 64 Dirac’s Theorem on Hamiltonian cycles

Proof of A ∩ B , ∅: Define A = { vi : vi−1 ∈ N(vn) for some i = 2,..., n } B = N(v1) We will show A ∩ B , ∅.

First, A ∪ B ⊂ V(G) \ {v1}: v1 < A (only v2,..., vn can be in A); v1 < B since v1 can’t be a neighbor of itself in a simple graph.

Thus, |A ∪ B| 6 n − 1. n n n Also, |A| + |B| = d(vn) + d(v1) > 2 + 2 = n, since δ(G) > 2 . Thus, |A ∩ B| = |A| + |B| − |A ∪ B| > n − (n − 1) > 1. So A ∩ B , ∅.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 35 / 64 What if δ(G) < n/2?

K4,2

 n−1  Let k = 2 (bxc is the floor of x: round it down to an integer).

The complete bipartite graph Kk,n−k has δ(G) = k < n/2.

It isn’t Hamiltonian: any cycle has to alternate between the two parts, so the maximum length of a cycle is 2k.

So Dirac’s Theorem cannot be extended to δ(G) < n/2, because some graphs with δ(G) < n/2 are Hamiltonian and others aren’t.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 36 / 64 Guarantee on having a cycle of a certain length

Theorem Let k > 2 and δ(G) > k. Then G has a cycle of length at least k + 1. Proof:

Let P = v1 ... vr be a longest path in G.

N(vr) ⊂ V(P) because if vr has any more neighbors, the path could have been extended an additional step.

d(vr) > k since the minimum degree is at least k.

r > k + 1 since the > k neighbors of vr are all in v1,..., vr−1.

v1,..., vr−k has at least one neighbor of vr, since we removed vr (not a neighbor of vr) and vr−k+1,..., vr−1 (k − 1 elements).

Then vivi+1 ··· vrvi has length r − i + 1 > r − (r − k) + 1 > k + 1.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 37 / 64 Eulerian trails / tours

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 38 / 64 Eulerian Trails and the Seven Bridges of Königsberg

http://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg

In the 1700s, a river in Köningsberg, Prussia, split the city into four land masses (including two islands), connected by seven bridges.

Can you walk through town and cross every bridge exactly once? No backtracking, no partial bridge-crossings, etc.

In 1735, Leonhard Euler proved it is impossible, and created the foundations of .

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 39 / 64 Seven Bridges of Königsberg

http://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg Replace each land mass by a vertex and each bridge by an edge.

For every vertex (except the first and last, if different) each time we enter on one edge, we exit on another. If we use up all the edges in this fashion, the degree is even.

If the starting and ending vertices are different, they have odd degrees since the first/last edge do not have an in/out pair.

Here, all vertices have odd degree, so such a walk is impossible.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 40 / 64 Necessary conditions for Eulerian trails and tours

An Eulerian trail from vertex x to y (x , y) uses every edge once.

If x = y: An Eulerian tour is a tour that uses every edge once.

All the edges need to be in the same connected component.

Each time we enter a vertex on one edge, we exit on a separate edge. So all vertices in this connected component must have even degree, except if x , y then x, y have odd degrees. x y

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 41 / 64 Hierholzer’s to construct an Eulerian tour Theorem (Eulerian tours) A connected graph has an Eulerian tour if and only if all vertices have even degrees.

Proof, Step 1: Construct a tour starting at any vertex x. We showed necessity; now we’ll show sufficiency.

Start at any vertex, v0 = x. Pick any edge on x, say e1, and follow it to the next vertex, v1. Pick any unused edge on v1, say e2. Follow it to the next vertex, v2. x Continue alternately picking vertices & edges v0, e1, v1, e2, v2,..., ek, vk, until forced to stop at a vertex vk with no un- used edge to follow.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 42 / 64 Eulerian tours, Proof, Step 1, continued Construct a tour starting at any vertex x x

So far we selected v0 = x, e1, v1, e2, v2,..., ek, vk. We used an even number of edges at every vertex on this trail (one edge in, another out), except the first and last vertices (x, vk).

If vk , x, then we used an odd number of edges on vk. But vk has even degree, so there’s an unused edge on it, and we did not have to stop!

Thus, vk = x, and that vertex uses an even number of edges too: first (e1), last (ek), and possibly in/out pairs in-between.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 43 / 64 Eulerian tours, Proof, Step 1, continued Construct a tour starting at any vertex x

x

We constructed a tour based at x, but it may not use all edges of the graph.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 44 / 64 Eulerian tours, Proof, Step 2 C x x

D

w w

We have a tour C, but an Eulerian tour must use all edges. What if some edges are unused? Since the graph is connected, there is an unused edge that touches some vertex w on C. Form a new tour D based at w, using the same algorithm as in Step 1. No edges in Step 1 may be re-used. Splice D into C at w to make C larger. Repeat until all edges are in C. Now it’s an Eulerian tour!

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 45 / 64 Eulerian trails

Theorem A connected graph G has an Eulerian trail from x to y (x , y) if and only if x, y have odd degrees while all other vertices have even degree.

Proof. Form graph G0 by adding an edge {x, y} to G. Now all vertices have even degree, so there is an Eulerian tour. Remove the new edge {x, y} to form an Eulerian trail from x to y. 

Generalization For both Eulerian tours and trails, we may replace “connected graph” by “a graph whose vertices with nonzero degree form a single connected component”. We may add isolated vertices since they have no edges, so no walks go through them.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 46 / 64 Strongly connected components in a directed graph 1 2 3 1 2 3

4 5 6 4 5 6 Not strongly connected Strongly connected

A directed graph is strongly connected iff for every pair of vertices x , y, there is a directed path from x to y and also one from y to x. A vertex is balanced if its indegree and outdegree are equal. A graph is balanced if all vertices are balanced. Left graph: None of the vertices are balanced. Right graph: 1, 3, 4, 6 are balanced, and 2, 5 are unbalanced. Does either graph have an Eulerian trail or tour? Left: Can’t go in then out of 1, so no. (Not balanced.) Right: Start/end at the two unbalanced vertices: 2,4,1,5,2,6,3,5

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 47 / 64 Eulerian tours in a directed graph

Theorem A directed graph has an Eulerian tour iff it is balanced and all vertices with degree , 0 form a strongly connected component. If a directed graph is balanced and the undirected version of the graph is connected, the directed graph is strongly connected. A directed graph has an Eulerian trail from x to y (x , y) iff outdegree(x) = indegree(x) + 1 indegree(y) = outdegree(y) + 1 indegree(v) = outdegree(v) for all other vertices The vertices with nonzero degree form a connected component in the undirected version of the graph.

Proof (sketch) For Eulerian tours in undirected graphs, when we entered a vertex, even degrees ensured there was an edge on which to exit. Balance ensures that for directed graphs.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 48 / 64 De Bruijn Sequences

Consider the string s = 00011101. Starting at each position, take 3 consecutive characters, wrapping around back to the start if needed: 0 0 0 1 1 1 0 100 0 0 0 0 0 1 0 1 1 1 1 1 1 1 0 1 0 1 0 10 100

Note that this produced all 3-digit binary strings! String s is a de Bruijn sequence with alphabet A = {0, 1}, alphabet size n = 2, and word size k = 3.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 49 / 64 De Bruijn Sequences

Let A be a finite alphabet of size n, such as A = {0, 1,..., n − 1}.

Let k > 0.

Ak is the set of all length k strings over A, e.g., {0, 1}3 = {000, 001, 010, 011, 100, 101, 110, 111}. It has size |Ak| = nk.

A de Bruijn sequence is a string s such that every element of Ak occurs exactly once in s (possibly wrapping around s at the end).

String s has length nk: Each position in s is the start of a string from Ak, and all strings in Ak occur once in s.

How can we find a de Bruijn sequence? We’ll see a solution involving an Eulerian tour.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 50 / 64 De Bruijn Sequences 001 000 00 01

100 101 011 010 10 11

110 111

(figure from Tesler (2017) Journal of Combinatorics) Form a directed graph ~G(n, k) (called a de Bruijn graph): Vertices V = Ak−1. This example has A= {0, 1}, n=2, and k=3. For each pair of vertices u, v, if removing the first character of u and the last character of v results in the remaining characters , then form a directed edge u → v. Example for k = 6: 00101 → 01011 since 0101 matches. Merge them to give 001011 on the edge: 00101 −−−→001011 01011 The edges represent all nk strings of length k over A.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 51 / 64 De Bruijn Sequences

001 000 00 01

100 101 011 010 10 11

110 111 Find a de Bruijn sequence as follows: Find any Eulerian tour in the graph:

10 −−→100 00 −−→000 00 −−→001 01 −−→011 11 −−→111 11 −−→110 10 −−→101 01 −−→010 10

Take the last character of each vertex (except skip the last vertex since it repeats the first vertex): s = 00011101.

This tour gave the de Bruijn sequence 00011101.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 52 / 64 De Bruijn Sequences

001 000 00 01

100 101 011 010 10 11

110 111 The de Bruijn graph is related to the line graph (from homework).

For a directed graph G, form the line graph L(G) with Vertices of L(G) are the edges of G: V(L(G)) = E(G) For each pair of edges of G of the form e = (x, y), f = (y, z) (the 2nd vertex of e is the 1st vertex of f ), form an edge e → f in L(G).

Then L(G(n, k)) = G(n, k + 1).

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 53 / 64 Generalizations of Eulerian tours and Hamiltonian cycles

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 54 / 64 Postman problem

FE4

2 6 30

Post office 5 G A 8 D

20 7 2

BC3

This graph represents roads on a map.

A weight function ω : E(G) → R gives a cost for each edge (written along the edge in the diagram). This may be the time it takes to travel that road, the physical distance, the fuel cost, etc.

The walk G-D-C-B has cost 8 + 2 + 3 = 13.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 55 / 64 Postman problem a.k.a. Chinese Postman Problem or Route Inspection Problem

FE4

2 6 30

Post office 5 G A 8 D

20 7 2

BC3 Postman problem (introduced by Kwan Mei-Ko in 1960) A postman travels every road at least once to deliver the mail. Find the lowest cost route.

The route is a closed walk that starts and ends at the post office and includes every edge at least once. If an edge is used multiple times, its cost is contributed each time.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 56 / 64 Postman problem

FE4

2 6 30

Post office 5 G A 8 D

20 7 2

BC3

This example is undirected and connected. The problem can be extended to strongly connected directed graphs (e.g., with one-way streets). If all vertices have even degree, the solution is any Eulerian tour. Otherwise, at least one vertex has odd degree. Recall that there is always an even number of vertices whose degrees are odd. We will add paths between them by covering some edges more than once, in order to make all degrees even.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 57 / 64 Postman problem

FE4

2 6 30

Post office 5 G A 8 D

20 7 2

BC3 Here, A, B, D, E have odd degree. We need to pair them up. Here are the possible ways: AB and DE AD and BE AE and BD For m vertices with odd degree, the number of ways to pair them up is (m − 1)(m − 3)(m − 5) ··· 1. Double-factorial: n!! = n(n − 2)(n − 4) ··· down to 2 or 1.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 58 / 64 Postman problem FE4

2 6 30

Post office 5 G A 8 D

20 7 2

BC3 Pairs to consider: AB+DE; AD+BE; AE+BD. Find the lowest cost paths connecting each pair, and the total for that combination of pairs: AB+DE AD+BE AE+BD AB: A-G-B = 12 AD: A-G-D = 13 AE: A-F-E = 6 DE: D-G-E = 14 BE: B-G-E = 13 BD: B-C-D = 5 Total = 26 Total = 26 Total = 11 The lowest cost among these is AE+BD. Later we’ll see an algorithm for finding shortest / lowest cost paths. Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 59 / 64 Postman problem

FE4

4 2 2 6 30

Post office 5 G A 8 D 2 20 7 2 3

BC3

The lowest cost option was to add paths A-F-E and B-C-D.

Add additional edges for those paths, with the same costs as the original edges.

Degrees of endpoints are increased by 1, changing them from odd to even. Degrees in the middle are increased by 2 and stay even.

So now, all vertices have even degree.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 60 / 64 Postman problem FE4 4 2 2 6 30

Post office 5 G A 8 D 2 20 7 2 3

BC3 Use Hierholzer’s algorithm to find an Eulerian tour starting and ending at the post office. Total cost: Add edge costs in the order of the tour shown above: 6 + (4 + 4) + 30 + 2 + (3 + 3) + 2 + 8 + 7 + 20 + (2 + 2) + 5 = 98 Or, just add all edge costs, with proper multiplicities: (2 + 2) + (4 + 4) + 6 + 30 + 5 + 8 + 20 + 7 + (2 + 2) + (3 + 3) = 98

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 61 / 64 Travelling Salesman Problem (TSP) C

10 5 7 D 9 6 8 AB

Consider a complete graph, Kn. Vertices represent cities. There’s a positive cost (such as distance) for each pair of cities. A salesman wants to visit every city exactly once and return to the start, at minimum cost. It’s a Hamiltonian cycle, but we also need to minimize the cost. E.g., cycle (A,B,C,D) (meaning A-B-C-D-A) has cost 8 + 7 + 10 + 9 = 34.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 62 / 64 Travelling Salesman Problem (TSP)

C

10 5 7 D 9 6 8 AB

Brute force solution: consider all n! cycles. Best solution is (A,C,B,D): 5 + 7 + 6 + 9 = 27. It takes n steps to add the numbers together. Doing this for each of the n! cycles yields n · n! steps. Rotating a cycle (e.g., (C,B,D,A)), or flipping it (e.g., (D,B,C,A)), gives an equivalent result. But this only cuts it down by a factor 2n.

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 63 / 64 Travelling Salesman Problem (TSP)

C

10 5 7 D 9 6 8 AB

The fastest known algorithm takes about n22n steps. There is no known efficient way to solve it that’s guaranteed to work in all cases. In Computer Science, it’s classified as an NP-hard problem (covered in CSE 101).

Prof. Tesler Ch. 2. Walks Math 154 / Winter 2020 64 / 64