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

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

Divide and Conquer Algorithms

Lecture 6: Divide-and-Conquer

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

Lecture 6 Sequences II. Parallel and Sequential Data Structures and Algorithms, (Fall 2013) Lectured by Danny Sleator 12 September 2013

Subset sum problem and dynamic programming

PRAM Divide and Conquer Algorithms

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

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 25 Suffix Arrays

The DAG Model; Analysis of For-Loops; Reduction

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

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

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

Parallel Breadth First Search

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Sorting is a problem for which we can prove a non-trivial lower bound.

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

(Refer Slide Time: 01.26)

Dynamic Programming Algorithms

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Let the dynamic table support the operations TABLE-INSERT and TABLE-DELETE It is convenient to use the load factor ( )

Graph Contraction. Graph Contraction CSE341T/CSE549T 10/20/2014. Lecture 14

Principles of Algorithm Design

Lecture 2: Getting Started

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

ECE608 - Chapter 15 answers

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

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

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

CMPSCI 311: Introduction to Algorithms Practice Final Exam

15 210: Parallel and Sequential Data Structures and Algorithms

Algorithms and Applications

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

The complexity of Sorting and sorting in linear-time. Median and Order Statistics. Chapter 8 and Chapter 9

Design and Analysis of Algorithms

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Sorting & Growth of Functions

Lecture Notes on Quicksort

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Lecture 6: Arithmetic and Threshold Circuits

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Dynamic Programming Algorithms

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

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

(Refer Slide Time: 1:27)

CS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example

Hypercubes. (Chapter Nine)

EE/CSCI 451 Spring 2018 Homework 8 Total Points: [10 points] Explain the following terms: EREW PRAM CRCW PRAM. Brent s Theorem.

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Recitation 1. Scan. 1.1 Announcements. SkylineLab has been released, and is due Friday afternoon. It s worth 125 points.

Lecture 15 : Review DRAFT

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

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Exam Sample Questions CS3212: Algorithms and Data Structures

Selection (deterministic & randomized): finding the median in linear time

More Counting Sort and Sorting-by-Key

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

The divide and conquer strategy has three basic parts. For a given problem of size n,

CS584/684 Algorithm Analysis and Design Spring 2017 Week 3: Multithreaded Algorithms

CSCI-1200 Data Structures Fall 2017 Lecture 13 Problem Solving Techniques

ECE 122. Engineering Problem Solving Using Java

COSC242 Lecture 7 Mergesort and Quicksort

Data Structures Lecture 8

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/27/18

Dynamic Programming. CS 374: Algorithms & Models of Computation, Fall Lecture 11. October 1, 2015

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Recurrences and Divide-and-Conquer

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Greedy Algorithms II

Binary Search to find item in sorted array

6.001 Notes: Section 8.1

EECS 2011M: Fundamentals of Data Structures

Algorithms Lab 3. (a) What is the minimum number of elements in the heap, as a function of h?

Intro. Scheme Basics. scm> 5 5. scm>

Introduction to Algorithms I

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Algorithms. Lecture Notes 5

1 Probabilistic analysis and randomized algorithms

II (Sorting and) Order Statistics

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

1 More on the Bellman-Ford Algorithm

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Transcription:

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 most important one was REDUCE given n elements, you can return the sum (or really, apply any associative operator) of all elements in O(n) work and O(lg n) span. Today, we will learn a much more powerful primitive. Given a sequence A[1..n], the scan operation returns another sequence B[0..n] such that B[i] = i A[j] j=1 Where the sum here can be any associative operation. That is, you can have any operator or function f, such that f(f(x, y), z) = f(x, f(y, z)). Since it computes prefix of each index, this operation is sometimes called prefix sums. Note that B[0] always contains the identity of the function you are applying and B[n] contains the answer to REDUCE(A). One way to do a scan is to do it sequentially with O(n) work and O(n) span. Another way is to do n reduce operations O(n lg n) work and O(lg n) span. We want the best of both worlds. 1.1 Contraction In order to learn about scan, we are first going to learn about another algorithmic technique. What techniques have you learned so far? Divide and Conquer is the most important one. This is another important one, called contraction. This is another common inductive technique in algorithms design. It is inductive in that such an algorithm involves solving a smaller instance of the same problem, much in the same spirit as a divide-and-conquer algorithm. In particular, the contraction technique involves the following steps: 1. Reduce the instance of the problem to a (much) smaller instance (of the same sort) 2. Solve the smaller instance recursively 3. Use the solution to help solve the original instance 1

The contraction approach is a useful technique in algorithms design. For various reasons, it is more common in parallel algorithm than in sequential algorithms, usually because the contraction and expansion can be done in parallel and the recursion only goes logarithmically deep because the problem size is shrunk by a constant fraction each time. 1.2 Applying Contraction to Scan We ll demonstrate this technique by applying it to the scan problem. To begin, we have to answer the following question: How do we make the input instance smaller in a way that the solution on this smaller instance will benefit us in constructing the final solution? Let s look at an example for motivation. Suppose we re to run plus_scan (i.e. scan (op +)) on the sequence [2, 1, 3, 2, 2, 5, 4, 1]. What we should get back is [0, 2, 3, 6, 8, 10, 15, 19, 20] We will call the last element the final element. Thought Experiment I: At some level, this problem seems like it can be solved using the divideand-conquer approach. Let s try a simple pattern: divide up the input sequence in half, recursively solve each half, and piece together the solutions. A moment s thought shows that the two recursive calls are not independent indeed, the right half depends on the outcome of the left one because it has to know the cumulative sum. So, although the work is O(n), we effectively haven t broken the chain of sequential dependencies. In fact, we can see that any scheme that splits the sequence into left and right parts like this will essentially run into the same problem. Thought Experiment II: The crux of this problem is the realization that we can easily generate a sequence consisting of every other element of the final output, together with the final sum and this is enough information to produce the desired final output with ease. Let s say we are able to somehow generate the sequence [0, 3, 8, 15, 20]. Then, the diagram below shows how to produce the final output sequence: Input = h2, 1, 3, 2, 2, 5, 4, 1i Partial Output = (h0, 3, 8, 15i, 20) + + + + Desired Output = (h0, 2, 3, 6, 8, 10, 15, 19i, 20) But how do we generate the partial output the sequence with every other element of the desired output? The idea is simple: we pairwise add adjacent elements of the input sequence and 2

recursively run scan on it. That is, on input sequence 2, 1, 3, 2, 2, 5, 4, 1, we would be running scan on 3, 5, 7, 5, which will generate the desired partial output. Scan(A, n) 1 parallel for i 1 to n/2 2 do C[i] A[2i 1] + A[2i] 3 C SCAN(C, n/2) 4 parallel for i 0 to n/2 1 5 do B[2i] C [i] 6 B[2i] C [i] + A[2i + 1] 7 B[n] C[n/2] 8 return B Now, lets analyze it. The work recurrence is W (n) = W (n/2) + Θ(n) = Θ(n). The span recurrence is S(n) = S(n/2) + Θ(lg n) = Θ(lg 2 n). Best Known Bounds for Scan: You can do scan in O(n) work and O(lg n) span. However, the algorithm is a little complicated and we won t cover it in class. For the purposes of this class, you may assume that you are given that algorithm as a black-box and you can cite this result when analyzing other algorithms. Feel free to think about how you would go about improving the span of scan. 2 Parenthesis Matching We first look at the parenthesis matching problem, which is defined as follows: You are given a sequence of characters such that each character is either ( or ). You want to return true if the sequence is well formed and false if the sequence is not well-formed. For instance (, (, ), (, ), ) is a well formed sequence, while ), (, ), (, ), ) is not. 2.1 Sequence Fold Lets start with the simplest sequential solution. You can just go through and keep a counter. When you see an open parenthesis, increment the counter. When you see a closed parenthesis, decrement it. If the counter ever goes negative, return false. The counter should be 0 at the end of the sequence. You can show that this solution has O(n) work and span, where n is the length of the input sequence. How can we make it more parallel? 3

2.2 Divide and Conquer Lets try the simplest: Divide the sequence into two equal halves. What should the recursive calls return for us to be able to merge? The first thing that comes to mind might be that the function returns whether the given sequence is well-formed. Clearly, if both s 1 and s 2 are well-formed expressions, s 1 concatenated with s 2 must be a well-formed expression. The problem is that we could have s 1 and s 2 such that neither of which is well-formed but s 1 s 2 is well-formed (e.g., ((( and ))) ). This is not enough information to conclude whether s 1 s 2 is well-formed. We need more information from the recursive calls. We ll crucially rely on the following observations (which can be formally shown by induction): Observation 1 If s contains () as a substring, then s is a well-formed parenthesis expression if and only if s derived by removing this pair of parenthesis () from s is a well-formed expression. Applying this reduction repeatedly, we can show that a parenthesis sequence is well-formed if and only if it eventually reduces to an empty string. Observation 2 If s does not contain () as a substring, then s has the form ) i ( j. That is, it is a sequence of close parens followed by a sequence of open parens. That is to say, on a given sequence s, we ll keep simplifying s conceptually until it contains no substring () and return the pair (i, j) as our result. This is relatively easy to do recursively. Consider that if s = s 1 s 2, after repeatedly getting rid of () in s 1 and separately in s 2, we ll have that s 1 reduces to ) i ( j and s 2 reduces to ) k ( l for some i, j, k, l. To completely simplify s, we merge the results. That is, we merge ) i ( j with ) k ( l. The rules are simple: If j k (i.e., more close parens than open parens), we ll get ) i+k j ( l. Otherwise j > k (i.e., more open parens than close parens), we ll get ) i ( l+j k. This directly leads to a divide and conquer algorithm. Exercise 1 Write down this divide and conquer algorithm. What is the work and span of this algorithm? W (n) = 2W (n/2) + O(1) = O(n) S(n) = S(n/2) + O(1) = O(lg n) 4

2.3 Using Scan We can use scan to solve the parenthesis matching problem even more easily. Remember our original sequential algorithm? If we first map each open parenthesis to 1 and each close parenthesis to 1, then we just need to make sure that the k i 1 s i is never negative for any k. This is exactly what scan can do. We do a +-scan on this integer sequence. The elements in the sequence returned by scan exactly correspond how many unmatched parenthesis there are in that prefix of the string. Therefore, if the sum of any prefix is ever negative, then we had too many closed parenthesis and the sequence is not well-formed. For example: (, ), (, (, ), ), ) becomes and then 1, 1, 1, 1, 1, 1, 1 0, 1, 0, 1, 2, 1, 0, 1 and then fails, because the counter went negative at some point indicating an imbalance. 3 MCSS Problem We looked at the maximum contiguous subsequence sum problem last week; given a sequence S, we wanted to find a contiguous subsequence that had the largest sum. Lets say that we (for the heck of it) do a sum-scan of the sequence and store the result in array X. What is the meaning of X[j]? It is the sum of the subsequence from S[1] to S[j]. What if we wanted to calculate the sum of subsequence from i to j (both inclusive) we can simply calculate X[j] X[i 1]. Can we use this to solve a simpler problem: What if you wanted to find out the MCSS that ended at a particular index j? Can I write down this solution in terms of just X? Well this is R j = j max i=1 j S k = max j (X[j] X[i 1]) = X j min j 1 X[i] i=1 i=0 k=i What is the last term? It is just the minimum value of X up to j (exclusive). Now we want to calculate it for all j, so we can use a scan (min) operation. Once we have R j, we can calculate the minimum R j using reduce. Lets write down the pseudocode: 5

1 X sum-scan(s) 2 Y min-scan(x) 3 parallel for all i 4 do R[i] X[i] Y [i 1] 5 return max-reduce(r) The work of each of the steps (two scans, a parallel-for loop and reduce) is O(n) and the span is O(log n). We therefore have a routine that is even better than any of our divide and conquer routines. 4 Remove Duplicates You are given a sorted array A which contains duplicate elements. How can we create an array B which contains all the elements of A, but without the duplicates? We first simply create a boolean array C such that C[i] = 1 if A[i] A[i 1] and 0 otherwise. We can now do a sum-scan on C if D[i] = k, then the number of distinct elements in A which are smaller than or equal A[i] is k. Therefore, A[i] should go in the kth position in B. 1 parallel for i 1 to n 2 do if A[i] A[i 1] 3 then C[i] 1 4 else C[i] 0 5 D sum-scan(c) 6 parallel for i 1 to n 7 do if A[i] A[i 1] 8 then B[D[i]] A[i] The work and the span of this algorithm are the same as that of the Scan algorithm. 6