THE EULER TOUR TECHNIQUE: EVALUATION OF TREE FUNCTIONS

Similar documents
THE EULER TOUR TECHNIQUE: EVALUATION OF TREE FUNCTIONS

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Complexity and Advanced Algorithms Monsoon Parallel Algorithms Lecture 4

March 20/2003 Jayakanth Srinivasan,

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

TREES. Trees - Introduction

Trees. Truong Tuan Anh CSE-HCMUT

1 Leaffix Scan, Rootfix Scan, Tree Size, and Depth

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Associate Professor Dr. Raed Ibraheem Hamed

Chapter 20: Binary Trees

IX. Binary Trees (Chapter 10)

Introduction to Computers and Programming. Concept Question

Binary Trees, Binary Search Trees

Binary Trees


(2,4) Trees. 2/22/2006 (2,4) Trees 1

Trees. CSE 373 Data Structures

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Successor/Predecessor Rules in Binary Trees

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Properties of red-black trees

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Foundations of Computer Science Spring Mathematical Preliminaries

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Data Structure. IBPS SO (IT- Officer) Exam 2017

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

Foundations of Discrete Mathematics

CSE 230 Intermediate Programming in C and C++ Binary Tree

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Programming II (CS300)

PRAM ALGORITHMS: BRENT S LAW

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Binary Heaps in Dynamic Arrays

Parallel Euler tour and Post Ordering for Parallel Tree Accumulations

Euler Tours and Their Applications. Chris Moultrie CSc 8530

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

Algorithms. Red-Black Trees

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Data Structure - Binary Tree 1 -

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Graph Algorithms Using Depth First Search

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Trees. (Trees) Data Structures and Programming Spring / 28

Binary Search Trees Treesort

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Garbage Collection: recycling unused memory

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Trees. Tree Structure Binary Tree Tree Traversals

13.4 Deletion in red-black trees

Search Trees. Undirected graph Directed graph Tree Binary search tree

CSCI2100B Data Structures Trees

Trees. Arash Rafiey. 20 October, 2015

CS301 - Data Structures Glossary By

3. According to universal addressing, what is the address of vertex d? 4. According to universal addressing, what is the address of vertex f?

CS 598: Communication Cost Analysis of Algorithms Lecture 15: Communication-optimal sorting and tree-based algorithms

Lecture 6: Analysis of Algorithms (CS )

COSC 2011 Section N. Trees: Terminology and Basic Properties

Algorithms. AVL Tree

CE 221 Data Structures and Algorithms

v V Question: How many edges are there in a graph with 10 vertices each of degree 6?

Lecture 8: The Traveling Salesman Problem

COMP3121/3821/9101/ s1 Assignment 1

Trees Algorhyme by Radia Perlman

Fundamentals of Data Structure

each processor can in one step do a RAM op or read/write to one global memory location

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Programming II (CS300)

Courant Institute. This research was supported in part by NSF grant DCR and by an IBM faculty development award.

Solutions to relevant spring 2000 exam problems

Elements of Graph Theory

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Multiway Search Trees. Multiway-Search Trees (cont d)

Constructions of hamiltonian graphs with bounded degree and diameter O(log n)

CSE 530A. B+ Trees. Washington University Fall 2013

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Lecture 26. Introduction to Trees. Trees

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

CSI33 Data Structures

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

DATA STRUCTURES USING C

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Lecture 3: Graphs and flows

Lecture 9 March 4, 2010

Analysis of Algorithms

Chapter 6. Parallel Algorithms. Chapter by M. Ghaari. Last update 1 : January 2, 2019.

Transcription:

PARALLEL AND DISTRIBUTED ALGORITHMS BY DEBDEEP MUKHOPADHYAY AND ABHISHEK SOMANI http://cse.iitkgp.ac.in/~debdeep/courses_iitkgp/palgo/index.htm THE EULER TOUR TECHNIQUE: EVALUATION OF TREE FUNCTIONS 2 1

OVERVIEW Tree contraction Evaluation of arithmetic expressions 3 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. 4 2

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. 5 PARALLEL DEPTH-FIRST SEARCH It is difficult to do depth-first search in parallel. We cannot assign depth-first numbering to the node n unless we have assigned depth-first numbering to all the nodes in the subtree A. 6 3

PARALLEL DEPTH-FIRST SEARCH There is a definite order of visiting the nodes in depth-first search. We can introduce additional edges to the tree to get this order. The Euler tour technique converts a tree into a list by adding additional edges. 7 PARALLEL DEPTH-FIRST SEARCH The red (or, magenta ) arrows are followed when we visit a node for the first (or, second) time. If the tree has n nodes, we can construct a list with 2n - 2 nodes, where each arrow (directed edge) is a node of the list. 8 4

EULER TOUR TECHNIQUE For a node v T, p(v) is the parent of v. Each red node in the list represents an edge of the nature < p(v), v >. We can determine the preorder numbering of a node of the tree by counting the red nodes in the list. 9 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

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) = < u 0, u 1,, u d -1 > The successor of edge < u i, v >is: s(< u i, v >) = < v, u (i + 1) mod d >, 0 i (d -1) 12 6

CONSTRUCTING AN EULER TOUR Successor function table The resulting Eulerian Circuit 13 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

CORRECTNESS OF EULER TOUR basis: When the tree has 2 nodes, there is only one edge and one cycle with two edges. Suppose, the claim is true for n nodes. We should show that it is true when there are n + 1 nodes. 15 CORRECTNESS OF EULER TOUR We can introduce an extra node by introducing a leaf to an existing tree, like the leaf v. Initially, adj(u) = <, v, v, >. Hence, s(< v, u >) = < u, v >. 16 8

CORRECTNESS OF EULER TOUR After the introduction of v, adj(u) = <, v, v, v, > s(< v, u >) = < u, v >and s(< v, u >) = < u, v > Hence, there is only one cycle after v is introduced. 17 CONSTRUCTION OF EULER TOUR IN PARALLEL 18 9

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 u i adjacent to v. We need: The successor < v, u (i + 1) mod d >for < u i, v >. This is done by making the list circular. < u i, v >. This is done by keeping a direct pointer from u i in L[v] to v in L[u i ]. 19 CONSTRUCTION OF EULER TOUR IN PARALLEL We can construct an Euler tour in O(1) time using O(n) processors. One processor is assigned to each node of the adjacency list. There is no need of concurrent reading, hence the EREW PRAM model is sufficient. 20 10

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(< u d -1, r >) = 0. u d -1 is the last vertex adjacent to r. In other words, we break the Euler tour at r. 21 ROOTING A TREE 22 11

ROOTING A TREE Perform a parallel prefix sum with a weight of one assigned to each arc. 23 ROOTING A TREE Input: The Euler tour of a tree and a special vertex r. Output: For each vertex v r, the parent p(v) of v in the tree rooted at r. 24 12

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

POSTORDER NUMBERING Input: A rooted tree with root r, and the corresponding Euler path defined by the function s. Output: For each vertex v, the postorder number post(v) of each vertex v. 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

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

TREE CONTRACTION Some tree computations cannot be solved efficiently with the Euler tour technique alone. An important problem is evaluation of an arithmetic expression given as a binary tree. 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

THE RAKE OPERATION Let T = (V, E) be a rooted binary tree and for each vertex v, p(v) is its parent. sib(v) is the child of p(v). We consider only binary trees. In the rake operation for a leaf u such that p(u) r. Remove u and p(u) from T, and Connect sib(u) to p(p(u)). 33 THE RAKE OPERATION In our tree contraction algorithm, we apply the rake operation repeatedly to reduce the size of the binary tree. We need to apply rake to many leaves in parallel in order to achieve a fast running time. 34 17

THE RAKE OPERATION But we cannot apply rake operation to nodes whose parents are adjacent on the tree. For example, rake operation cannot be applied to nodes 1 and 8 in parallel. We need to apply the rake operation to nonconsecutive leaves as they appear from left to right. 35 THE RAKE OPERATION We first label the leaves consecutively from left to right. In an Euler path for a rooted tree, the leaves appear from left to right. We can assign a weight 1 to each edge of the kind (v, p(v)) where v is a leaf. We exclude the leftmost and the rightmost leaves. These two leaves will be the two children of the root when the tree is contracted to a three-node tree. We do a prefix sum on the resulting list and the leaves are numbered from left to right. 36 18

THE RAKE OPERATION We now store all the n leaves in an array A (except the left most and right most leaves). A odd is the subarray consisting of the oddindexed elements of A. A even is the subarray consisting of the evenindexed elements of A. We can create the arrays A odd and A even in O(1) time and O(n) work. 37 TREE CONTRACTION ALGORITHM begin for iterations do 1. Apply the rake operation in parallel to all the elements of A odd that are left children 2. Apply the rake operation in parallel to the rest of the elements in A odd. 3. Set A := A even. end 38 19

TREE CONTRACTION ALGORITHM 39 CORRECTNESS OF TREE CONTRACTION Whenever the rake operation is applied in parallel to several leaves, the parents of any two such leaves are not adjacent. The number of leaves reduces by half after each iteration of the loop. Hence the tree is contracted in O(log n) time. Euler tour takes O(n) work. The total number of operations for all the iterations of the loop is: 40 20

EVALUATION OF ARITHMETIC EXPRESSIONS If we evaluate an expression tree bottom-up, it will take O(n) time for a long and skinny tree. Hence we apply tree contraction. 41 EVALUATION OF ARITHMETIC EXPRESSIONS We do not completely evaluate each internal node. We evaluate the internal nodes partially. For each internal node v, we associate a label (a v, b v ). a v and b v are constants. The value of the expression at node is: (a v X + b v ), where X is an unknown value for the expression of the subtree rooted at v, and + is the operator stored in node v. 42 21

KEEPING AN INVARIANT Invariant: Let u be an internal node which holds the operation,. Let v and w are the children of u with labels (a v, b v ) and (a w, b w ). Then the value at u is: val(u) = (a v val(v) + b v ) (a w val(w) + b w ) 43 KEEPING AN INVARIANT 44 22

APPLYING THE RAKE OPERATION The value at node u is: val(u) = (a v c v + b v ) (a w X + b w ) X is the unknown value at node w. 45 APPLYING THE RAKE OPERATION The contribution of val(u) to the value of node p(u) is: a u val(u) + b u = a u [(a v c v + b v ) (a w X + b w )] + b u We can adjust the labels of node w to (a w, b w ) a w = a u (a v c v + b v ) a w b w = a u (a v c v + b v ) b w + b u 46 23

COMPLEXITY OF EXPRESSION EVALUATION The correctness of the expression evaluation depends on correctly maintaining the invariants. We start with a label (1, 0) for each leaf and correctly maintain the invariant at each rake operation. We have already proved the correctness of the rake operation. Hence, evaluation of an expression given as a binary tree takes O(n) work and O(log n) time. 47 AN EXAMPLE 48 24

AN EXAMPLE 49 AN EXAMPLE 50 25