<<

18.434: Seminar in Theoretical Computer Science Feb 26, 2015 Euler Tour Technique

Alex Grinman [email protected]

1 The Dynamic Connectivity Problem with Forests

Problem: Let graph G be a set of undirected, disjoint trees. G is dynamically changed such that edges are added and removed, with the one requirement that no cycles can ever be created. Given two nodes u, v determine whether or not there exists a from u → v. We define the following functions:

• link(u, v): adds an edge from u to v (assume always legal, no ) • cut(u, v): remove edge (u, v) • connected(u, v): returns true ⇐⇒ there exists a path from u to v.

Example: Let G = T1 ∪ T2, where each Ti is an undirected . T1 : { a-b, b-c}, T2 : { d-e, e-g, e-f} 1. cut(e, f) 2. link(b, d) 3. connected(c, d) =? Ideas: 1. Naive ? Try to perform BFS from u and report false if v not found. (Linear time) 2. Naive ? Keep track of which nodes connected to each other? Updating data structure initially seems expensive. This is where Euler Tour Trees come in!

2 Euler Tours

Definition: An Eulerian Tour or Circuit is a path in an undirected connected graph which visits every edge exactly once and ends at the same at which it starts. Important: we will assume we can always lexigraphically order nodes (i.e. a is before b, d is before e).

Example: Let graph G : { a-b, a-c, c-d, b-d}. Euler Tour is a − b − d − c − a.

2.1 Euler Tour on Trees Question: How do we use Euler Tours on trees? Method: Given an undirected tree T create an isomorphic T 0 that contains an Eulerian Tour. 1. for each edge (u, v) ∈ T make (u, v) directed: u → v. 2. Then, for each edge (u, v) ∈ T , create new edge (v, u) with direction v → u. Note that for each edge we are splitting the non-directed edge into two opposite direction edges. This pre- serves the connectivity of the tree while allowing us to create a simple Euler tour on the tree. T 0 is no longer a tree, but it is essentially equivalent in connectivity to the tree T .

Example: Let T : { a-b, b-c, b-g, b-d, d-e, d-f}. Then after our conversion above, T 0 = {a ↔ b, b ↔ c, b ↔ g, b ↔ d, d ↔ e, d ↔ f}. Euler tour on T 0 is: a → b → c → b → g → b → d → e → d → f → d → b → a.

1 3 Euler Tour Technique

We can solve the dynamic connectivity on a forest problem by maintaining a group of Euler Tour Tree data structures. Each Euler Tour tree is a balanced .The idea is that the Euler Tour of each tree in the forest, as specified in section 2.1, will be stored in a Balanced Binary tree keyed on the index in which a node appears in the Euler tour.

Example: Let G be a forest of two trees T1 and T2 such that G = T1 ∪ T2. Let T1 : { a-b, b-c, c-e, b-d} and T2 : { f-g, g-i, g-h}

1. Create Euler Tour tree on T1 =⇒ Eulerian tour ET1 = {a → b → d → c → e → c → b → a}

2. Create Euler Tour tree on T2 =⇒ Eulerian tour ET2 = {f → g → h → g → f}

3.1 Euler Tour Trees as Lists (Balanced Binary Trees) As we know from 6.046, given a list of comparable objects, we can represent that list as a balanced binary tree (BBT ). In the interest of time we will hand wave the exact operations on merging/splitting balanced binary trees, and instead we will represent our Euler Tour Trees as the Euler Tour path (a list of the nodes in the order(s) they appear). 1. first and last appearances signify cutting/linking points 2. we can always (in O(log n)) rotate the root of the Euler Tour in our BBT NOTE: We will assume that all operations on lists are easy (O(log n)) because they are just operations on BBTs.

3.2 Dynamic Connectivity Operations Let D be a hash map, where the key u is the root node of a tree in the dynamic connectivity forest and the value is a pointer to the Euler tour tree corresponding to the tree in the forest rooted on u. We also have a map ED that maps edges (u, v) to first and last occurrences of u and v.

We now describe each of the dynamic connectivity operations and their run-time with our data structure D:

• link(u, v) : Let ET1 = D[find_root(u)] and ET2 = D[find_root(v)].

Create ET1,2 = ET1 ∪ ET2. (this is the merge operation on BBTs) Remove D[find_root(u)] and D[find_root(v)].

Set D[ET1,2[0]] = ET1,2

• cut(u, v) : Let ET1 = D[find_root(u)] 0 Separate ET1 into ET1 and ET2, using BBT operation on u ↔ v. v now is the root of 0 ET2. ET1 is ET1 without the subtree rooted at v. 0 Set D[find_root(u)] = ET1. Set D[v] = ET2 • connected(u, v) : return true ⇐⇒ (find_root(u) = find_root(v)) link/cut simply reduce to merge/split on Balanced Binary Trees, a O(log n) operation. And connected reduces to two find_root operations. Hence, each Dynamic Connectivity on Forests operation is O(log n) where n is the number of nodes in the Forest.

References

[1] Wikipedia. Euler Tour Techniques. http://en.wikipedia.org/wiki/Euler_tour_technique [2] Stanford CS166. Euler Tour Trees. http://web.stanford.edu/class/archive/cs/cs166/cs166.1146/lectures/04/Small04.pdf

2