Successfully reported this slideshow.
Upcoming SlideShare
×

# Parallel Algorithms for Trees

5,102 views

Published on

How to operate on tree data structures using multiple parallel threads of execution. Parallel algorithms allows for achieving higher performance on multi-core machines, but getting the correct result from parallel execution can be tricky. This set of slides illustrates a few primary algorithms that operate on tree data structures in parallel.

Published in: Education, Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• i like this slide very much

Are you sure you want to  Yes  No

### Parallel Algorithms for Trees

1. 1. Parallel Algorithms for Trees Ahmad Khayyat Department of Electrical & Computer Engineering ahmad.khayyat@ece.queensu.ca Parallel Computation: Models and Methods Selim G. Akl Chapter 6 CISC 879 — Algorithms and Applications Queen’s University October 29, 2008ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 1
2. 2. Introduction Building Euler Tours Applications of Euler Tours Conclusion Outline 1 Introduction 2 Building Euler Tours 3 Applications of Euler Tours 4 ConclusionECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 2
3. 3. Introduction Building Euler Tours Applications of Euler Tours Conclusion Graphs Outline 1 Introduction Graphs Euler Tours 2 Building Euler Tours 3 Applications of Euler Tours 4 ConclusionECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 3
4. 4. Introduction Building Euler Tours Applications of Euler Tours Conclusion Graphs Graphs A Graph G = (V, E) is a set of vertices V connected by a set of edges E. If the edges have orientation, the graph is directed. A path is an ordered list of edges in the form (vi , vj ), (vj , vk ), (vk , vl ). A cycle is a path that begins and ends at the same vertex. The degree of a vertex in an undirected graph is the number of edges adjacent to the vertex. The in-degree of a vertex v in a directed graph is the number of edges entering v, The out-degree of a vertex v in a directed graph is the number of edges leaving v.ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 4
5. 5. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Outline 1 Introduction Graphs Euler Tours 2 Building Euler Tours 3 Applications of Euler Tours 4 ConclusionECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 5
6. 6. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Euler Tours Euler Tour (ET) A cycle where every edge of the graph appears in the cycle exactly once. An undirected graph has an Euler tour if each vertex has an even degree. v4 v6 v3 v5 v1 v7 v2ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 6
7. 7. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Euler Tours Euler Tour (ET) A cycle where every edge of the graph appears in the cycle exactly once. An undirected graph has an Euler tour if each vertex has an even degree. (v1 , v7 ), (v7 , v2 ), (v2 , v3 ), (v3 , v6 ) v4 v6 v3 v5 v1 v7 v2ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 6
8. 8. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Euler Tours Euler Tour (ET) A cycle where every edge of the graph appears in the cycle exactly once. An undirected graph has an Euler tour if each vertex has an even degree. (v1 , v7 ), (v7 , v2 ), (v2 , v3 ), (v3 , v6 ) v4 v6 v3 (v6 , v7 ), (v7 , v5 ), (v5 , v6 ) v5 v1 v7 v2ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 6
9. 9. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Euler Tours Euler Tour (ET) A cycle where every edge of the graph appears in the cycle exactly once. An undirected graph has an Euler tour if each vertex has an even degree. (v1 , v7 ), (v7 , v2 ), (v2 , v3 ), (v3 , v6 ) v4 v6 v3 (v6 , v7 ), (v7 , v5 ), (v5 , v6 ) v5 (v6 , v4 ), (v4 , v5 ), (v5 , v1 ) v1 v7 v2ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 6
10. 10. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Euler Tours Euler Tour (ET) A cycle where every edge of the graph appears in the cycle exactly once. A directed graph has an Euler tour if the in-degree of each vertex is equal to the out-degree of the vertex. v1 v2 v3 v4 v5 v6ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 6
11. 11. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Euler Tours Euler Tour (ET) A cycle where every edge of the graph appears in the cycle exactly once. A directed graph has an Euler tour if the in-degree of each vertex is equal to the out-degree of the vertex. (v1 , v2 ), (v2 , v3 ), (v3 , v4 ) v1 v2 v3 v4 v5 v6ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 6
12. 12. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Euler Tours Euler Tour (ET) A cycle where every edge of the graph appears in the cycle exactly once. A directed graph has an Euler tour if the in-degree of each vertex is equal to the out-degree of the vertex. (v1 , v2 ), (v2 , v3 ), (v3 , v4 ) v1 v2 (v4 , v2 ), (v2 , v4 ) v3 v4 v5 v6ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 6
13. 13. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Euler Tours Euler Tour (ET) A cycle where every edge of the graph appears in the cycle exactly once. A directed graph has an Euler tour if the in-degree of each vertex is equal to the out-degree of the vertex. (v1 , v2 ), (v2 , v3 ), (v3 , v4 ) v1 v2 (v4 , v2 ), (v2 , v4 ) v3 v4 (v4 , v5 ), (v5 , v6 ), (v6 , v4 ), (v4 , v6 ) v5 v6ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 6
14. 14. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Euler Tours Euler Tour (ET) A cycle where every edge of the graph appears in the cycle exactly once. A directed graph has an Euler tour if the in-degree of each vertex is equal to the out-degree of the vertex. (v1 , v2 ), (v2 , v3 ), (v3 , v4 ) v1 v2 (v4 , v2 ), (v2 , v4 ) v3 v4 (v4 , v5 ), (v5 , v6 ), (v6 , v4 ), (v4 , v6 ) (v6 , v5 ), (v5 , v1 ) v5 v6ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 6
15. 15. Introduction Building Euler Tours Applications of Euler Tours Conclusion Euler Tours Trees Tree An undirected graph that is connected and contains no cycles. A tree with n vertices has exactly n − 1 edges. A directed tree (DT) with n vertices has 2n − 2 edges. ´ Its ET is a sequence of 2n − 2 edges. v3 v1 v4 v3 v1 v4 v2 v5 v2 v5 v7 v6 v7 v6ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 7
16. 16. Introduction Building Euler Tours Applications of Euler Tours Conclusion Data Structure Outline 1 Introduction 2 Building Euler Tours Data Structure The Algorithm Analysis 3 Applications of Euler Tours 4 ConclusionECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 8
17. 17. Introduction Building Euler Tours Applications of Euler Tours Conclusion Data Structure Directed Trees (DT) as Linked Lists n linked lists, one for each vertex. The nodes of a list are edges leaving that vertex. head(v1 ) (v1 , v3 ) (v1 , v4 ) (v1 , v5 ) head(v2 ) (v2 , v7 ) v3 v1 v4 head(v3 ) (v3 , v1 ) head(v4 ) (v4 , v1 ) v2 v5 head(v5 ) (v5 , v1 ) (v5 , v6 ) (v5 , v7 ) v7 v6 head(v6 ) (v6 , v5 ) head(v7 ) (v7 , v2 ) (v7 , v5 )ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 9
18. 18. Introduction Building Euler Tours Applications of Euler Tours Conclusion The Algorithm Outline 1 Introduction 2 Building Euler Tours Data Structure The Algorithm Analysis 3 Applications of Euler Tours 4 ConclusionECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 10
19. 19. Introduction Building Euler Tours Applications of Euler Tours Conclusion The Algorithm Algorithm Input and Output Input: n linked lists representing a directed tree (DT). Output: Compute the Euler tour (ET) for the given DT: ´ Arrange all the edges of the DT in a single linked list. ´ Each edge (vi , vj ) is followed by an edge (vj , vk ). ´ The ﬁrst edge leaves some vertex vl , the last edge enters vl . 1 4 (v1 , v3 ) (v3 , v1 ) (v1 , v4 ) (v4 , v1 ) v3 v1 v4 2 3 5 12 (v1 , v5 ) (v5 , v6 ) (v6 , v5 ) (v5 , v7 ) v2 v5 9 8 7 10 11 6 (v7 , v2 ) (v2 , v7 ) (v7 , v5 ) (v5 , v1 ) v7 v6ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 11
20. 20. Introduction Building Euler Tours Applications of Euler Tours Conclusion The Algorithm Building the Euler Tour in Parallel Assuming the shared-memory model, with n − 1 processors. Each processor Pij , i < j, is in charge of two edges: (vi , vj ) and (vj , vi ). Pij determines the position (actually, the successor) of the two nodes holding (vi , vj ) and (vj , vi ) (or ij and ji) — in ET. Successor of (vi , vj ) Successor of (vj , vi ) if next(ji) = jk then if next(ij) = im then succ(ij) ← jk succ(ji) ← im else else succ(ij) ← head(vj ) succ(ji) ← head(vi ) end if end ifECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 12
21. 21. Introduction Building Euler Tours Applications of Euler Tours Conclusion The Algorithm Example: Pij = P13 Successor of (v1 , v3 ) Successor of (vi , vj ) v3 v1 v4 if next(ji) = jk then succ(ij) ← jk v2 v5 else succ(ij) ← head(vj ) v7 v6 end if head(v1 ) (v1 , v3 ) (v1 , v4 ) (v1 , v5 ) . . . head(v3 ) (v3 , v1 )ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 13
22. 22. Introduction Building Euler Tours Applications of Euler Tours Conclusion The Algorithm Example: Pij = P13 Successor of (v1 , v3 ) Successor of (vi , vj ) v3 v1 v4 if next(ji) = jk then succ(ij) ← jk v2 v5 else succ(ij) ← head(vj ) v7 v6 end if head(v1 ) (v1 , v3 ) (v1 , v4 ) (v1 , v5 ) next(31) = null . . . head(v3 ) (v3 , v1 ) 31ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 13
23. 23. Introduction Building Euler Tours Applications of Euler Tours Conclusion The Algorithm Example: Pij = P13 Successor of (v1 , v3 ) Successor of (vi , vj ) v3 v1 v4 if next(ji) = jk then succ(ij) ← jk v2 v5 else succ(ij) ← head(vj ) v7 v6 end if 13 head(v1 ) (v1 , v3 ) (v1 , v4 ) (v1 , v5 ) succ(13) ← head(v3 ) . . . (v1 , v3 ) (v3 , v1 ) head(v3 ) (v3 , v1 )ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 13
24. 24. Introduction Building Euler Tours Applications of Euler Tours Conclusion The Algorithm Example: Pij = P13 Successor of (v3 , v1 ) Successor of (vj , vi ) v3 v1 v4 if next(ij) = im then succ(ji) ← im v2 v5 else succ(ji) ← head(vi ) v7 v6 end if head(v1 ) (v1 , v3 ) (v1 , v4 ) (v1 , v5 ) . . . head(v3 ) (v3 , v1 )ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 14
25. 25. Introduction Building Euler Tours Applications of Euler Tours Conclusion The Algorithm Example: Pij = P13 Successor of (v3 , v1 ) Successor of (vj , vi ) v3 v1 v4 if next(ij) = im then succ(ji) ← im v2 v5 else succ(ji) ← head(vi ) v7 v6 end if 13 14 head(v1 ) (v1 , v3 ) (v1 , v4 ) (v1 , v5 ) next(13) = 14 . . . head(v3 ) (v3 , v1 )ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 14
26. 26. Introduction Building Euler Tours Applications of Euler Tours Conclusion The Algorithm Example: Pij = P13 Successor of (v3 , v1 ) Successor of (vj , vi ) v3 v1 v4 if next(ij) = im then succ(ji) ← im v2 v5 else succ(ji) ← head(vi ) v7 v6 end if 14 head(v1 ) (v1 , v3 ) (v1 , v4 ) (v1 , v5 ) succ(31) ← 14 . . . (v1 , v3 ) (v3 , v1 ) (v1 , v4 ) head(v3 ) (v3 , v1 ) 31ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 14
27. 27. Introduction Building Euler Tours Applications of Euler Tours Conclusion Analysis Outline 1 Introduction 2 Building Euler Tours Data Structure The Algorithm Analysis 3 Applications of Euler Tours 4 ConclusionECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 15
28. 28. Introduction Building Euler Tours Applications of Euler Tours Conclusion Analysis Correctness Visits all the edges: ´ Every pair of edges is assigned to a processor. Produces a single cycle, not several small cycles: (vi , vj ) (vj , vk ) vi vj vk head(vj ) (vj , vi ) (vj , vk ) next(ji) = jkECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 16
29. 29. Introduction Building Euler Tours Applications of Euler Tours Conclusion Analysis Correctness Visits all the edges: ´ Every pair of edges is assigned to a processor. Produces a single cycle, not several small cycles: (vi , vj ) (vj , vk ) (vj , vi ) (vk , vj ) vi vj vk head(vj ) (vj , vi ) (vj , vk ) next(ji) = jk next(jk) = ji, or head(vj ) = jiECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 16
30. 30. Introduction Building Euler Tours Applications of Euler Tours Conclusion Analysis Complexity t(n) = O(1) p(n) = n − 1 = O(n) ⇒ c(n) = O(n)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 17
31. 31. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations Outline 1 Introduction 2 Building Euler Tours 3 Applications of Euler Tours Utility Computations Simple Applications of Euler Tours Computing Minima 4 ConclusionECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 18
32. 32. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations Pointer Jumping parent(vi ) ← parent(parent(vi )) v0 v1 Find the root of v8 . v2 v3 v4 v5 v6 v7 v8ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 19
33. 33. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations Pointer Jumping parent(vi ) ← parent(parent(vi )) v0 v1 1 Find the root of v8 . v2 v3 1 v4 v5 1 v6 v7 1 v8ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 19
34. 34. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations Pointer Jumping parent(vi ) ← parent(parent(vi )) v0 v1 1 Find the root of v8 . v2 2 v3 1 v4 v5 1 v6 2 v7 1 v8ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 19
35. 35. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations Pointer Jumping parent(vi ) ← parent(parent(vi )) v0 v1 1 Find the root of v8 . v2 2 v3 1 Time Complexity v4 3 Sequential: O(n) v5 1 Parallel: O(log n) v6 2 v7 1 v8ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 19
36. 36. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations Parallel Preﬁx Computation for Linked Lists Step 1: forall i do in parallel next(i) ← succ(i) Input & Output Step 2: ﬁnished ← false Input: A linked list Step 3: while not ﬁnished do Output: Preﬁx computation (3.1) ﬁnished ← true (3.2) forall i do in parallel (i) if next(i) = nil then (a) val(next(i)) ← val(i) ◦ val(next(i)) (b) next(i) ← next(next(i)) (ii) if next(i) = nil then common ﬁnished ← − − false − −− x0 x1 x2 x3 x4 x5 x6 x7ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 20
37. 37. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations Parallel Preﬁx Computation for Linked Lists Step 1: forall i do in parallel next(i) ← succ(i) Notation Step 2: ﬁnished ← false xa-b = xa ◦ xa+1 ◦ · · · ◦ xb Step 3: while not ﬁnished do (3.1) ﬁnished ← true (3.2) forall i do in parallel (i) if next(i) = nil then (a) val(next(i)) ← val(i) ◦ val(next(i)) (b) next(i) ← next(next(i)) (ii) if next(i) = nil then common ﬁnished ← − − false − −− x0 x1 x2 x3 x4 x5 x6 x7 x0 x0-1 x1-2 x2-3 x3-4 x4-5 x5-6 x6-7ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 20
38. 38. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations Parallel Preﬁx Computation for Linked Lists Step 1: forall i do in parallel next(i) ← succ(i) Step 2: ﬁnished ← false Step 3: while not ﬁnished do (3.1) ﬁnished ← true (3.2) forall i do in parallel (i) if next(i) = nil then (a) val(next(i)) ← val(i) ◦ val(next(i)) (b) next(i) ← next(next(i)) (ii) if next(i) = nil then common ﬁnished ← − − false − −− x0 x1 x2 x3 x4 x5 x6 x7 x0 x0-1 x0-2 x0-3 x1-4 x2-5 x3-6 x4-7ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 20
39. 39. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations Parallel Preﬁx Computation for Linked Lists Step 1: forall i do in parallel next(i) ← succ(i) Complexity Step 2: ﬁnished ← false t(n) = O(log n) Step 3: while not ﬁnished do (3.1) ﬁnished ← true p(n) = n (3.2) forall i do in parallel (i) if next(i) = nil then (a) val(next(i)) ← val(i) ◦ val(next(i)) (b) next(i) ← next(next(i)) (ii) if next(i) = nil then common ﬁnished ← − − false − −− x0 x1 x2 x3 x4 x5 x6 x7 x0 x0-1 x0-2 x0-3 x0-4 x0-5 x0-6 x0-7ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 20
40. 40. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations Parallel Suﬃx Computation for Linked Lists Step 1: forall i do in parallel next(i) ← succ(i) Complexity Step 2: ﬁnished ← false t(n) = O(log n) Step 3: while not ﬁnished do (3.1) ﬁnished ← true p(n) = n (3.2) forall i do in parallel (i) if next(i) = nil then (a) val(i) ← val(i) ◦ val(next(i)) (b) next(i) ← next(next(i)) (ii) if next(i) = nil then common ﬁnished ← − − false − −− x0 x1 x2 x3 x4 x5 x6 x7 x0-7 x1-7 x2-7 x3-7 x4-7 x5-7 x6-7 x7ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 21
41. 41. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations List Sequencing List Sequencing Computing the distance of each node from the beginning of the list (node position). Input: Linked list L Output: Sequence numbers forall i do in parallel 1 1 1 1 1 1 1 1 val(i) ← 1 Parallel-Preﬁx(L, +)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 22
42. 42. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations List Sequencing List Sequencing Computing the distance of each node from the beginning of the list (node position). Input: Linked list L Output: Sequence numbers forall i do in parallel 1 1 1 1 1 1 1 1 val(i) ← 1 1 2 3 4 5 6 7 8 Parallel-Preﬁx(L, +)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 22
43. 43. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations List Ranking List Ranking Computing the distance of each node from the end of the list. Input: Linked list L Output: Node ranks forall i do in parallel 1 1 1 1 1 1 1 1 (3.1) val(i) ← 1 (3.2) if succ(i) = nil then succ(succ(i)) ← i Parallel-Preﬁx(L, +)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 23
44. 44. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations List Ranking List Ranking Computing the distance of each node from the end of the list. Input: Linked list L Output: Node ranks forall i do in parallel 1 1 1 1 1 1 1 1 (3.1) val(i) ← 1 (3.2) if succ(i) = nil then succ(succ(i)) ← i Parallel-Preﬁx(L, +)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 23
45. 45. Introduction Building Euler Tours Applications of Euler Tours Conclusion Utility Computations List Ranking List Ranking Computing the distance of each node from the end of the list. Input: Linked list L Output: Node ranks forall i do in parallel 1 1 1 1 1 1 1 1 (3.1) val(i) ← 1 8 7 6 5 4 3 2 1 (3.2) if succ(i) = nil then succ(succ(i)) ← i Complexity Parallel-Preﬁx(L, +) t(n) = O(log n), p(n) = O(n)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 23
46. 46. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Outline 1 Introduction 2 Building Euler Tours 3 Applications of Euler Tours Utility Computations Simple Applications of Euler Tours Computing Minima 4 ConclusionECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 24
47. 47. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours An Euler Tour is a . . . 1 4 v3 v1 v4 2 3 5 12 v2 v5 9 8 7 10 11 6 v7 v6 An Euler Tour returns to the root of a subtree only after all vertices in the subtree have been visited.ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 25
48. 48. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Depth-First Traversal 1 4 v3 v1 v4 2 3 5 12 v2 v5 9 8 7 10 11 6 v7 v6 An Euler Tour returns to the root of a subtree only after all vertices in the subtree have been visited.ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 25
49. 49. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Finding Parents Recall: ´ pos(i) is node i’s sequence number. ´ In an Euler Tour, a node looks like (vi , vj ). ´ If (vi , vj ) is a node, then (vj , vi ) is also a node. pos(vi , vj ) < pos(vj , vi ) ⇒ (vi , vj ) is an advance edge otherwise (vi , vj ) is a retreat edge. 1 4 v3 v1 v4 parent(root) ← nil 2 3 forall (vi , vj ) ∈ ET do in parallel 5 12 if pos(vi , vj ) < pos(vj , vi ) then v2 v5 9 8 7 parent(vj ) ← vi 10 11 6 v7 v6ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 26
50. 50. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Counting Descendants des(vi ) The subtree of vi edges appear between the advance edge (parent(vi ), vi ) and the retreat edge (vi , parent(vi )) in ET. Half of these edges are advance edges and half are retreat. des(vi ) is the number of advance edges in the subtree + 1. pos(vi , parent(vi )) − 1 − pos(parent(vi ), vi ) des(vi ) = +1 2 1 4 (v1 , v3 ) (v3 , v1 ) (v1 , v4 ) (v4 , v1 ) v3 v1 v4 2 3 5 12 (v1 , v5 ) (v5 , v6 ) (v6 , v5 ) (v5 , v7 ) v2 v5 9 8 7 10 11 6 (v7 , v2 ) (v2 , v7 ) (v7 , v5 ) (v5 , v1 ) v7 v6ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 27
51. 51. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Numbering Vertices Preorder (top-down) The preorder number of a vertex vj is the number of advance edges in ET traversed before reaching vj for the ﬁrst time + 1. preorder(root) ← 1 forall (vi , vj ) ∈ ET do in parallel if pos(vi , vj ) < pos(vj , vi ) then 1 4 v3 v1 v4 val(ij) ← 1 2 3 5 12 else val(ij) ← 0 v2 v5 9 8 7 Parallel-Preﬁx(ET, +) 10 11 6 v7 v6 forall (vi , vj ) ∈ ET do in parallel if pos(vi , vj ) < pos(vj , vi ) then preorder(vj ) ← val(ij) + 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 28
52. 52. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Numbering Vertices Preorder (top-down) The preorder number of a vertex vj is the number of advance edges in ET traversed before reaching vj for the ﬁrst time + 1. preorder(root) ← 1 1, 3 3, 1 1, 4 4, 1 forall (vi , vj ) ∈ ET do in parallel 1 0 1 0 if pos(vi , vj ) < pos(vj , vi ) then val(ij) ← 1 else 1, 5 5, 6 6, 5 5, 7 1 1 0 1 val(ij) ← 0 Parallel-Preﬁx(ET, +) 7, 2 2, 7 7, 5 5, 1 forall (vi , vj ) ∈ ET do in parallel 1 0 0 0 if pos(vi , vj ) < pos(vj , vi ) then preorder(vj ) ← val(ij) + 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 28
53. 53. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Numbering Vertices Preorder (top-down) The preorder number of a vertex vj is the number of advance edges in ET traversed before reaching vj for the ﬁrst time + 1. preorder(root) ← 1 1, 3 3, 1 1, 4 4, 1 forall (vi , vj ) ∈ ET do in parallel 1 0 1 0 if pos(vi , vj ) < pos(vj , vi ) then 1 1 2 2 val(ij) ← 1 else 1, 5 5, 6 6, 5 5, 7 1 1 0 1 val(ij) ← 0 3 4 4 5 Parallel-Preﬁx(ET, +) 7, 2 2, 7 7, 5 5, 1 forall (vi , vj ) ∈ ET do in parallel 1 0 0 0 if pos(vi , vj ) < pos(vj , vi ) then 6 6 6 6 preorder(vj ) ← val(ij) + 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 28
54. 54. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Numbering Vertices Preorder (top-down) The preorder number of a vertex vj is the number of advance edges in ET traversed before reaching vj for the ﬁrst time + 1. preorder(root) ← 1 1, 3 3, 1 1, 4 4, 1 forall (vi , vj ) ∈ ET do in parallel 1 0 1 0 if pos(vi , vj ) < pos(vj , vi ) then 2 3 val(ij) ← 1 else 1, 5 5, 6 6, 5 5, 7 1 1 0 1 val(ij) ← 0 4 5 6 Parallel-Preﬁx(ET, +) 7, 2 2, 7 7, 5 5, 1 forall (vi , vj ) ∈ ET do in parallel 1 0 0 0 if pos(vi , vj ) < pos(vj , vi ) then 7 preorder(vj ) ← val(ij) + 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 28
55. 55. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Numbering Vertices Postorder (bottom-up) The postorder number of a vertex vi is the number of retreat edges in ET traversed before reaching vi for the last time + 1. postorder(root) ← n forall (vi , vj ) ∈ ET do in parallel if pos(vi , vj ) < pos(vj , vi ) then 1 4 v3 v1 v4 val(ij) ← 0 2 3 5 12 else val(ij) ← 1 v2 v5 9 8 7 Parallel-Preﬁx(ET, +) 10 11 6 v7 v6 forall (vi , vj ) ∈ ET do in parallel if pos(vi , vj ) > pos(vj , vi ) then postorder(vi ) ← val(ij)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 29
56. 56. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Numbering Vertices Postorder (bottom-up) The postorder number of a vertex vi is the number of retreat edges in ET traversed before reaching vi for the last time + 1. postorder(root) ← n 1, 3 3, 1 1, 4 4, 1 forall (vi , vj ) ∈ ET do in parallel 0 1 0 1 if pos(vi , vj ) < pos(vj , vi ) then val(ij) ← 0 1, 5 5, 6 6, 5 5, 7 else 0 0 1 0 val(ij) ← 1 Parallel-Preﬁx(ET, +) 7, 2 2, 7 7, 5 5, 1 forall (vi , vj ) ∈ ET do in parallel 0 1 1 1 if pos(vi , vj ) > pos(vj , vi ) then postorder(vi ) ← val(ij)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 29
57. 57. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Numbering Vertices Postorder (bottom-up) The postorder number of a vertex vi is the number of retreat edges in ET traversed before reaching vi for the last time + 1. postorder(root) ← n 1, 3 3, 1 1, 4 4, 1 forall (vi , vj ) ∈ ET do in parallel 0 1 0 1 if pos(vi , vj ) < pos(vj , vi ) then 0 1 1 2 val(ij) ← 0 1, 5 5, 6 6, 5 5, 7 else 0 0 1 0 val(ij) ← 1 2 2 3 3 Parallel-Preﬁx(ET, +) 7, 2 2, 7 7, 5 5, 1 forall (vi , vj ) ∈ ET do in parallel 0 1 1 1 if pos(vi , vj ) > pos(vj , vi ) then 3 4 5 6 postorder(vi ) ← val(ij)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 29
58. 58. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Numbering Vertices Postorder (bottom-up) The postorder number of a vertex vi is the number of retreat edges in ET traversed before reaching vi for the last time + 1. postorder(root) ← n 1, 3 3, 1 1, 4 4, 1 forall (vi , vj ) ∈ ET do in parallel 0 1 0 1 if pos(vi , vj ) < pos(vj , vi ) then 1 2 val(ij) ← 0 1, 5 5, 6 6, 5 5, 7 else 0 0 1 0 val(ij) ← 1 3 Parallel-Preﬁx(ET, +) 7, 2 2, 7 7, 5 5, 1 forall (vi , vj ) ∈ ET do in parallel 0 1 1 1 if pos(vi , vj ) > pos(vj , vi ) then 4 5 6 postorder(vi ) ← val(ij)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 29
59. 59. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Tree Binary Relations “An Euler Tour returns to the root of a subtree only after all vertices in the subtree have been visited.” vi is an ancestor of vj (vj is a descendant of vi ) iﬀ: preorder(vi ) ≤ preorder(vj ) < preorder(vi ) + des(vi ) 2 1 3 Examples: v3 v1 v4 v7 is an ancestor of v2 : 7 6≤7<6+2=8 4 v2 v5 v6 is not an ancestor of v2 : v7 v6 5≤7 5+1=6 6 5ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 30
60. 60. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Levels of Vertices level(vj ) = retreat edges − advance edges, following ﬁrst vj in ET level(root) ← 0 forall (vi , vj ) ∈ ET do in parallel 1, 3 3, 1 1, 4 4, 1 if pos(vi , vj ) < pos(vj , vi ) then −1 1 −1 1 val(ij) ← −1 else 1, 5 5, 6 6, 5 5, 7 val(ij) ← 1 −1 −1 1 −1 Parallel-Suﬃx(ET, +) forall (vi , vj ) ∈ ET do in parallel 7, 2 2, 7 7, 5 5, 1 if pos(vi , vj ) < pos(vj , vi ) then −1 1 1 1 level(vj ) ← val(ij) + 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 31
61. 61. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Levels of Vertices level(vj ) = retreat edges − advance edges, following ﬁrst vj in ET level(root) ← 0 forall (vi , vj ) ∈ ET do in parallel 1, 3 3, 1 1, 4 4, 1 if pos(vi , vj ) < pos(vj , vi ) then −1 1 −1 1 0 1 0 1 val(ij) ← −1 else 1, 5 5, 6 6, 5 5, 7 val(ij) ← 1 −1 −1 1 −1 0 1 2 1 Parallel-Suﬃx(ET, +) forall (vi , vj ) ∈ ET do in parallel 7, 2 2, 7 7, 5 5, 1 if pos(vi , vj ) < pos(vj , vi ) then −1 1 1 1 2 3 2 1 level(vj ) ← val(ij) + 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 31
62. 62. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Levels of Vertices level(vj ) = retreat edges − advance edges, following ﬁrst vj in ET level(root) ← 0 forall (vi , vj ) ∈ ET do in parallel 1, 3 3, 1 1, 4 4, 1 if pos(vi , vj ) < pos(vj , vi ) then −1 1 −1 1 1 1 val(ij) ← −1 else 1, 5 5, 6 6, 5 5, 7 val(ij) ← 1 −1 −1 1 −1 1 2 2 Parallel-Suﬃx(ET, +) forall (vi , vj ) ∈ ET do in parallel 7, 2 2, 7 7, 5 5, 1 if pos(vi , vj ) < pos(vj , vi ) then −1 1 1 1 3 level(vj ) ← val(ij) + 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 31
63. 63. Introduction Building Euler Tours Applications of Euler Tours Conclusion Simple Applications of Euler Tours Levels of Vertices level(vj ) = retreat edges − advance edges, following ﬁrst vj in ET = advance edges − retreat edges, up to ﬁrst vj in ET level(root) ← 0 forall (vi , vj ) ∈ ET do in parallel 1, 3 3, 1 1, 4 4, 1 if pos(vi , vj ) < pos(vj , vi ) then −1 1 −1 1 1 1 val(ij) ← −1 else 1, 5 5, 6 6, 5 5, 7 val(ij) ← 1 −1 −1 1 −1 1 2 2 Parallel-Suﬃx(ET, +) forall (vi , vj ) ∈ ET do in parallel 7, 2 2, 7 7, 5 5, 1 if pos(vi , vj ) < pos(vj , vi ) then −1 1 1 1 3 level(vj ) ← val(ij) + 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 31
64. 64. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Outline 1 Introduction 2 Building Euler Tours 3 Applications of Euler Tours Utility Computations Simple Applications of Euler Tours Computing Minima 4 ConclusionECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 32
65. 65. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima The Interval Minima Problem S = {sk : k = preorder(vi ) ⇒ sk = val(vi )} 1 v1 10 Example: 2 7 8 v2 v3 v4 3 = preorder(v5 ), s3 = 6 5 2 1 3 5 S = {10, 5, 6, 7, 3, 4, 2, 1} v5 v6 6 3 4 6 v7 v8 7 4ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 33
66. 66. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima The Interval Minima Problem S = {sk : k = preorder(vi ) ⇒ sk = val(vi )} Svi = {sk , sk+1 , . . . , s }, = k + des(vi ) − 1 1 v1 10 Example: 2 7 8 v2 v3 v4 3 = preorder(v5 ), s3 = 6 5 2 1 3 5 S = {10, 5, 6, 7, 3, 4, 2, 1} v5 v6 6 3 Sv2 : k = 2, = 2 + 5 − 1 = 6 4 6 Sv2 = {5, 6, 7, 3, 4} v7 v8 7 4ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 33
67. 67. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima The Interval Minima Problem S = {sk : k = preorder(vi ) ⇒ sk = val(vi )} Svi = {sk , sk+1 , . . . , s }, = k + des(vi ) − 1 ﬁnd min(Svi ) ∀ vi 1 v1 10 Example: 2 7 8 v2 v3 v4 3 = preorder(v5 ), s3 = 6 5 2 1 3 5 S = {10, 5, 6, 7, 3, 4, 2, 1} v5 v6 6 3 Sv2 : k = 2, = 2 + 5 − 1 = 6 4 6 Sv2 = {5, 6, 7, 3, 4} v7 v8 7 4ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 33
68. 68. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima The Interval Minima Tree Preﬁx Minima (PM) The preﬁx minima of a sequence {a1 , a2 , . . . , an } are given by a sequence {b1 , b2 , . . . , bn } where bi = min(a1 , a2 , . . . , ai ). Suﬃx Minima (SM) The suﬃx minima of a sequence {a1 , a2 , . . . , an } are given by a sequence {d1 , d2 , . . . , dn } where di = min(ai , ai+1 , . . . , an }. PM 2 1 1 1 1 1 Seq. 2 1 3 4 5 6 SM 1 1 3 4 5 6ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 34
69. 69. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima The Interval Minima Tree Preﬁx Minima (PM) The preﬁx minima of a sequence {a1 , a2 , . . . , an } are given by a sequence {b1 , b2 , . . . , bn } where bi = min(a1 , a2 , . . . , ai ). Suﬃx Minima (SM) The suﬃx minima of a sequence {a1 , a2 , . . . , an } are given by a sequence {d1 , d2 , . . . , dn } where di = min(ai , ai+1 , . . . , an }. A complete binary tree such that: Leaves are s1 , s2 , . . . , sn . Each internal node x contains two sequences: the preﬁx minima (PM) and suﬃx minima (SM) of the values held by leaves in the subtree rooted at x.ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 34
70. 70. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Constructing the Tree PM = SM = 1 v1 PM = PM = 10 SM = SM = 2 7 8 v2 v3 v4 5 2 1 PM = PM = PM = PM = 3 5 v5 v6 SM = SM = SM = SM = 6 3 1 2 3 4 5 6 7 8 4 6 v1 v2 v5 v7 v6 v8 v3 v4 v7 v8 10 5 6 7 3 4 2 1 7 4ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 35
71. 71. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Constructing the Tree PM = SM = PM: L {min(lp , ri )} PM = PM = SM: {min(r1 , li )} R SM = SM = PM = 10, PM = 6, PM = 3, PM = 2, L R l1 , l2 , . . . , lp r1 , r2 , . . . , rp SM = ,5 SM = ,7 SM = ,4 SM = ,1 1 2 3 4 5 6 7 8 v1 v2 v5 v7 v6 v8 v3 v4 10 5 6 7 3 4 2 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 35
72. 72. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Constructing the Tree PM = SM = PM: L {min(lp , ri )} PM = PM = SM: {min(r1 , li )} R SM = SM = PM = 10,5 PM = 6,6 PM = 3,3 PM = 2,1 L R l1 , l2 , . . . , lp r1 , r2 , . . . , rp SM = 5,5 SM = 6,7 SM = 3,4 SM = 1,1 1 2 3 4 5 6 7 8 v1 v2 v5 v7 v6 v8 v3 v4 10 5 6 7 3 4 2 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 35
73. 73. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Constructing the Tree PM = SM = PM: L {min(lp , ri )} PM = 10,5, PM = 3,3, SM: {min(r1 , li )} R SM = ,6,7 SM = ,1,1 PM = 10,5 PM = 6,6 PM = 3,3 PM = 2,1 L R l1 , l2 , . . . , lp r1 , r2 , . . . , rp SM = 5,5 SM = 6,7 SM = 3,4 SM = 1,1 1 2 3 4 5 6 7 8 v1 v2 v5 v7 v6 v8 v3 v4 10 5 6 7 3 4 2 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 35
74. 74. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Constructing the Tree PM = SM = PM: L {min(lp , ri )} PM = 10,5,5,5 PM = 3,3,2,1 SM: {min(r1 , li )} R SM = 5,5,6,7 SM = 1,1,1,1 PM = 10,5 PM = 6,6 PM = 3,3 PM = 2,1 L R l1 , l2 , . . . , lp r1 , r2 , . . . , rp SM = 5,5 SM = 6,7 SM = 3,4 SM = 1,1 1 2 3 4 5 6 7 8 v1 v2 v5 v7 v6 v8 v3 v4 10 5 6 7 3 4 2 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 35
75. 75. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Constructing the Tree PM = 10,5,5,5, SM = ,1,1,1,1 PM: L {min(lp , ri )} PM = 10,5,5,5 PM = 3,3,2,1 SM: {min(r1 , li )} R SM = 5,5,6,7 SM = 1,1,1,1 PM = 10,5 PM = 6,6 PM = 3,3 PM = 2,1 L R l1 , l2 , . . . , lp r1 , r2 , . . . , rp SM = 5,5 SM = 6,7 SM = 3,4 SM = 1,1 1 2 3 4 5 6 7 8 v1 v2 v5 v7 v6 v8 v3 v4 10 5 6 7 3 4 2 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 35
76. 76. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Constructing the Tree PM = 10,5,5,5,3,3,2,1 SM = 1,1,1,1,1,1,1,1 PM: L {min(lp , ri )} PM = 10,5,5,5 PM = 3,3,2,1 SM: {min(r1 , li )} R SM = 5,5,6,7 SM = 1,1,1,1 PM = 10,5 PM = 6,6 PM = 3,3 PM = 2,1 L R l1 , l2 , . . . , lp r1 , r2 , . . . , rp SM = 5,5 SM = 6,7 SM = 3,4 SM = 1,1 1 2 3 4 5 6 7 8 v1 v2 v5 v7 v6 v8 v3 v4 10 5 6 7 3 4 2 1ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 35
77. 77. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Finding the Minima Using the Tree An Example: Sv2 k = 2, =k+5−1=6 ⇒ Sv2 = {s2 , . . . , s6 } PM = 10,5,5,5,3,3,2,1 SM = 1,1,1,1,1,1,1,1 1 v1 PM = 10,5,5,5 PM = 3,3,2,1 10 SM = 5,5,6,7 SM = 1,1,1,1 2 7 8 v2 v3 v4 5 2 1 PM = 10,5 PM = 6,6 PM = 3,3 PM = 2,1 3 5 v5 v6 SM = 5,5 SM = 6,7 SM = 3,4 SM = 1,1 6 3 1 2 3 4 5 6 7 8 4 6 v1 v2 v5 v7 v6 v8 v3 v4 v7 v8 10 5 6 7 3 4 2 1 7 4ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 36
78. 78. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Finding the Minima Using the Tree An Example: Sv2 k = 2, =k+5−1=6 ⇒ Sv2 = {s2 , . . . , s6 } PM = 10,5,5,5,3,3,2,1 w SM = 1,1,1,1,1,1,1,1 1 v1 PM = 10,5,5,5 PM = 3,3,2,1 10 SM = 5,5,6,7 SM = 1,1,1,1 2 7 8 v2 v3 v4 5 2 1 PM = 10,5 PM = 6,6 PM = 3,3 PM = 2,1 3 5 v5 v6 SM = 5,5 SM = 6,7 SM = 3,4 SM = 1,1 6 3 1 2 3 4 5 6 7 8 4 6 v1 v2 v5 v7 v6 v8 v3 v4 v7 v8 10 5 6 7 3 4 2 1 7 4ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 36
79. 79. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima Finding the Minima Using the Tree An Example: Sv2 k = 2, =k+5−1=6 ⇒ Sv2 = {s2 , . . . , s6 } PM = 10,5,5,5,3,3,2,1 w SM = 1,1,1,1,1,1,1,1 1 v1 PM = 10,5,5,5 PM = 3,3,2,1 10 x y SM = 5,5,6,7 SM = 1,1,1,1 2 7 8 v2 v3 v4 5 2 1 PM = 10,5 PM = 6,6 PM = 3,3 PM = 2,1 3 5 v5 v6 SM = 5,5 SM = 6,7 SM = 3,4 SM = 1,1 6 3 1 2 3 4 5 6 7 8 4 6 v1 v2 v5 v7 v6 v8 v3 v4 v7 v8 10 5 6 7 3 4 2 1 7 4ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 36
80. 80. Introduction Building Euler Tours Applications of Euler Tours Conclusion Computing Minima The Interval Minima Algorithm Input: A tree T Output: min(Svi ) for all vi ∈ T O(1) ET ← Parallel-Euler-Tour(T ) O(log n) Parallel-Preorder(ET) O(log n) Parallel-Descendants(ET) O(log n) T ← Interval-Minima-Tree(T ) forall vi ∈ T do in parallel O(1) k ← preorder(vi ), ← k + des(vi ) − 1 O(log n) w ← Lowest-Common-Ancestor(sk , s ) in T O(1) x ← Left-Child(w), y ← Right-Child(w); (x) (y) O(1) min(Svi ) ← min(SMk , PM ) Complexity: t(n) = O(log n) p(n) = O(n) ⇒ c(n) = n log(n)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 37
81. 81. Introduction Building Euler Tours Applications of Euler Tours Conclusion Summary Outline 1 Introduction 2 Building Euler Tours 3 Applications of Euler Tours 4 Conclusion Summary QuestionsECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 38
82. 82. Introduction Building Euler Tours Applications of Euler Tours Conclusion Summary Summary Levels Parent Des IntMin O(log n) O(1) O(1) O(log n) DFT ET Pos Bin O(1) O(1) O(log n) O(1) Rank Preﬁx Preorder O(log n) O(log n) O(log n)ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 39
83. 83. Introduction Building Euler Tours Applications of Euler Tours Conclusion Summary Thank You Thank You!ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 40