The Euler Tour Technique: Evaluation of Tree Functions
The Euler Tour Technique: Evaluation of Tree Functions
1
05‐09‐2015
OVERVIEW
PROBLEMS IN PARALLEL
COMPUTATIONS OF TREE
FUNCTIONS
Computations of tree functions are important
for designing many algorithms for trees and
graphs.
Some of these computations include preorder,
postorder, inorder numbering of the nodes of
a tree, number of descendants of each vertex,
level of each vertex etc.
2
05‐09‐2015
PROBLEMS IN PARALLEL
COMPUTATIONS OF TREE
FUNCTIONS
Most sequential algorithms for these
problems use depth-first search for
solving these problems.
However, depth-first search seems to be
inherently sequential in some sense.
PARALLEL DEPTH-FIRST
SEARCH
3
05‐09‐2015
PARALLEL DEPTH-FIRST
SEARCH
PARALLEL DEPTH-FIRST
SEARCH
4
05‐09‐2015
EULER TOUR
TECHNIQUE
EULER TOUR
TECHNIQUE
Let T = (V, E) be a given tree and let T’ = (V, E’ ) be
a directed graph obtained from T.
Each edge (u, v) E is replaced by two edges < u,
v > and < v, u >.
Both the indegree and outdegree of an internal
node of the tree are now same.
The indegree and outdegree of a leaf is 1 each.
Hence T’ is an Eulerian graph: ie. it has a directed
circuit that traverses each arc exactly once.
10
5
05‐09‐2015
EULER TOUR
TECHNIQUE
An Euler circuit of a graph is an edge-disjoint
circuit which traverses all the nodes.
A graph permits an Euler circuit if and only if
each vertex has equal indegree and
outdegree.
An Euler circuit can be used for optimal
parallel computation of many tree functions.
To construct an Euler circuit, we have to
specify the successor edge for each edge.
11
CONSTRUCTING AN
EULER TOUR
Each edge on an Euler circuit has a unique
successor edge.
For each vertex v V we fix an ordering of the
vertices adjacent to v.
If d is the degree of vertex v, the vertices
adjacent to v are:
adj(v) = < u0, u1, …, ud -1 >
The successor of edge < ui, v > is:
s(< ui, v >) = < v, u(i + 1) mod d >, 0 i (d - 1)
12
6
05‐09‐2015
CONSTRUCTING AN
EULER TOUR
CORRECTNESS OF
EULER TOUR
Consider the graph T’ = (V, E’ ) , where E’ is
obtained by replacing each e E by two directed
edges of opposite directions.
Lemma: The successor function s defines only
one cycle and not a set of edge-disjoint cycles in
T’.
Proof: We have already shown that the graph is
Eulerian.
We prove the lemma through induction.
14
7
05‐09‐2015
CORRECTNESS OF
EULER TOUR
basis: When the tree has 2 nodes, there is
only one edge and one cycle with two
edges.
15
CORRECTNESS OF
EULER TOUR
16
8
05‐09‐2015
CORRECTNESS OF
EULER TOUR
17
CONSTRUCTION OF EULER
TOUR IN PARALLEL
18
9
05‐09‐2015
CONSTRUCTION OF EULER
TOUR IN PARALLEL
We assume that the tree is given as a set of
adjacency lists for the nodes. The adjacency list
L[v] for v is given in an array.
Consider a node v and a node ui adjacent to v.
We need:
The successor < v, u(i + 1) mod d > for < ui, v >. This is done
by making the list circular.
< ui, v >. This is done by keeping a direct pointer from ui
in L[v] to v in L[ui].
19
CONSTRUCTION OF EULER
TOUR IN PARALLEL
We can construct an Euler tour in O(1) time
using O(n) processors.
20
10
05‐09‐2015
ROOTING A TREE
For doing any tree computation, we need to
know the parent p(v) for each node v.
Hence, we need to root the tree at a vertex r.
We first construct an Euler tour and for the
vertex r, set s(< ud -1, r >) = 0.
ud -1 is the last vertex adjacent to r.
In other words, we break the Euler tour at r.
21
ROOTING A TREE
22
11
05‐09‐2015
ROOTING A TREE
Perform a parallel
prefix sum with a
weight of one
assigned to each arc.
23
ROOTING A TREE
24
12
05‐09‐2015
ROOTING A TREE
begin
1. Set s(< u, r >) = 0, where u is the last vertex in
the adjacency list of r.
2. Assign a weight 1 to each edge of the list
and compute parallel prefix.
3. For each edge < x, y >, set x = p(y) whenever
the prefix sum of < x, y > is smaller than the
prefix sum of < y, x >.
end
25
ROOTING A TREE
26
13
05‐09‐2015
POSTORDER
NUMBERING
Input: A rooted tree with root r, and the
corresponding Euler path defined by the
function s.
27
POSTORDER
NUMBERING
The Euler path (EP) can be used to solve this.
The EP visits each vertex several times, the
first time by the arc <p(v),v>, and the last
time by the arc <v,p(v)>, after visiting all the
descendants of v.
Thus ordered sublist of all vertices obtained
by retention of the last occurrence of each
vertex defines precisely the postorder
traversal of the vertices of T.
How can we do that?
28
14
05‐09‐2015
POSTORDER
NUMBERING
begin
1. For each vertex v≠r, assign the weights
w(<v,p(v)>)=1, and w(<p(v),v>)=0.
2. Perform parallel prefix sum on the list of arcs
defined by s.
3. For each vertex v≠r, set post(v) equal to the
prefix sum of <v,p(v)>. For v=r, set post(r)=n,
where n is the number of vertices in the
given tree.
end
29
COMPUTATION OF TREE
FUNCTIONS
Given a tree T, for many tree computations:
We first construct the Euler tour of T
Then we root the tree at a vertex
We can compute:
The postorder number of each vertex
The preorder number of each vertex
The inorder number of each vertex
The level of each vertex
The number of descendants of each vertex.
30
15
05‐09‐2015
TREE CONTRACTION
31
TREE CONTRACTION
Each leaf holds a constant and each internal node
holds an arithmetic operator like +,.
The goal is to compute the value of the expression at
the root.
The tree contraction technique is a systematic way
of shrinking a tree into a single vertex.
We successively apply the operation of merging a
leaf with its parent or merging a degree-2 vertex with
its parent.
32
16
05‐09‐2015
33
34
17
05‐09‐2015
35
18
05‐09‐2015
37
TREE CONTRACTION
ALGORITHM
begin
for iterations do
1. Apply the rake operation in parallel to all the
elements of Aodd that are left children
2. Apply the rake operation in parallel to the
rest of the elements in Aodd.
3. Set A := Aeven.
end
38
19
05‐09‐2015
TREE CONTRACTION
ALGORITHM
39
CORRECTNESS OF TREE
CONTRACTION
40
20
05‐09‐2015
TREE COMPUTATIONS
Rooting a tree:
41
21