CSL 730: Parallel Programming

Size: px
Start display at page:

Download "CSL 730: Parallel Programming"

Transcription

1 CSL 73: Parallel Programming

2 General Algorithmic Techniques Balance binary tree Partitioning Divid and conquer Fractional cascading Recursive doubling Symmetry breaking Pipelining 2

3 PARALLEL ALGORITHM TECHNIQUES: BALANCED BINARY TREE

4 Reduction n operands => log n steps Total work = O(n) How do you map? Balance Binary tree technique

5 Reduction n operands => log n steps How do you map? n/2 i processors per step step i: if!(id%2 i )

6 Reduction n operands => log n steps Only have p processors per step Agglomerate and Map

7 Prefix Sums P[] = x[] For i = 1 to n-1 P[i] = P[i-1] + x[i]

8 Recursive Prefix Sums P[] = x[] For i = 1 to n-1 P[i] = P[i-1] + x[i] Prefix Sum

9 Recursive Prefix Sums prefixsums(p, x, (:n]) { parallel for i in (:n/2] y[i] = OP(x[2*i], x[2*i+1]) prefixsum(z, y, (:n/2]) P[] = x[] parallel for i in [1:n] if(i&1) P[i] = z[i/2] else P[i] = OP(z[i/2-1 ], x[i]) } Prefix Sum Or OP -1 (z[i/2], x[i]), if op inver@ble

10 Prefix Sums P[] = x[] For i = 1 to n-1 P[i] = P[i-1] + x[i] S(:n/2] S(n/2:n] S(n/2:3n/4]

11 Prefix Sums P[] = x[] For i = 1 to n-1 P[i] = P[i-1] + x[i] S(:n/2] S(:n/2] S(n/2:n] S(:3n/4]

12 Non-recursive Prefix Sums parallel for i = to n B[][i] = A[i] for h = 1 to log n parallel for i in :n/2 h B[h][i] = B[h-1][2i] OP B[h-1][2i+1] for h = log n to C[h][] = B[h][] parallel for i in 1:n/2 h Odd i: C[h][i] = C[h+1][i/2] Even i: C[h][i] = C[h+1][(i/2-1] OP B[h][i]

13 Prefix Sums: Data flow up B[3][] B[2][] B[2][1] B[1][] B[1][1] B[1][2] B[1][3] B[][] B[][1] B[][2] B[][3] B[][4] B[][5] B[][6] B[][7]

14 Prefix Sums: Data flow down C[3][] = B[3][] C[2][] C[2][1] C[1][] C[1][1] C[1][2] C[1][3] C[][] C[][1] C[][2] C[][3] C[][4] C[][5] C[][6] C[][7] O(n) work, O(log

15 Processor Mapping P P P1 P P P1 P1 P P P P P1 P1 P1 P1

16 Balanced Tree Approach Build binary tree on the input Hierarchically divide into groups and groups of groups.. Traverse tree upwards/downwards Useful to think of tree network topology Only for algorithm design Later map sub-trees to processors 16

17 PARALLEL ALGORITHM TECHNIQUES: PARTITIONING

18 Merge Sorted Sequences (A,B) Determine Rank of each element in A U B Rank(x, A U B) = Rank(x, A) + Rank(x, B) Only need to compute the rank in the other list, if A and B are each sorted already Find Rank(A, B), and similarly Rank(B, A) Find Rank by binary search O(log n) time O(n log n) work 18

19 Towards Optimal Merge (A,B) Partition A and B into log n sized blocks Select from B, elements i * log n, i :n/log n Rank each selected element of B in A Binary search Merge pairs of sub-sequences If A i = log(n), Sequential merge in time O(log(n) ) Otherwise, partition A i into log n blocks And Recursively subdivide B i into sub-sub-sequences

20 Towards Optimal Merge (A,B) Total time is O(log(n)) Total work is O(n) 2

21 Towards Optimal Merge (A,B) Partition A and B into log n sized blocks Select from B, elements i * log n, i = :n/log n Rank each selected element of B in A Binary search Merge pairs of sub-sequences If A i = log(n), Sequential merge in time O(log(n) ) Otherwise, partition A i into log n sized blocks And (similarly) subdivide B i into sub-sub-sequences Complexity: O(log(n)) time O(n) work Should we expect bejer?

22 Fast Merge (A,B) Partition A and B into n blocks Select from B, elements i n, i (: n] Rank each selected element of B in A Parallel search using n processors each search Similarly rank n selected elements from A in B Recursively merge pairs of sub-sequences Total time: T(n) = O(1)+T( n) = O(log log n) Total work: W(n) = O(n)+ n W( n) = O(n log log n) Fast but still need to reduce work Not oplmal yet

23 Optimal Merge (A,B) Use the fast, but non-optimal, algorithm on small enough subsets Subdivide A and B into blocks of size log log n A 1, A 2,.. B 1, B 2,.. Select first element of each block A = p 1, p 2.. B = q 1, q 2.. Now merge loglogn sized blocks n/loglogn times

24 Optimal Merge (A,B) A llg n A n/llg n merge B n/llg n B llg n llg n llg: log log 24

25 Optimal Merge (A,B) A llg n A n/llg n merge B n/llg n B llg n llg n 25

26 Optimal Merge (A,B) 1.Merge A and B find Rank(A :B ), Rank(B :A ) using fast non-optimal algorithm Time = O(log log n) Work = O(n) 2.Compute Rank(A :B) and Rank(B :A) llg If Rank(p i, B) is r i, p i lies in block B ri Sequentially Time = O(log log n) Work = O(n) 3.Compute ranks of remaining elements Sequentially llg Time = O(log log n) Work = O(n) llg

27 Quick Sort Choose the pivot Select median? Subdivide into two groups Group sizes linearly related with high probability Sort each group independently

28 QuickSort Algorithm QuickSort(int A[], int first, int last) { } Select random m in [first:last] parallel for i in [first:last] flag[i] = A[i] < A[m]; // A[m] is pivot Split(A); // Separate flag values and 1, A[m] goes to k // Use Prefix Sum Quicksort A[first:k-1] and A[k+1:last]

29 Quick Sort Choose the pivot Select median? Subdivide into two groups Group sizes linearly related with high probability Sort each group independently Expected O(log n ) rounds Time per round = O(log n) Total work = O(n log n) with high probability

30 Hyper Quick Sort Partition into p groups Sort each group independently O(n/p log n) Choose median of one of the groups Partition each group into less and more set Binary search of the median: (log n) Separate into low and high Merge p/2 less and p/2 more pairs Each sequentially: O(n/p) Now we have p/2 less lists and p/2 more lists Partition and recurse Total time = O(n/p log n) with high probability

31 Partitioning Approach Break into p roughly equal sized problems Solve each sub-problem Preferably, independently of each other Focus on subdividing into independent parts 31

32 PARALLEL ALGORITHM TECHNIQUES: DIVIDE AND CONQUER

33 Merge Sort Partition data into two halves Assign half the processors to each half If only one processor remains, sequentially sort Sort each half Merge results T(n) = T(n/2) + O(log log n) W(n) = 2 W(n/2) + O(n) More on this later

34 Convex Hull Upper Hull Lower Hull

35 Convex Hull p i

36 Convex Hull

37 Convex Hull X

38 PARALLEL ALGORITHM TECHNIQUES: ACCELERATED CASCADING

39 Min-find Input: array with n numbers Algorithm A1 using O(n 2 ) processors: parallel for i in (:n] M[i]:= parallel for i,j in (:n] if i j && C[i] < C[j] M[j]=1 parallel for i in (:n] if M[i]= min = A[i] Not optimal: O(n 2 ) work

40 Optimal Min-find Balanced Binary tree O(log n) time O(n) work => Optimal Use Accelerated cascading Make the tree branch much faster Number of children of node u = n u Where n u is the number of leaves in u s subtree Works if the operation at each node can be performed in O(1)

41 From n 2 processors to n n A1 A1 A1 A1 A1 A1 A1 A1 A1 A1 A1 Step 1: Partition into disjoint blocks of size Step 2: Apply A1 to each block Step 3: Apply A1 to the results from the step 2 n

42 From n n processors to n 1+1/4 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 Step 1: Partition into disjoint blocks of size Step 2: Apply A2 to each block Step 3: Apply A2 to the results from the step 2 n 1/2. n 3/4 n 3/4

43 n 2 -> n 1+1/2 -> n 1+1/4 -> n 1+1/8 -> n 1+1/2k.. ~n 1+ ℇ Algorithm A k takes O(1) time with processors Does it? Algorithm A k+1 1. Partition input array C (size n) into disjoint blocks of size n 1/2 each 2. Solve for each block in parallel using algorithm A k 3. Re-apply A k to the results of step 3: n/n 1/2 minima n 1/2i = O(1) at leaf Doubly logarithmic-depth tree n log log n work, log log n time

44 Min-Find Review Constant-time algorithm O(n 2 ) work O(log n) Balanced Tree Approach O(n) work Optimal O(loglog n) Doubly-log depth tree Approach O(n loglog n) work Degree is high at the root, reduces going down #Children of node u = (#nodes in tree rooted at u) Depth = O(log log n)

45 Accelerated Cascading Solve recursively Start bojom-up with the oplmal algorithm unll the problem sizes is smaller Switch to fast (non-oplmal algorithm) A few small problems solved fast but non-workoplmally Min Find: OpLmal algorithm for lower loglog n levels Then switch to O(n loglog n)-work algorithm n work, log log n time

46 First 1 problem Input: n-bit vector Output: minimum index of a 1-bit Algorithm: Divide into n blocks of n bits each For block i, B[i] = 1 if there is a 1 in the block Arbitrary CRCW, O(n) work, O(1) time Find the first j such that B[j] = 1 O(n) work, O(1) time Find first 1 in the j th block O(n) work, O(1) time 46

47 All Nearest Smaller Value For each element A[i] in a vector find the largest j<i, such that A[j] < A[i] Algorithm: Find left match: For each i For each j < i, B[i,j] = (A[j] < A[i]) O(n 2 ) work For each B[i] find largest j<i with B[i,j] = 1 47

48 Prefix Minima For each element A[i] in a vector find the minimum of all A[j], j < i Algorithm: Find ANSV For each A[i] O(n 2 ) work O(1) Lme For each A[j], j<i PrefixMin[i] = A[j] where j is the largest index such that A[j] has no smaller value on its left m+ m+ m+ m+ m b a 48

49 Prefix Minima 1.Partition into n blocks of n each 2.Find prefix min for each block j recursively 3.Consider B = m 1.. m n where m j is the minima of block j Find prefix min of B in constant time 4.Minimum of A[1]..A[i], A[i] in block j is the minimum of m 1..m j-1 and A[i] s block prefix minima (computed in step 2) O(1) Lme O(n) work O(n log log n) work, O(log log n) time 49

50 PARALLEL ALGORITHM TECHNIQUES: RECURSIVE DOUBLING

51 List Ranking Given a linked list, compute distance of each node from the end: Initialize: if(next[i] == null) rank[i]=; else rank[i] = 1; log n times: parallel for i in [:n) if(next[i]!= null) rank[i] = rank[i] + rank[next[i]] next[i] = next[next[i]] 51

52 Find Roots in a Forest p(i): parent of node i Do in parallel p(i) = p(p(i)) Stop if p(i) = i Time: log(height) Work: n log(height) 9 7 9

53 Find Roots in a Forest p(i): parent of node i Do in parallel p(i) = p(p(i)) Stop if p(i) = i Time: log(height) Work: n log(height) 9 4 7

54 Find Roots in a Forest p(i): parent of node i Do in parallel p(i) = p(p(i)) Stop if p(i) = i Time: log(height) Work: n log(height) 9 1

55 Pointer Jumping Progressively push computation to all elements at a given distance Doubling the distance at each step After k steps the computation has been performed for elements within a distance of 2 k Applies to array, list, tree

56 PARALLEL ALGORITHM TECHNIQUES: SYMMETRY BREAKING

57 Cycle Coloring c[i]!= c[j] if e[i] = j e[i] = The next vertex from i O(n) SequenLal algorithm Parallel: Make local decision But all verlces look the same locally Break symmetry Put verlces in idenlfiable classes

58 3-Coloring Use vertex index to break symmetry But too many indices Algorithm: Initialize c[i] = i Iterate, reducing #colors each time parallel for all i do Let k = Least significant bit in which c[i] and c[e[i]] differ Set c[i] = 2k + bit k of c[i] 58

59 3-Coloring k = 2; 2 nd bit = => New color = 2*2 + = k = ; th bit = 1=> New color = 2* + 1 = 1 Why does this produce a coloring?

60 3-Coloring #new colors = O(log (#old colors)) Time O(log*n) Work O(n log*n) But can t reduce the number of colors below 6 t bits for i => log t + 1 bits for iteration i+1, t > 3 One more steps each to remove three extra colors parallel for all i If(c[i] = extra-color) c[i] = {,1,2} and NOT (c[[e[i]], c[predecessor[i]) 6

61 General Algorithmic Techniques Pipelining Balanced binary tree Divide and conquer Partitioning Accelerated Cascading Pointer doubling Symmetry breaking 61

CSL 860: Modern Parallel

CSL 860: Modern Parallel CSL 860: Modern Parallel Computation PARALLEL ALGORITHM TECHNIQUES: BALANCED BINARY TREE Reduction n operands => log n steps Total work = O(n) How do you map? Balance Binary tree technique Reduction n

More information

CSL 730: Parallel Programming. Algorithms

CSL 730: Parallel Programming. Algorithms CSL 73: Parallel Programming Algorithms First 1 problem Input: n-bit vector Output: minimum index of a 1-bit First 1 problem Input: n-bit vector Output: minimum index of a 1-bit Algorithm: Divide into

More information

Example of usage of Prefix Sum Compacting an Array. Example of usage of Prexix Sum Compacting an Array

Example of usage of Prefix Sum Compacting an Array. Example of usage of Prexix Sum Compacting an Array Example of usage of Prefix um A 0 0 0 e 1 0 0 0 0 0 B e 1 Example of usage of Prexix um A 0 0 0 e 1 0 0 0 0 0 B e 1 Initialize B with zeroes Any idea on the solution (first in sequential)? If A[i]!= 0

More information

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms Part 2 1 3 Maximum Selection Problem : Given n numbers, x 1, x 2,, x

More information

Complexity and Advanced Algorithms Monsoon Parallel Algorithms Lecture 2

Complexity and Advanced Algorithms Monsoon Parallel Algorithms Lecture 2 Complexity and Advanced Algorithms Monsoon 2011 Parallel Algorithms Lecture 2 Trivia ISRO has a new supercomputer rated at 220 Tflops Can be extended to Pflops. Consumes only 150 KW of power. LINPACK is

More information

Question 7.11 Show how heapsort processes the input:

Question 7.11 Show how heapsort processes the input: Question 7.11 Show how heapsort processes the input: 142, 543, 123, 65, 453, 879, 572, 434, 111, 242, 811, 102. Solution. Step 1 Build the heap. 1.1 Place all the data into a complete binary tree in the

More information

CS256 Applied Theory of Computation

CS256 Applied Theory of Computation CS256 Applied Theory of Computation Parallel Computation IV John E Savage Overview PRAM Work-time framework for parallel algorithms Prefix computations Finding roots of trees in a forest Parallel merging

More information

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT. Week 10 1 Binary s 2 3 4 5 6 Sorting Binary s 7 8 General remarks Binary s We return to sorting, considering and. Reading from CLRS for week 7 1 Chapter 6, Sections 6.1-6.5. 2 Chapter 7, Sections 7.1,

More information

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT. Week 10 1 2 3 4 5 6 Sorting 7 8 General remarks We return to sorting, considering and. Reading from CLRS for week 7 1 Chapter 6, Sections 6.1-6.5. 2 Chapter 7, Sections 7.1, 7.2. Discover the properties

More information

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting Simple Sorting 7. Sorting I 7.1 Simple Sorting Selection Sort, Insertion Sort, Bubblesort [Ottman/Widmayer, Kap. 2.1, Cormen et al, Kap. 2.1, 2.2, Exercise 2.2-2, Problem 2-2 19 197 Problem Algorithm:

More information

CSL 860: Modern Parallel

CSL 860: Modern Parallel CSL 860: Modern Parallel Computation PARALLEL SORTING BitonicMerge and Sort Bitonicsequence: {a 0, a 1,, a n-1 }: A sequence with a monotonically increasing part and a monotonically decreasing part For

More information

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n. Problem 5. Sorting Simple Sorting, Quicksort, Mergesort Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all 1 i j n. 98 99 Selection Sort

More information

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( End Semester ) SEMESTER ( Autumn ) Roll Number Section Name Subject Number C S 6 0 0 2 6 Subject Name Parallel

More information

Paradigms for Parallel Algorithms

Paradigms for Parallel Algorithms S Parallel Algorithms Paradigms for Parallel Algorithms Reference : C. Xavier and S. S. Iyengar, Introduction to Parallel Algorithms Binary Tree Paradigm A binary tree with n nodes is of height log n Can

More information

Real parallel computers

Real parallel computers CHAPTER 30 (in old edition) Parallel Algorithms The PRAM MODEL OF COMPUTATION Abbreviation for Parallel Random Access Machine Consists of p processors (PEs), P 0, P 1, P 2,, P p-1 connected to a shared

More information

Searching, Sorting. part 1

Searching, Sorting. part 1 Searching, Sorting part 1 Week 3 Objectives Searching: binary search Comparison-based search: running time bound Sorting: bubble, selection, insertion, merge Sorting: Heapsort Comparison-based sorting

More information

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to

More information

Lesson 1 4. Prefix Sum Definitions. Scans. Parallel Scans. A Naive Parallel Scans

Lesson 1 4. Prefix Sum Definitions. Scans. Parallel Scans. A Naive Parallel Scans Lesson 1 4 Prefix Sum Definitions Prefix sum given an array...the prefix sum is the sum of all the elements in the array from the beginning to the position, including the value at the position. The sequential

More information

COMP Parallel Computing. PRAM (2) PRAM algorithm design techniques

COMP Parallel Computing. PRAM (2) PRAM algorithm design techniques COMP 633 - Parallel Computing Lecture 3 Aug 29, 2017 PRAM algorithm design techniques Reading for next class (Thu Aug 31): PRAM handout secns 3.6, 4.1, skim section 5. Written assignment 1 is posted, due

More information

Algorithms and Applications

Algorithms and Applications Algorithms and Applications 1 Areas done in textbook: Sorting Algorithms Numerical Algorithms Image Processing Searching and Optimization 2 Chapter 10 Sorting Algorithms - rearranging a list of numbers

More information

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Algorithm Efficiency & Sorting Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Overview Writing programs to solve problem consists of a large number of decisions how to represent

More information

COMP Data Structures

COMP Data Structures COMP 2140 - Data Structures Shahin Kamali Topic 5 - Sorting University of Manitoba Based on notes by S. Durocher. COMP 2140 - Data Structures 1 / 55 Overview Review: Insertion Sort Merge Sort Quicksort

More information

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms Analysis of Algorithms Unit 4 - Analysis of well known Algorithms 1 Analysis of well known Algorithms Brute Force Algorithms Greedy Algorithms Divide and Conquer Algorithms Decrease and Conquer Algorithms

More information

: Parallel Algorithms Exercises, Batch 1. Exercise Day, Tuesday 18.11, 10:00. Hand-in before or at Exercise Day

: Parallel Algorithms Exercises, Batch 1. Exercise Day, Tuesday 18.11, 10:00. Hand-in before or at Exercise Day 184.727: Parallel Algorithms Exercises, Batch 1. Exercise Day, Tuesday 18.11, 10:00. Hand-in before or at Exercise Day Jesper Larsson Träff, Francesco Versaci Parallel Computing Group TU Wien October 16,

More information

Divide and Conquer Sorting Algorithms and Noncomparison-based

Divide and Conquer Sorting Algorithms and Noncomparison-based Divide and Conquer Sorting Algorithms and Noncomparison-based Sorting Algorithms COMP1927 16x1 Sedgewick Chapters 7 and 8 Sedgewick Chapter 6.10, Chapter 10 DIVIDE AND CONQUER SORTING ALGORITHMS Step 1

More information

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

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors 1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors on an EREW PRAM: See solution for the next problem. Omit the step where each processor sequentially computes the AND of

More information

We can use a max-heap to sort data.

We can use a max-heap to sort data. Sorting 7B N log N Sorts 1 Heap Sort We can use a max-heap to sort data. Convert an array to a max-heap. Remove the root from the heap and store it in its proper position in the same array. Repeat until

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

Parallel Sorting Algorithms

Parallel Sorting Algorithms CSC 391/691: GPU Programming Fall 015 Parallel Sorting Algorithms Copyright 015 Samuel S. Cho Sorting Algorithms Review Bubble Sort: O(n ) Insertion Sort: O(n ) Quick Sort: O(n log n) Heap Sort: O(n log

More information

Exercise 1 : B-Trees [ =17pts]

Exercise 1 : B-Trees [ =17pts] CS - Fall 003 Assignment Due : Thu November 7 (written part), Tue Dec 0 (programming part) Exercise : B-Trees [+++3+=7pts] 3 0 3 3 3 0 Figure : B-Tree. Consider the B-Tree of figure.. What are the values

More information

PART IV. Given 2 sorted arrays, What is the time complexity of merging them together?

PART IV. Given 2 sorted arrays, What is the time complexity of merging them together? General Questions: PART IV Given 2 sorted arrays, What is the time complexity of merging them together? Array 1: Array 2: Sorted Array: Pointer to 1 st element of the 2 sorted arrays Pointer to the 1 st

More information

Sorting and Selection

Sorting and Selection Sorting and Selection Introduction Divide and Conquer Merge-Sort Quick-Sort Radix-Sort Bucket-Sort 10-1 Introduction Assuming we have a sequence S storing a list of keyelement entries. The key of the element

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 310 Advanced Data Structures and Algorithms Sorting June 13, 2017 Tong Wang UMass Boston CS 310 June 13, 2017 1 / 42 Sorting One of the most fundamental problems in CS Input: a series of elements with

More information

8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel

8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel Heapsort, Quicksort, Mergesort 8. Sorting II 8.1 Heapsort [Ottman/Widmayer, Kap. 2.3, Cormen et al, Kap. 6] 9 210 Heapsort [Max-]Heap 6 Binary tree with the following properties Wurzel Inspiration from

More information

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 2. O(n) 2. [1 pt] What is the solution to the recurrence T(n) = T(n/2) + n, T(1)

More information

Range Reporting. Range Reporting. Range Reporting Problem. Applications

Range Reporting. Range Reporting. Range Reporting Problem. Applications Philip Bille Problem problem. Preprocess at set of points P R 2 to support report(x1, y1, x2, y2): Return the set of points in R P, where R is rectangle given by (x1, y1) and (x2, y2). Applications Relational

More information

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

CSE 530A. B+ Trees. Washington University Fall 2013 CSE 530A B+ Trees Washington University Fall 2013 B Trees A B tree is an ordered (non-binary) tree where the internal nodes can have a varying number of child nodes (within some range) B Trees When a key

More information

Complexity and Advanced Algorithms Monsoon Parallel Algorithms Lecture 4

Complexity and Advanced Algorithms Monsoon Parallel Algorithms Lecture 4 Complexity and Advanced Algorithms Monsoon 2011 Parallel Algorithms Lecture 4 Advanced Optimal Solutions 1 8 5 11 2 6 10 4 3 7 12 9 General technique suggests that we solve a smaller problem and extend

More information

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

CS 598: Communication Cost Analysis of Algorithms Lecture 15: Communication-optimal sorting and tree-based algorithms CS 598: Communication Cost Analysis of Algorithms Lecture 15: Communication-optimal sorting and tree-based algorithms Edgar Solomonik University of Illinois at Urbana-Champaign October 12, 2016 Defining

More information

Computational Geometry

Computational Geometry Windowing queries Windowing Windowing queries Zoom in; re-center and zoom in; select by outlining Windowing Windowing queries Windowing Windowing queries Given a set of n axis-parallel line segments, preprocess

More information

Design and Analysis of Algorithms - - Assessment

Design and Analysis of Algorithms - - Assessment X Courses» Design and Analysis of Algorithms Week 1 Quiz 1) In the code fragment below, start and end are integer values and prime(x) is a function that returns true if x is a prime number and false otherwise.

More information

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Comparison Sorts. Chapter 9.4, 12.1, 12.2 Comparison Sorts Chapter 9.4, 12.1, 12.2 Sorting We have seen the advantage of sorted data representations for a number of applications Sparse vectors Maps Dictionaries Here we consider the problem of

More information

Quick Sort. CSE Data Structures May 15, 2002

Quick Sort. CSE Data Structures May 15, 2002 Quick Sort CSE 373 - Data Structures May 15, 2002 Readings and References Reading Section 7.7, Data Structures and Algorithm Analysis in C, Weiss Other References C LR 15-May-02 CSE 373 - Data Structures

More information

Algorithms & Data Structures 2

Algorithms & Data Structures 2 Algorithms & Data Structures 2 PRAM Algorithms WS2017 B. Anzengruber-Tanase (Institute for Pervasive Computing, JKU Linz) (Institute for Pervasive Computing, JKU Linz) RAM MODELL (AHO/HOPCROFT/ULLMANN

More information

would be included in is small: to be exact. Thus with probability1, the same partition n+1 n+1 would be produced regardless of whether p is in the inp

would be included in is small: to be exact. Thus with probability1, the same partition n+1 n+1 would be produced regardless of whether p is in the inp 1 Introduction 1.1 Parallel Randomized Algorihtms Using Sampling A fundamental strategy used in designing ecient algorithms is divide-and-conquer, where that input data is partitioned into several subproblems

More information

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Merge Sort & Quick Sort 1 Divide-and-Conquer Divide-and conquer is a general algorithm

More information

Lecture 19 Sorting Goodrich, Tamassia

Lecture 19 Sorting Goodrich, Tamassia Lecture 19 Sorting 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 2004 Goodrich, Tamassia Outline Review 3 simple sorting algorithms: 1. selection Sort (in previous course) 2. insertion Sort (in previous

More information

Merge Sort Goodrich, Tamassia Merge Sort 1

Merge Sort Goodrich, Tamassia Merge Sort 1 Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 2004 Goodrich, Tamassia Merge Sort 1 Review of Sorting Selection-sort: Search: search through remaining unsorted elements for min Remove: remove

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Searching in Lists Ulf Leser Topics of Next Lessons Search: Given a (sorted or unsorted) list A with A =n elements (integers). Check whether a given value c is contained

More information

Chapter 11: Indexing and Hashing

Chapter 11: Indexing and Hashing Chapter 11: Indexing and Hashing Basic Concepts Ordered Indices B + -Tree Index Files B-Tree Index Files Static Hashing Dynamic Hashing Comparison of Ordered Indexing and Hashing Index Definition in SQL

More information

Cpt S 122 Data Structures. Sorting

Cpt S 122 Data Structures. Sorting Cpt S 122 Data Structures Sorting Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Sorting Process of re-arranging data in ascending or descending order Given

More information

Lecture 3: Sorting 1

Lecture 3: Sorting 1 Lecture 3: Sorting 1 Sorting Arranging an unordered collection of elements into monotonically increasing (or decreasing) order. S = a sequence of n elements in arbitrary order After sorting:

More information

TREES. Trees - Introduction

TREES. Trees - Introduction TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)

More information

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 1. O(logn) 2. O(n) 3. O(nlogn) 4. O(n 2 ) 5. O(2 n ) 2. [1 pt] What is the solution

More information

PRAM Divide and Conquer Algorithms

PRAM Divide and Conquer Algorithms PRAM Divide and Conquer Algorithms (Chapter Five) Introduction: Really three fundamental operations: Divide is the partitioning process Conquer the the process of (eventually) solving the eventual base

More information

CISC 621 Algorithms, Midterm exam March 24, 2016

CISC 621 Algorithms, Midterm exam March 24, 2016 CISC 621 Algorithms, Midterm exam March 24, 2016 Name: Multiple choice and short answer questions count 4 points each. 1. The algorithm we studied for median that achieves worst case linear time performance

More information

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting) IS 709/809: Computational Methods in IS Research Algorithm Analysis (Sorting) Nirmalya Roy Department of Information Systems University of Maryland Baltimore County www.umbc.edu Sorting Problem Given an

More information

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used Sorting Sunday, October 21, 2007 11:47 PM Two types of sort internal - all done in memory external - secondary storage may be used 13.1 Quadratic sorting methods data to be sorted has relational operators

More information

Brute Force: Selection Sort

Brute Force: Selection Sort Brute Force: Intro Brute force means straightforward approach Usually based directly on problem s specs Force refers to computational power Usually not as efficient as elegant solutions Advantages: Applicable

More information

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1 Sorting Riley Porter 1 Introduction to Sorting Why study sorting? Good algorithm practice! Different sorting algorithms have different trade-offs No single best sort for all scenarios Knowing one way to

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today Recursive Sorting Methods and their Complexity: Mergesort Conclusions on sorting algorithms and complexity Next Time:

More information

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7 CSE341T 09/20/2017 Lecture 7 Scan and Quicksort 1 Scan Scan is a very useful primitive for parallel programming. We will use it all the time in this class. First, lets start by thinking about what other

More information

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8 CSE341T/CSE549T 09/17/2014 Lecture 8 Scan and its Uses 1 Scan Today, we start by learning a very useful primitive. First, lets start by thinking about what other primitives we have learned so far? The

More information

Divide-and-Conquer. Dr. Yingwu Zhu

Divide-and-Conquer. Dr. Yingwu Zhu Divide-and-Conquer Dr. Yingwu Zhu Divide-and-Conquer The most-well known algorithm design technique: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances independently

More information

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions Dr. Amotz Bar-Noy s Compendium of Algorithms Problems Problems, Hints, and Solutions Chapter 1 Searching and Sorting Problems 1 1.1 Array with One Missing 1.1.1 Problem Let A = A[1],..., A[n] be an array

More information

More PRAM Algorithms. Techniques Covered

More PRAM Algorithms. Techniques Covered More PRAM Algorithms Arvind Krishnamurthy Fall 24 Analysis technique: Brent s scheduling lemma Techniques Covered Parallel algorithm is simply characterized by W(n) and S(n) Parallel techniques: Scans

More information

Parallel Sorting. Sathish Vadhiyar

Parallel Sorting. Sathish Vadhiyar Parallel Sorting Sathish Vadhiyar Parallel Sorting Problem The input sequence of size N is distributed across P processors The output is such that elements in each processor P i is sorted elements in P

More information

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017 CS 137 Part 8 Merge Sort, Quick Sort, Binary Search November 20th, 2017 This Week We re going to see two more complicated sorting algorithms that will be our first introduction to O(n log n) sorting algorithms.

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

Visualizing Data Structures. Dan Petrisko

Visualizing Data Structures. Dan Petrisko Visualizing Data Structures Dan Petrisko What is an Algorithm? A method of completing a function by proceeding from some initial state and input, proceeding through a finite number of well defined steps,

More information

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms. Next 1. Covered basics of a simple design technique (Divideand-conquer) Ch. 2 of the text. 2. Next, more sorting algorithms. Sorting Switch from design paradigms to applications. Sorting and order statistics

More information

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

CS8391-DATA STRUCTURES QUESTION BANK UNIT I CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Define data structure. The data structure can be defined as the collection of elements and all the possible operations which are required for those

More information

CS 171: Introduction to Computer Science II. Binary Search Trees

CS 171: Introduction to Computer Science II. Binary Search Trees CS 171: Introduction to Computer Science II Binary Search Trees Binary Search Trees Symbol table applications BST definitions and terminologies Search and insert Traversal Ordered operations Delete Symbol

More information

UNIT 7. SEARCH, SORT AND MERGE

UNIT 7. SEARCH, SORT AND MERGE UNIT 7. SEARCH, SORT AND MERGE ALGORITHMS Year 2017-2018 Industrial Technology Engineering Paula de Toledo CONTENTS 7.1. SEARCH 7.2. SORT 7.3. MERGE 2 SEARCH Search, sort and merge algorithms Search (search

More information

Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b

Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b simas@cs.aau.dk Range Searching in 2D Main goals of the lecture: to understand and to be able to analyze the kd-trees

More information

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CS 315 Data Structures Spring 2012 Final examination Total Points: 80 CS 315 Data Structures Spring 2012 Final examination Total Points: 80 Name This is an open-book/open-notes exam. Write the answers in the space provided. Answer for a total of 80 points, including at least

More information

Disjoint Sets. (Disjoint Sets) Data Structures and Programming Spring / 50

Disjoint Sets. (Disjoint Sets) Data Structures and Programming Spring / 50 Disjoint Sets (Disjoint Sets) Data Structures and Programming Spring 2017 1 / 50 Making a Good Maze What s a Good Maze? Connected Just one path between any two rooms Random The Maze Construction Problem

More information

Balanced Search Trees

Balanced Search Trees Balanced Search Trees Computer Science E-22 Harvard Extension School David G. Sullivan, Ph.D. Review: Balanced Trees A tree is balanced if, for each node, the node s subtrees have the same height or have

More information

Introduction to Computers and Programming. Today

Introduction to Computers and Programming. Today Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 10 April 8 2004 Today How to determine Big-O Compare data structures and algorithms Sorting algorithms 2 How to determine Big-O Partition

More information

Randomized Algorithms

Randomized Algorithms Randomized Algorithms Last time Network topologies Intro to MPI Matrix-matrix multiplication Today MPI I/O Randomized Algorithms Parallel k-select Graph coloring Assignment 2 Parallel I/O Goal of Parallel

More information

Arrays, Vectors Searching, Sorting

Arrays, Vectors Searching, Sorting Arrays, Vectors Searching, Sorting Arrays char s[200]; //array of 200 characters different type than class string can be accessed as s[0], s[1],..., s[199] s[0]= H ; s[1]= e ; s[2]= l ; s[3]= l ; s[4]=

More information

BM267 - Introduction to Data Structures

BM267 - Introduction to Data Structures BM267 - Introduction to Data Structures 7. Quicksort Ankara University Computer Engineering Department Bulent Tugrul Bm 267 1 Quicksort Quicksort uses a divide-and-conquer strategy A recursive approach

More information

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014 CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting Aaron Bauer Winter 2014 The main problem, stated carefully For now, assume we have n comparable elements in an array and we want

More information

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer CSC 8301- Design and Analysis of Algorithms Lecture 5 Decrease and Conquer Algorithm Design Technique Decrease-and-Conquer This algorithm design technique is based on exploiting a relationship between

More information

List Ranking. Chapter 4

List Ranking. Chapter 4 List Ranking Chapter 4 Problem on linked lists 2-level memory model List Ranking problem Given a (mono directional) linked list L of n items, compute the distance of each item from the tail of L. Id Succ

More information

Lecture 6 Sorting and Searching

Lecture 6 Sorting and Searching Lecture 6 Sorting and Searching Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101 There are many algorithms for sorting a list

More information

CSE 638: Advanced Algorithms. Lectures 10 & 11 ( Parallel Connected Components )

CSE 638: Advanced Algorithms. Lectures 10 & 11 ( Parallel Connected Components ) CSE 6: Advanced Algorithms Lectures & ( Parallel Connected Components ) Rezaul A. Chowdhury Department of Computer Science SUNY Stony Brook Spring 01 Symmetry Breaking: List Ranking break symmetry: t h

More information

List Ranking. Chapter 4

List Ranking. Chapter 4 List Ranking Chapter 4 Problem on linked lists 2-level memory model List Ranking problem Given a (mono directional) linked list L of n items, compute the distance of each item from the tail of L. Id Succ

More information

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment. CSE 3101: Introduction to the Design and Analysis of Algorithms Instructor: Suprakash Datta (datta[at]cse.yorku.ca) ext 77875 Lectures: Tues, BC 215, 7 10 PM Office hours: Wed 4-6 pm (CSEB 3043), or by

More information

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4. COMPSCI 330: Design and Analysis of Algorithms 8/28/2014 Lecturer: Debmalya Panigrahi Lecture #2 Scribe: Yilun Zhou 1 Overview This lecture presents two sorting algorithms, quicksort and mergesort, that

More information

Chapter 7 Sorting. Terminology. Selection Sort

Chapter 7 Sorting. Terminology. Selection Sort Chapter 7 Sorting Terminology Internal done totally in main memory. External uses auxiliary storage (disk). Stable retains original order if keys are the same. Oblivious performs the same amount of work

More information

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE A6-R3: DATA STRUCTURE THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be answered in the TEAR-OFF

More information

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia Merge Sort 7 9 4 fi 4 7 9 7 fi 7 9 4 fi 4 9 7 fi 7 fi 9 fi 9 4 fi 4 Merge Sort 1 Divide-and-Conquer ( 10.1.1) Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S

More information

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue Readings Priority Queues & Binary Heaps Chapter Section.-. CSE Data Structures Winter 00 Binary Heaps FindMin Problem Quickly find the smallest (or highest priority) item in a set Applications: Operating

More information

Parallel Random-Access Machines

Parallel Random-Access Machines Parallel Random-Access Machines Marc Moreno Maza University of Western Ontario, London, Ontario (Canada) CS3101 (Moreno Maza) Parallel Random-Access Machines CS3101 1 / 69 Plan 1 The PRAM Model 2 Performance

More information

Euler Tours and Their Applications. Chris Moultrie CSc 8530

Euler Tours and Their Applications. Chris Moultrie CSc 8530 Euler Tours and Their Applications Chris Moultrie CSc 8530 Topics Covered History Terminology Sequential Algorithm Parallel Algorithm BSP/CGM Algorithm History Started with the famous Konigsberg problem.

More information

Material You Need to Know

Material You Need to Know Review Quiz 2 Material You Need to Know Normalization Storage and Disk File Layout Indexing B-trees and B+ Trees Extensible Hashing Linear Hashing Decomposition Goals: Lossless Joins, Dependency preservation

More information

University of the Western Cape Department of Computer Science

University of the Western Cape Department of Computer Science University of the Western Cape Department of Computer Science Algorithms and Complexity CSC212 Paper II Final Examination 13 November 2015 Time: 90 Minutes. Marks: 100. UWC number Surname, first name Mark

More information

CS 8391 DATA STRUCTURES

CS 8391 DATA STRUCTURES DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK CS 8391 DATA STRUCTURES UNIT- I PART A 1. Define: data structure. A data structure is a way of storing and organizing data in the memory for

More information

CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information