64
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

Winter 2020 Math 154 Prof. Tesler (Chapters 1.7, 2.1–2.6)gptesler/154/slides/154_walks_20...Walks Math 154 / Winter 2020 11 / 64 Chapter 2: Walks Prof. Tesler Ch. 2. Walks Math 154

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

  • Chapter 2. Walks

    (Chapters 1.7, 2.1–2.6)

    Prof. Tesler

    Math 154Winter 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

    G’

    5 2

    1

    4 3

    5 2

    4 3

    G

    Let G = (V, E) be a graph.A subgraph of G is G ′ = (V ′, E ′) where V ′ ⊆ V, E ′ ⊆ E, andthe edges of E ′ only involve vertices of V ′.If we remove a vertex v from G, we also must remove all edgesincident 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 onspecifying 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

    G − {1,4}G

    5 2

    1

    4 3

    5 2

    3

    Let X be a subset of the vertices of G.G − X is the subgraph of G obtained by removing those verticesand all edges incident with them:

    Vertices: V(G) \ XEdges: { 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)

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

    5 2

    1

    4 3

    5 2

    4 3

    G

    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: XEdges: { e ∈ E(G) : e ⊂ X }

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

  • Subtracting edges from a graph

    3

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

    5 2

    1

    4 3

    5 2

    1

    4

    Let L be a subset of the edges of G.G − L is the subgraph of G with the same vertices as G, but edgesL removed.

    Vertices: V(G)Edges: E(G) \ L

    On the right, vertex 1 is an isolated vertex : a vertex of degree 0.

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

  • Subgraph spanned by edges (only keep those edges)

    3

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

    5 2

    1

    4 3

    5 2

    Let L be a subset of the edges of G.The subgraph of G spanned by L has edge set L, and all verticesincident with edges in L:

    Vertices:⋃e∈L

    e Edges: L

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

  • Spanning subgraph

    3

    G G’

    5 2

    1

    4 3

    5 2

    1

    4

    A similar but different term: a spanning subgraph of G is anysubgraph 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 andadjacent 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

    G G/ {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

    5 2

    1

    4 3

    6

    a c

    e

    i

    b

    fg h

    d

    Route c from station 1 to station 2Route f from station 2 to station 5Route g from station 5 to station 4Route 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, soit’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

    5 2

    1

    4 3

    6

    a c

    e

    i

    b

    fg h

    d

    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 connectedto 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 numberof vertices, k + 1).

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

  • WalksNote: Some authors use these terms differently

    2

    1

    4 3

    6

    a c

    e

    i

    b

    g h

    5

    Trail

    5 2

    1

    4 3

    6

    a c

    e

    i

    b

    g h

    d

    Tour

    5 2

    1

    4 3

    6

    a c

    e

    i

    b

    g h

    Walk

    d d5 2

    1

    4 3

    6

    a c

    e

    i

    b

    g h

    d

    Path

    ff f f

    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, butno other vertices may be repeated).

    A tour is a closed trail (all edges distinct).A cycle is a closed path (all vertices and edges distinct, apart fromstart = end).

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

  • Example: Paths in a computer network

    ISP

    PC1

    PC2

    PC3

    Hard drive

    Printer

    Modem Remote server

    For PC1 to print a page from a website, first it retrieves the pageRemote server→ · · · → ISP→ Modem→ PC1

    and then it sends it to the printerPC1→ 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

    Amy

    Emily

    Gina Harry

    Cindy

    Frank

    Dan

    Irene

    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, EmilyEach is distance 1 from Amy.

    Amy and Frank’s mutual friends: Bob, CindyThey are the middle vertex on a path of length 2 from Amy to Frank.

    Amy’s friends of friends: Frank, Gina, IreneEach is distance 2 from Amy.

    “Six degrees of separation”: A popular concept is that everyoneis 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 indifferent 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

    2 components1 component 2 components

    An undirected graph is connected if for all vertices u, v, there is apath 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 sameconnected 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 edges1

    2

    34

    5

    1

    2

    34

    5 5

    1

    2

    4 3

    Hamiltonian cycleHamiltonian path1,2,3,4,5 1,2,3,4,5,1 1,2,3,4,5,1,3,5,2,4,1

    Eulerian tour

    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 allother vertices are used just once.

    These are used in many algorithms in Computer Science.While the descriptions are similar, there are good solutions for theEulerian 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 theirneighbors. X indicates people who do not know each other andthus may be seated next to each other.

    1 2 3 4 5 6 7 81 X X X X2 X X3 X X X4 X X X5 X X X X6 X X X7 X X X8 X X X X

    Form a graph on vertices {1, . . . , 8}, with an edge {i, j} if i and j maybe 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

    1 2 3 4 5 6 7 81 X X X X2 X X3 X X X4 X X X5 X X X X6 X X X7 X X X8 X X X X

    8

    21

    3

    4

    56

    7

    The solutions correspond to Hamiltonian cycles!Start with 1 and successively follow edges to vertices not yetused, 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 yetgo back to 1!

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

  • Dinner seating arrangements21

    3

    4

    56

    7

    8

    1 5

    2

    8

    63

    7

    4

    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 allgraphs, 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 atthe same circular table. How many dinners can be arranged sothat no one repeats sitting next to the same person?

    After d such dinners, each person will have sat next to 2d peopleout of the n − 1 people besides themself, so 2d 6 n − 1.

    An upper bound on the number of dinners possible is⌊n−1

    2

    ⌋.

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

  • Dinner seating arrangements1

    3

    45

    6

    7 2

    K7

    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 aHamiltonian path/cycle that’s guaranteed to work in all cases.

    Since it’s an NP-complete problem, it’s suspected that there willnever 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

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

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

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

  • Dirac’s Theorem on Hamiltonian cycles

    TheoremLet G be a simple graph on n > 3 vertices with δ(G) > n/2. Then G isHamiltonian (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 ofedges possible.The complete graph Kn is Hamiltonian, so G , Kn. Thus, at leastone pair of vertices is not adjacent. Call them v1 and vn.Add a new edge {v1, vn} to G, obtaining G ′.G ′ has more edges than the largest counterexample, so it musthave a Hamiltonian cycle, C = v1v2 · · · vnv1. (continues . . . )

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

  • Dirac’s Theorem on Hamiltonian cycles7

    1

    4

    6

    8

    2 3

    5

    1

    4

    6

    8

    2 3

    5 7

    G G ′

    We will use the above example for G. However, since proof bycontradiction assumes something that isn’t actually true, thereisn’t an actual example meeting the condition we assumed.

    Add the red edge; number its vertices 1 & 8; and call the graph G ′.

    By our assumption, G doesn’t have a Hamiltonian cycle, but G ′

    does. So there must be a Hamiltonian cycle in G ′ 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

    1

    4

    6

    8

    2 3

    5 7

    Define A= { vi : vi−1 ∈ N(vn) for some i = 2, . . . , n }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 G ′).Since 4 ∈ B, we have a path 1 − 4 in G (and G ′).The red edge 8 − 1 is in G ′ only (not in G).Combined, we have a cycle 8, 3, 4, 1, 8 in G ′.

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

  • Dirac’s Theorem on Hamiltonian cycles

    1

    4

    6

    8

    2 3

    5 7

    1

    4

    6

    8

    2 3

    5 7

    G G ′

    Left: Hamiltonian cycle C in G ′ in yellow4-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

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

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

    Define A= { vi : vi−1 ∈ N(vn) for some i = 2, . . . , n }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!

    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 inG 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.

    Also, |A|+ |B| = d(vn) + d(v1) > n2 +n2 = n, since δ(G) >

    n2 .

    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?

    4,2K

    Let k =⌊n−1

    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 twoparts, so the maximum length of a cycle is 2k.

    So Dirac’s Theorem cannot be extended to δ(G) < n/2, becausesome 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

    TheoremLet 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 pathcould 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 fourland 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 thefoundations of graph theory.

    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 timewe enter on one edge, we exit on another. If we use up all theedges in this fashion, the degree is even.

    If the starting and ending vertices are different, they have odddegrees 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 separateedge. So all vertices in this connected component must have evendegree, except if x , y then x, y have odd degrees.

    y

    x

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

  • Hierholzer’s Algorithm to construct an Eulerian tour

    Theorem (Eulerian tours)A connected graph has an Eulerian tour if and only if all vertices haveeven 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.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.

    x

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

  • Eulerian tours, Proof, Step 1, continuedConstruct 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 wedid 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, continuedConstruct a tour starting at any vertex x

    x

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

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

  • Eulerian tours, Proof, Step 2

    w w

    C

    D

    x x

    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 inStep 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

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

    Proof.Form graph G ′ 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. �

    GeneralizationFor both Eulerian tours and trails, we may replace “connected graph”by “a graph whose vertices with nonzero degree form a singleconnected component” . We may add isolated vertices since they haveno 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

    Strongly connected

    1 2 3

    654

    1 2 3

    654

    Not strongly connected

    A directed graph is strongly connected iff for every pair of verticesx , 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

    TheoremA directed graph has an Eulerian tour iff it is balanced and allvertices with degree , 0 form a strongly connected component.If a directed graph is balanced and the undirected version of thegraph 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) + 1indegree(y) = outdegree(y) + 1indegree(v) = outdegree(v) for all other verticesThe vertices with nonzero degree form a connected component inthe 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, wrappingaround back to the start if needed:

    0 0 0 1 1 1 0 1 0 00 0 0

    0 0 10 1 1

    1 1 11 1 0

    1 0 10 1 0

    1 0 0

    Note that this produced all 3-digit binary strings!String s is a de Bruijn sequence with alphabet A = {0, 1}, alphabetsize 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 stringfrom Ak, and all strings in Ak occur once in s.

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

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

  • De Bruijn Sequences

    00 01

    10 11

    001

    011

    110

    100

    010

    101

    000

    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 uand the last character of v results in the remaining charactersmatching, 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

    00 01

    10 11

    001

    011

    110

    100

    010

    101

    000

    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 vertexsince 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

    00 01

    10 11

    001

    011

    110

    100

    010

    101

    000

    111

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

    For a directed graph G, form the line graph L(G) withVertices 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 andHamiltonian cycles

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

  • Postman problem

    5A

    F E

    B C

    D

    20 2

    6

    3

    7

    8

    30

    4

    2

    G

    Post office

    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 ittakes 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 problema.k.a. Chinese Postman Problem or Route Inspection Problem

    5A

    F E

    B C

    D

    20 2

    6

    3

    7

    8

    30

    4

    2

    G

    Post office

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

    The route is a closed walk that starts and ends at the post officeand 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

    5A

    F E

    B C

    D

    20 2

    6

    3

    7

    8

    30

    4

    2

    G

    Post office

    This example is undirected and connected. The problem can beextended to strongly connected directed graphs (e.g., withone-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 isalways an even number of vertices whose degrees are odd. Wewill add paths between them by covering some edges more thanonce, in order to make all degrees even.

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

  • Postman problem

    5A

    F E

    B C

    D

    20 2

    6

    3

    7

    8

    30

    4

    2

    G

    Post office

    Here, A, B, D, E have odd degree. We need to pair them up. Hereare the possible ways:

    AB and DEAD and BEAE and BD

    For m vertices with odd degree, the number of ways to pair themup 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

    5A

    F E

    B C

    D

    20 2

    6

    3

    7

    8

    30

    4

    2

    G

    Post office

    Pairs to consider: AB+DE; AD+BE; AE+BD.

    Find the lowest cost paths connecting each pair, and the total forthat combination of pairs:

    AB+DE AD+BE AE+BDAB: A-G-B = 12 AD: A-G-D = 13 AE: A-F-E = 6DE: D-G-E = 14 BE: B-G-E = 13 BD: B-C-D = 5

    Total = 26 Total = 26 Total = 11The 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

    3

    A

    F E

    B C

    D

    20 2

    6

    3

    7

    8

    30

    4

    2

    G

    Post office

    5

    2

    4

    2

    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 theoriginal edges.

    Degrees of endpoints are increased by 1, changing them from oddto 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

    A

    F E

    B C

    D

    20 2

    6

    3

    7

    8

    30

    4

    2

    G

    Post office

    5

    2

    4

    2

    3

    Use Hierholzer’s algorithm to find an Eulerian tour starting andending 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)

    10

    A B

    C

    D

    5

    69

    8

    7

    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 thestart, 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 cost8 + 7 + 10 + 9 = 34.

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

  • Travelling Salesman Problem (TSP)

    A B

    C

    D

    5

    69

    8

    710

    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 eachof 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)

    A B

    C

    D

    5

    69

    8

    710

    The fastest known algorithm takes about n22n steps.

    There is no known efficient way to solve it that’s guaranteed towork in all cases. In Computer Science, it’s classified as anNP-hard problem (covered in CSE 101).

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