Algorithm Search⌘K

Proofs: Trémaux traversal

These are the human proofs of Trémaux traversal, at full length.

Trémaux's rule walks each edge of the component twice and returns to the start full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) be an undirected multigraph, s∈Vs \in V, and CC the vertex set of the component of ss. The walker is at vv. Call the vertices with $\mathrm{visited} = $ true visited. For each visited u≠su \ne s, the edge entry[u]\mathrm{entry}[u] joins uu to the vertex p(u)p(u) at which the walker stood when he first entered uu. Let PP be the sequence s=u0,u1,…,uk=vs = u_0, u_1, \ldots, u_k = v in which ui−1=p(ui)u_{i-1} = p(u_i); we show that it exists.

Invariant. At the start of each iteration of the loop of line L2:

  1. the entry edges of the visited vertices other than ss form a tree TT on the visited vertices, and vv is visited, so PP is the path in TT from ss to vv;
  2. an edge has mark 1 if and only if it is entry[ui]\mathrm{entry}[u_i] for some i≥1i \ge 1;
  3. each visited vertex that is not on PP has no edge with mark 0;
  4. the walk so far goes from ss to vv, an edge with mark 0 has not been walked, and an edge with mark jj has been walked jj times.

Initialization. Only ss is visited, TT has no edges, P=(s)P = (s), all marks are 0 and the walk is empty. All four parts hold.

A consequence. The edges with mark 1 at vv are entry[v]\mathrm{entry}[v], if v≠sv \ne s, and nothing else. By part 2, an edge with mark 1 is entry[ui]={ui−1,ui}\mathrm{entry}[u_i] = \{u_{i-1}, u_i\}. It is not a loop, because uiu_i was unvisited and p(ui)p(u_i) visited when it was set. If it contains v=ukv = u_k, then ui=vu_i = v or ui−1=vu_{i-1} = v, and a vertex is on a path only once, so i=ki = k.

Maintenance. Consider the three cases of one iteration.

  • Line L5. The edge e={v,w}e = \{v, w\} has mark 0 and ww is visited. The walker goes to ww and back, ee gets mark 2 and has been walked twice. Nothing else changes, so all parts still hold.
  • Line L6. The edge e={v,w}e = \{v, w\} has mark 0 and ww is not visited. Now ww is visited with entry[w]=e\mathrm{entry}[w] = e and p(w)=vp(w) = v, so TT grows by a leaf, and PP becomes u0,…,uk,wu_0, \ldots, u_k, w. The edge ee has mark 1, it has been walked once, and it is the entry edge of the new last vertex of PP. Part 3 is unchanged because ww is on PP.
  • Line L8. No edge at vv has mark 0, and v≠sv \ne s. The walker goes along entry[v]\mathrm{entry}[v] to uk−1u_{k-1}, which gives that edge its second walk and mark 2. Now PP is u0,…,uk−1u_0, \ldots, u_{k-1}, which agrees with part 2. The vertex vv leaves PP and has no edge with mark 0, so part 3 holds.

Termination. Each iteration that does not stop raises the sum of all marks by 1 or 2, and the sum is at most 2∣E∣2|E|, because a mark never exceeds 2: lines L4 to L6 act only on an edge with mark 0, and line L8 acts only on entry[v]\mathrm{entry}[v], which has mark 1 by part 2. So the loop stops after at most 2∣E∣2|E| iterations.

Lemma 1 (local rule). At each step, the walker can choose by the local marks alone: an edge at vv with mark 0 for line L3, and, for line L8, the only edge at vv with mark 1.

Proof. This is the consequence above. □\square

Lemma 2. When the walker stops, he is at ss, every vertex of CC is visited, every edge with an end in CC has mark 2, and no other edge has a mark.

Proof. Line L9 runs only if entry[v]=NIL\mathrm{entry}[v] = \mathrm{NIL}, so v=sv = s, P=(s)P = (s), and no edge at ss has mark 0. By part 2, no edge has mark 1. By part 3, and the test at ss, no visited vertex has an edge with mark 0. Suppose that a vertex of CC is not visited. A path from ss to it has an edge {x,y}\{x, y\} with xx visited and yy not. This edge has mark 2, so it has been walked, by part 4, and the walker has stood at yy. Each arrival at a vertex happens at line L4, L5 or L8, and an arrival at an unvisited vertex makes it visited at line L6. So yy is visited, a contradiction. So all of CC is visited, and each edge with an end in CC has mark 2. The walk stays in CC, so other edges are never walked and have mark 0. □\square

Lemma 3 (Trémaux tree). At the end, TT is a spanning tree of CC, and each edge {x,y}\{x, y\} of the component that is not in TT joins an ancestor and a descendant in TT.

Proof. TT is a tree on the visited vertices, which are CC, by part 1 and Lemma 2. A non-tree edge {x,y}\{x, y\} gets its first mark at line L4 when the walker stands at one end, say xx, and line L5 applies, so yy is visited at that moment. If yy were not on the path PP from ss to xx at that moment, then by part 3 it would have no edge with mark 0, but {x,y}\{x, y\} has mark 0 then. So yy is on the tree path from ss to xx, which makes it an ancestor of xx. A loop x=yx = y joins xx to itself. □\square

Theorem. TREMAUX(G,s)(G, s) stops after a closed walk from ss that visits every vertex of the component of ss, walks each edge of the component exactly twice, never walks another edge, and needs at each step only the marks at the current vertex. Its entry edges form a spanning tree of the component in which every other edge joins an ancestor and a descendant.

Proof. Termination and Lemma 2 give the walk and its end at ss. By part 4 and Lemma 2, each edge of the component has been walked exactly twice and every other edge never. Lemma 1 gives the local rule and Lemma 3 the tree. ■\blacksquare

Back to Trémaux traversal