Parallel Algorithms for Trees Ahmad Khayyat Department of Electrical & Computer Engineering [email protected]Parallel Computation: Models and Methods Selim G. Akl Chapter 6 CISC 879 — Algorithms and Applications Queen’s University October 29, 2008 ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 1
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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Parallel Algorithms for Trees
Ahmad KhayyatDepartment of Electrical & Computer Engineering
Parallel Computation: Models and MethodsSelim G. Akl Chapter 6
CISC 879 — Algorithms and ApplicationsQueen’s University
October 29, 2008
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 1
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Outline
1 Introduction
2 Building Euler Tours
3 Applications of Euler Tours
4 Conclusion
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 2
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Graphs
Outline
1 IntroductionGraphsEuler Tours
2 Building Euler Tours
3 Applications of Euler Tours
4 Conclusion
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 3
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 setof 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 numberof edges adjacent to the vertex.
The in-degree of a vertex v in a directed graph is the numberof edges entering v,The out-degree of a vertex v in a directed graph is thenumber of edges leaving v.
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 4
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Euler Tours
Outline
1 IntroductionGraphsEuler Tours
2 Building Euler Tours
3 Applications of Euler Tours
4 Conclusion
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 5
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 exactlyonce.
An undirected graph has an Euler tour if each vertex has an evendegree.
(v1, v7), (v7, v2), (v2, v3), (v3, v6)
(v6, v7), (v7, v5), (v5, v6)
(v6, v4), (v4, v5), (v5, v1)
v1
v5
v7
v4 v6 v3
v2
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 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 exactlyonce.
An undirected graph has an Euler tour if each vertex has an evendegree.
(v1, v7), (v7, v2), (v2, v3), (v3, v6)
(v6, v7), (v7, v5), (v5, v6)
(v6, v4), (v4, v5), (v5, v1)
v1
v5
v7
v4 v6 v3
v2
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 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 exactlyonce.
An undirected graph has an Euler tour if each vertex has an evendegree.
(v1, v7), (v7, v2), (v2, v3), (v3, v6)
(v6, v7), (v7, v5), (v5, v6)
(v6, v4), (v4, v5), (v5, v1)
v1
v5
v7
v4 v6 v3
v2
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 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 exactlyonce.
An undirected graph has an Euler tour if each vertex has an evendegree.
(v1, v7), (v7, v2), (v2, v3), (v3, v6)
(v6, v7), (v7, v5), (v5, v6)
(v6, v4), (v4, v5), (v5, v1) v1
v5
v7
v4 v6 v3
v2
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 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 exactlyonce.
A directed graph has an Euler tour if the in-degree of each vertexis equal to the out-degree of the vertex.
(v1, v2), (v2, v3), (v3, v4)
(v4, v2), (v2, v4)
(v4, v5), (v5, v6), (v6, v4), (v4, v6)
(v6, v5), (v5, v1)
v1 v2
v3 v4
v5 v6
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 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 exactlyonce.
A directed graph has an Euler tour if the in-degree of each vertexis equal to the out-degree of the vertex.
(v1, v2), (v2, v3), (v3, v4)
(v4, v2), (v2, v4)
(v4, v5), (v5, v6), (v6, v4), (v4, v6)
(v6, v5), (v5, v1)
v1 v2
v3 v4
v5 v6
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 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 exactlyonce.
A directed graph has an Euler tour if the in-degree of each vertexis equal to the out-degree of the vertex.
(v1, v2), (v2, v3), (v3, v4)
(v4, v2), (v2, v4)
(v4, v5), (v5, v6), (v6, v4), (v4, v6)
(v6, v5), (v5, v1)
v1 v2
v3 v4
v5 v6
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 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 exactlyonce.
A directed graph has an Euler tour if the in-degree of each vertexis equal to the out-degree of the vertex.
(v1, v2), (v2, v3), (v3, v4)
(v4, v2), (v2, v4)
(v4, v5), (v5, v6), (v6, v4), (v4, v6)
(v6, v5), (v5, v1)
v1 v2
v3 v4
v5 v6
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 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 exactlyonce.
A directed graph has an Euler tour if the in-degree of each vertexis equal to the out-degree of the vertex.
(v1, v2), (v2, v3), (v3, v4)
(v4, v2), (v2, v4)
(v4, v5), (v5, v6), (v6, v4), (v4, v6)
(v6, v5), (v5, v1)
v1 v2
v3 v4
v5 v6
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 6
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.
v1v3 v4
v5
v6v7
v2
v1v3 v4
v5
v6v7
v2
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 7
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Data Structure
Outline
1 Introduction
2 Building Euler ToursData StructureThe AlgorithmAnalysis
3 Applications of Euler Tours
4 Conclusion
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 8
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.
(v1, v3)head(v1) (v1, v4) (v1, v5)
(v2, v7)head(v2)
(v3, v1)head(v3)
(v4, v1)head(v4)
(v5, v1)head(v5) (v5, v6) (v5, v7)
(v6, v5)head(v6)
(v7, v2)head(v7) (v7, v5)
v1v3 v4
v5
v6v7
v2
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 9
Introduction Building Euler Tours Applications of Euler Tours Conclusion
The Algorithm
Outline
1 Introduction
2 Building Euler ToursData StructureThe AlgorithmAnalysis
3 Applications of Euler Tours
4 Conclusion
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 10
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 first edge leaves some vertex vl, the last edge enters vl.
(v1, v3) (v3, v1) (v1, v4) (v4, v1)
(v1, v5) (v5, v6) (v6, v5) (v5, v7)
(v7, v2) (v2, v7) (v7, v5) (v5, v1)
v1v3 v4
v5
v6v7
v2
1
2 3
4
5 12
6
78
11
9
10
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 11
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 thetwo nodes holding (vi, vj) and (vj , vi) (or ij and ji) — in ET.
Successor of (vi, vj)
if next(ji) = jk thensucc(ij) ← jk
elsesucc(ij) ← head(vj)
end if
Successor of (vj , vi)
if next(ij) = im thensucc(ji) ← im
elsesucc(ji) ← head(vi)
end if
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 12
Introduction Building Euler Tours Applications of Euler Tours Conclusion
The Algorithm
Example: Pij = P13Successor of (v1, v3)
Successor of (vi, vj)
if next(ji) = jk thensucc(ij) ← jk
elsesucc(ij) ← head(vj)
end if
(v1, v3) (v3, v1)
v1v3 v4
v5
v6v7
v2
(v1, v3)head(v1) (v1, v4) (v1, v5)...
(v3, v1)head(v3)
31
13
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 13
Introduction Building Euler Tours Applications of Euler Tours Conclusion
The Algorithm
Example: Pij = P13Successor of (v1, v3)
Successor of (vi, vj)
if next(ji) = jk thensucc(ij) ← jk
elsesucc(ij) ← head(vj)
end if
next(31) = null
(v1, v3) (v3, v1)
v1v3 v4
v5
v6v7
v2
(v1, v3)head(v1) (v1, v4) (v1, v5)...
(v3, v1)head(v3)
31
13
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 13
Introduction Building Euler Tours Applications of Euler Tours Conclusion
The Algorithm
Example: Pij = P13Successor of (v1, v3)
Successor of (vi, vj)
if next(ji) = jk thensucc(ij) ← jk
elsesucc(ij) ← head(vj)
end if
succ(13) ← head(v3)
(v1, v3) (v3, v1)
v1v3 v4
v5
v6v7
v2
(v1, v3)head(v1) (v1, v4) (v1, v5)...
(v3, v1)head(v3)
31
13
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 13
Introduction Building Euler Tours Applications of Euler Tours Conclusion
The Algorithm
Example: Pij = P13Successor of (v3, v1)
Successor of (vj , vi)
if next(ij) = im thensucc(ji) ← im
elsesucc(ji) ← head(vi)
end if
(v1, v3) (v3, v1) (v1, v4)
v1v3 v4
v5
v6v7
v2
(v1, v3)head(v1) (v1, v4) (v1, v5)...
(v3, v1)head(v3)
13
31
14
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 14
Introduction Building Euler Tours Applications of Euler Tours Conclusion
The Algorithm
Example: Pij = P13Successor of (v3, v1)
Successor of (vj , vi)
if next(ij) = im thensucc(ji) ← im
elsesucc(ji) ← head(vi)
end if
next(13) = 14
(v1, v3) (v3, v1) (v1, v4)
v1v3 v4
v5
v6v7
v2
(v1, v3)head(v1) (v1, v4) (v1, v5)...
(v3, v1)head(v3)
13
31
14
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 14
Introduction Building Euler Tours Applications of Euler Tours Conclusion
The Algorithm
Example: Pij = P13Successor of (v3, v1)
Successor of (vj , vi)
if next(ij) = im thensucc(ji) ← im
elsesucc(ji) ← head(vi)
end if
succ(31) ← 14
(v1, v3) (v3, v1) (v1, v4)
v1v3 v4
v5
v6v7
v2
(v1, v3)head(v1) (v1, v4) (v1, v5)...
(v3, v1)head(v3)
13
31
14
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 14
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Analysis
Outline
1 Introduction
2 Building Euler ToursData StructureThe AlgorithmAnalysis
3 Applications of Euler Tours
4 Conclusion
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 15
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)
(vj , vi)head(vj) (vj , vk)
next(ji) = jk
next(jk) = ji, or head(vj) = ji
vi vj vk
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 16
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)
(vj , vi)head(vj) (vj , vk)
next(ji) = jk
next(jk) = ji, or head(vj) = ji
vi vj vk
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 16
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
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Utility Computations
Outline
1 Introduction
2 Building Euler Tours
3 Applications of Euler ToursUtility ComputationsSimple Applications of Euler ToursComputing Minima
4 Conclusion
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 18
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Utility Computations
Pointer Jumping parent(vi) ← parent(parent(vi))
Find the root of v8.
Time Complexity
Sequential: O(n)
Parallel: O(log n)
v0
v1
v2
v3
v4
v5
v6
v7
v8
1
1
1
1
1
1
1
1
2
2
1
1
1
1
2
2
3
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 19
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Utility Computations
Pointer Jumping parent(vi) ← parent(parent(vi))
Find the root of v8.
Time Complexity
Sequential: O(n)
Parallel: O(log n)
v0
v1
v2
v3
v4
v5
v6
v7
v8
1
1
1
1
1
1
1
1
2
2
1
1
1
1
2
2
3
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 19
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Utility Computations
Pointer Jumping parent(vi) ← parent(parent(vi))
Find the root of v8.
Time Complexity
Sequential: O(n)
Parallel: O(log n)
v0
v1
v2
v3
v4
v5
v6
v7
v8
1
1
1
1
1
1
1
1
2
2
1
1
1
1
2
2
3
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 19
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Utility Computations
Pointer Jumping parent(vi) ← parent(parent(vi))
Find the root of v8.
Time Complexity
Sequential: O(n)
Parallel: O(log n)
v0
v1
v2
v3
v4
v5
v6
v7
v8
1
1
1
1
1
1
1
1
2
2
1
1
1
1
2
2
3
ECE @ Queen’s • Fall 2008 Ahmad Khayyat Parallel Algorithms for Trees • 19
Introduction Building Euler Tours Applications of Euler Tours Conclusion
Utility Computations
Parallel Prefix Computation for Linked Lists
Step 1: forall i do in parallelnext(i) ← succ(i)
Step 2: finished ← falseStep 3: while not finished do
(3.1) finished ← true(3.2) forall i do in parallel