Sorting. Quick Sort Example 2, 1, 3, 4, 5 2, 4, 3, 1, 5, 5, 6, 9, 7, 8, 5 S={6, 5, 9, 2, 4, 3, 5, 1, 7, 5, 8} 1, 234, 5 5 5, 6, 7, 8, 9.

Slides:



Advertisements
Similar presentations
Optimal PRAM algorithms: Efficiency of concurrent writing “Computer science is no more about computers than astronomy is about telescopes.” Edsger Dijkstra.
Advertisements

Lecture 3: Parallel Algorithm Design
The Divide-and-Conquer Strategy
Introduction to Algorithms
2010/12/12
Simple Linear Work Suffix Array Construction J. Kärkkäinen, P. Sanders Proc. 30th International Conference on Automata, Languages and Programming 2003.
Advanced Topics in Algorithms and Data Structures Lecture pg 1 Recursion.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Advanced Topics in Algorithms and Data Structures Classification of the PRAM model In the PRAM model, processors communicate by reading from and writing.
PRAM Models Advanced Algorithms & Data Structures Lecture Theme 13 Prof. Dr. Th. Ottmann Summer Semester 2006.
Advanced Topics in Algorithms and Data Structures Page 1 Parallel merging through partitioning The partitioning strategy consists of: Breaking up the given.
Simulating a CRCW algorithm with an EREW algorithm Efficient Parallel Algorithms COMP308.
Lecture 8 Median and Order Statistics. Median and Order Statistics2 Order Statistics 問題敘述 在 n 個元素中,找出其中第 i 小的元素。 i = 1 ,即為找最小值。 i = n ,即為找最大值。 i = 或 ,即為找中位數。
: Matrix Decompressing ★★★★☆ 題組: Contest Volumes with Online Judge 題號: 11082: Matrix Decompressing 解題者:蔡權昱、劉洙愷 解題日期: 2008 年 4 月 18 日 題意:假設有一矩陣 R*C,
Sorting Algorithms CS 524 – High-Performance Computing.
1 Friday, November 17, 2006 “In the confrontation between the stream and the rock, the stream always wins, not through strength but by perseverance.” -H.
1 Lecture 11 Sorting Parallel Computing Fall 2008.
Network Connections ★★★☆☆ 題組: Contest Archive with Online Judge 題號: Network Connections 解題者:蔡宗翰 解題日期: 2008 年 10 月 20 日 題意:給你電腦之間互相連線的狀況後,題.
: Abundance and Perfect Numbers ★★★★☆ 題組: Contest Volumes with Online Judge 題號: 10914: Abundance and Perfect Numbers 解題者:劉洙愷 解題日期: 2008 年 5 月 2.
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
: Problem A : MiniMice ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11411: Problem A : MiniMice 解題者:李重儀 解題日期: 2008 年 9 月 3 日 題意:簡單的說,題目中每一隻老鼠有一個編號.
: Ahoy, Pirates! ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11402: Ahoy, Pirates! 解題者:李重儀 解題日期: 2008 年 8 月 26 日 題意:有一個海盜島有 N 個海盜,他們的編號 (id)
: Count DePrimes ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11408: Count DePrimes 解題者:李育賢 解題日期: 2008 年 9 月 2 日 題意: 題目會給你二個數字 a,b( 2 ≦ a ≦ 5,000,000,a.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
Lecture 7 Sorting in Linear Time. Sorting in Linear Time2 7.1 Lower bounds for sorting 本節探討排序所耗用的時間複雜度下限。 任何一個以比較為基礎排序的演算法,排序 n 個元 素時至少耗用 Ω(nlogn) 次比較。
: Problem G e-Coins ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10306: Problem G e-Coins 解題者:陳瀅文 解題日期: 2006 年 5 月 2 日 題意:給定一個正整數 S (0
: Lucky Number ★★★★☆ 題組: Proble Set Archive with Online Judge 題號: 10909: Lucky Number 解題者:李育賢 解題日期: 2008 年 4 月 25 日 題意:給一個奇數數列 1,3,5,7,9,11,13,15…
: GCD - Extreme II ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11426: GCD - Extreme II 解題者:蔡宗翰 解題日期: 2008 年 9 月 19 日 題意: 最多 20,000 組測資,題目會給一個數字.
: Ubiquitous Religions ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10583: Ubiquitous Religions 解題者:吳佳樺 解題日期: 2010 年 3 月 18 日 題意: 一開始給予兩個數字.
: Expect the Expected ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11427: Expect the Expected 解題者:李重儀 解題日期: 2008 年 9 月 21 日 題意:玩一種遊戲 (a game.
2015/6/ : Simple division ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10407: Simple division 解題者:侯沛彣、柯名澤 解題日期: 2006 年 5 月 16 日 題意: 給一個不為零的整數數列,請找出除以每個數字後能.
Sorting Algorithms Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar To accompany the text ``Introduction to Parallel Computing'', Addison Wesley,
:Commandos ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11463: Commandos 解題者:李重儀 解題日期: 2008 年 8 月 11 日 題意: 題目會給你一個敵營區內總共的建築物數,以及建築物 之間可以互通的路有哪些,並給你起點的建築物和終點.
1 高等演算法 -Introduction 1. Analysis 2. Basic arithmetic 3. Modular arithmetic 4. GCD 5. Primality testing 6. Cryptography.
Advanced Topics in Algorithms and Data Structures 1 Two parallel list ranking algorithms An O (log n ) time and O ( n log n ) work list ranking algorithm.
:Count the Trees ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10007:Count the Trees 解題者:楊家豪 解題日期: 2006 年 3 月 題意: 給 n 個點, 每一個點有自己的 Label,
: GCD (I) ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11424: GCD(I) 解題者:李育賢 解題日期: 2008 年 9 月 19 日 題意: 最多 20,000 組測資,題目會給一個數字 N(1
Supply and Demand Curve Cost-Volume-Profit Chart Profit-Volume Chart Economic Order Quantity 基本圖形介紹 95/10/08.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
質數 (Prime) 相關問題 Introducer: Hsing-Yen Ann Date: 2009/05/13.
Basic PRAM algorithms Problem 1. Min of n numbers Problem 2. Computing a position of the first one in the sequence of 0’s and 1’s.
Simulating a CRCW algorithm with an EREW algorithm Lecture 4 Efficient Parallel Algorithms COMP308.
1 Lecture 2: Parallel computational models. 2  Turing machine  RAM (Figure )  Logic circuit model RAM (Random Access Machine) Operations supposed to.
Computer Science and Engineering Parallel and Distributed Processing CSE 8380 February 8, 2005 Session 8.
Parallel Algorithms Sorting and more. Keep hardware in mind When considering ‘parallel’ algorithms, – We have to have an understanding of the hardware.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Introduction 因為需要 large amount of computation. Why parallel?
Outline  introduction  Sorting Networks  Bubble Sort and its Variants 2.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
1 PRAM Algorithms Sums Prefix Sums by Doubling List Ranking.
Computer Science and Engineering Parallel and Distributed Processing CSE 8380 February 3, 2005 Session 7.
1 Algorithms CSCI 235, Fall 2015 Lecture 19 Order Statistics II.
Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
4-3~ 鄭力瑋. concave polygon:convex polygon: Convex polygons 「凸」的定義是:圖形內任意兩點的連線不會經過圖形外部。
Data Structures and Algorithms in Parallel Computing Lecture 8.
+ Even Odd Sort & Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine.
3/12/2013Computer Engg, IIT(BHU)1 PRAM ALGORITHMS-3.
Unit-8 Sorting Algorithms Prepared By:-H.M.PATEL.
3/12/2013Computer Engg, IIT(BHU)1 PRAM ALGORITHMS-1.
Lecture 3: Parallel Algorithm Design
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing Parallel Odd-Even Sort Algorithm Dr. Xiao.
Lecture 2: Parallel computational models
Sorting Quiz questions
Numerical Algorithms Quiz questions
Professor Ioana Banicescu CSE 8843
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Module 6: Introduction to Parallel Computing
CSE 332: Parallel Algorithms
Presentation transcript:

Sorting

Quick Sort Example 2, 1, 3, 4, 5 2, 4, 3, 1, 5, 5, 6, 9, 7, 8, 5 S={6, 5, 9, 2, 4, 3, 5, 1, 7, 5, 8} 1, 234, 5 5 5, 6, 7, 8, 9

Quick Sort Step 1. If n = 1 then return. Step 2. Find the median m of the input array A. Step 3. Use m to partition A into two subsequences B and C. Step 4. Quick sort B. Step 5. Quick sort C. O(1) O(n) t(n/2)

The time complexity of Quick sort t(n)= O(1) + O(n) + O(n) + 2t(n/2) = cn + 2t(n/2) = O(n log n)

Merging Networks (1, 1)-merger (comparator): Sorting Networks

Merging Networks (2, 2)-merger:

Sorting Networks

Running Time s(2) = 1, i = 1 s(2 i ) = s(2 i-1 ) + 1, i > 1. s(2 i ): the time required in the ith stage. There are log n stages in all. s(2 i ) = i

t(n) = s(2 1 ) + s(2 2 ) + … + s(2 log n ) = … + log n = O(log 2 n)

Number of Processors q(2) = 1, i = 1 q(2 i ) = 2q(2 i-1 ) + 2 i-1 - 1, i > 1. q(2 i ): the number of comparators required in the ith stage. q(2 i ) = (i-1)2 i-1 + 1

q(2 i ) = 2q(2 i-1 ) + 2 i-1 – 1 = 2 2 q(2 i-2 ) + 2 i-1 – i-1 – 1 =2 3 q(2 i-3 ) + 2 i-1 – i-1 – i-1 – 2 0 … = 2 i-1 q(2 1 ) + 2 i-1 + … + 2 i-1 – ( … + 2 i-2 ) = 2 i-1 + (i – 1)2 i-1 – 2 i = (i – 1)2 i-1 + 1

p(n) = 2 (logn)-1 q(2 1 ) + 2 (logn)-2 q(2 2 ) + … q(2 log n ) = O(nlog 2 n)

Cost c(n) = p(n) * t(n) = O(nlog 2 n) * O(log 2 n) = O(nlog 4 n)

Sorting on a linear array (odd-even transposition)

Procedure ODD-EVEN TRANSPOSITION (S) for j = 1 to ┌ n/2 ┐ do (1) for i = 1, 3, …, 2 └ n/2 ┘ - 1 do in parallel if x i > x i+1 then x i ←→ x i+1 end if end for (2) for i = 2, 4, …, 2 └ (n-1)/2 ┘ do in parallel if x i > x i+1 then x i ←→ x i+1 end if end for

Time: odd-even steps. = O() Cost:

2 ways for reducing cost: (i) reduce running time (ii) reduce # of processors Reducing running time is hopeless since the lower bound for sorting on a linear array of n processors is..

Sorting on a linear array (odd-even transposition)

N processors are available, N < n. Each processor stores data elements. Stage 1: Sort sequentially in each processor. Stage 2: Odd-even transposition sort. Each comparison-exchange is replaced with a merge-split.. O((n/N)log(n/N)) ┌ N/2 ┐ O(n/N) 用 sequential merge, 每次 O(n/N) 的時間, 共有 N/2 個 steps, 所以總時間為

C(n) = p(n)*t(n) = The algorithm is cost optimal when..

CRCW Sort

Procedure CRCW SORT(S) Step 1. for i = 1 to n do in parallel for j = 1 to n do in parallel if (s i > s j ) or (s i = s j and i > j) then P(i, j) writes 1 in c i else P(i, j) writes 0 in c i end if end for Step 2. for i = 1 to n do in parallel P(i, 1) stores s i in position 1 + c i of S end for O(1)

p(n) = n 2 t(n) = O(1) c(n) = n 2

CREW Sort ( 利用 CREW MERGE) S={2, 8, 5, 10, 15, 1, 12, 6, 14, 3, 11, 7, 9, 4, 13, 16} N = 4 Step 1. P 1 : {2, 8, 5, 10} P 2 : {15, 1, 12, 6} P 3 : {14, 3, 11, 7} P 4 : {9, 4, 13, 16} Step 2. P 1 : {2, 5, 8, 10} P 2 : {1, 6, 12, 15} P 3 : {3, 7, 11, 14} P 4 : {4, 9, 13, 16} Step 3. P 1, P 2 : {1, 2, 5, 6, 8, 10, 12, 15} P 3, P 4 : {3, 4, 7, 9, 11, 13, 14, 16} P 1, P 2, P 3, P 4 : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}

Procedure CREW SORT(S) Step 1. for i = 1 to N do in parallel Processor P i 1.1 reads a distinct subsequence S i of S of size n/N 1.2 QUICKSORT(S i ) 1.3 S(i, 1) ← S i 1.4 P(i, 1)← {P i } O(n/N log n/N)

Step 2. u ← 1 v ← N while v > 1 do for m = 1 to └ v/2 ┘ do in parallel P(u+1, m) ← P(u, 2m-1) ∪ P(u, 2m) The processors in the set P(u+1, m) perform CREW MERGE(S(u, 2m-1), S(u, 2m), S(u+1, m)) end for if v is odd then P(u+1, [v/2]) ← P(u, v) S(u+1, [v/2]) ← S(u, v) end if u ← u + 1 v ← [v/2] end while 每次 O(n/N + log n) 共做 [log N] 次, 總共所花時間為 [log N] * O(n/N + log n)

t(n) = O((n/N) log(n/N)) + O((n/N) + log n) *log N = O((n/N) log n – (n/N) log N) + O((n/N) log N + log n log N) = O((n/N) log n + log n log N) c(n) = p(n) * t(n) = N * O((n/N) log n + log n log N) = O(n log n + N log n log N) The algorithm is cost optimal when N ≦ n/log N.

Sorting on the EREW Model Simulating Procedure CREW Sort: 用 MULTIPLE BROADCAST 來取代 Concurrent Read. 多花 log N 的時間. t(n) = O((n/N)log n + log n log N) * O(log N) = O([(n/N) + log N] log n log N) c(n) = O([(n/N) + log N] log n log N) * N = O((n +N log N) log n log N) which is not cost optimal.

Sorting by Conflict-Free Merging 用 EREW MERGE 取代 CREW MERGE. 每次 EREW MERGE 所花的時間為 O((n/N) + log n log N). 要做 log N 次, 所以總共所花時間為 t(n) = O((n/N) log (n/N)) + O((n/N) + log n log N) * log N = O([(n/N) + log 2 N] log n) c(n) = O((n + N log 2 N) log n) which is cost optimal when N ≦ n/log 2 N.

Parallel Quicksort S={5, 9, 12, 16, 18, 2, 10, 13, 17, 4, 7, 18, 18,1 1, 3, 17, 20, 19, 14, 8, 5, 17, 1, 11, 15, 10, 6} n = 27N = 5 N = n 1-x = 27 1-x x ≒ 0.5 將 n 個 data 分成 2 1/x 塊, 每塊有 n/2 1/x 個 data. 原因稍後解釋 2 1/x = 2 1/0.5 = 4 n/2 1/x = 27/4 ≒ 7 所以 PARALLEL SELECT 第 7, 14, 21 大的數字, 分別另為 m 1, m 2, 和 m 3. m 1 = 6, m 2 = 11, m 3 =17.

S 1 = {5, 2, 4, 3, 5, 1, 6} S 2 = {9, 10, 7, 8, 10, 11, 11} S 3 = {12, 16, 13, 14, 15, 17, 17} S 4 = {18, 18, 18, 20, 19, 17} m 1 = 6, m 2 = 11, m 3 = 17 n = 7 N = n 1-x = ≒ 2 P 1, P 2 : S 1 = {5, 2, 4, 3, 5, 1, 6} P 3, P 4 : S 2 = {9, 10, 7, 8, 10, 11, 11} m 1 = 2, m 2 = 4, m 3 = 5m 1 = 8, m 2 = 10, m 3 = 11 將 n 個 data 分成 2 1/x 塊, 每塊有 n/2 1/x 個 data. 7 42

P 1, P 2 : S = {5, 2, 4, 3, 5, 1, 6} P 3, P 4 : S = {9, 10, 7, 8, 10, 11, 11} m 1 = 2, m 2 = 4, m 3 = 5m 1 = 8, m 2 = 10, m 3 = 11 S 1 = {1, 2} S 2 = {3, 4} S 3 = {5, 5} S 4 = {6} S 1 = {7, 8} S 2 = {9, 10} S 3 = {10, 11} S 4 = {11}

S 1 = {5, 2, 4, 3, 5, 1, 6} S 2 = {9, 10, 7, 8, 10, 11, 11} S 3 = {12, 16, 13, 14, 15, 17, 17} S 4 = {18, 18, 18, 20, 19, 17} m 1 = 6, m 2 = 11, m 3 = 17 n = 7 N = n 1-x = ≒ 2 P 1, P 2 : S 3 = {12, 16, 13, 14, 15, 17, 17} P 3, P 4 : S 4 = {18, 18, 18, 20, 19, 17} m 1 = 13, m 2 = 15, m 3 = 17 m 1 = 18, m 2 = 18, m 3 = 20 將 n 個 data 分成 2 1/x 塊, 每塊有 n/2 1/x 個 data. 7 42

P 1, P 2 : S = {12, 16, 13, 14, 15, 17, 17} P 3, P 4 : S = {18, 18, 18, 20, 19, 17} m 1 = 13, m 2 = 15, m 3 = 17 m 1 = 18, m 2 = 18, m 3 = 20 S 1 = {12, 13} S 2 = {14, 15} S 3 = {16, 17} S 4 = {17} S 1 = {17, 18} S 2 = {18, 18} S 3 = {19, 20} S 4 = { }

procedure EREW SORT (S) if then QUICKSORT (S) else (1) for i=1 to k-1 do PARALLEL SELECT (S, ) {Obtain} (3) for i=2 to k-1 do end for (5) for i=1 to k/2 do in parallel EREW SORT end for (2) (4) (6) for to k do in parallel end for EREW SORT end if

: number of processors,

Why ? elements use processors. elements use processors.

time: which is cost optimal. c(n) = p(n)*t(n) = n 1-x * n x log n = n logn