Richard Anderson Lecture 14 Divide and Conquer

Slides:



Advertisements
Similar presentations
Algorithm Design Paradigms
Advertisements

Divide-and-Conquer Matrix multiplication and Strassen’s algorithm Median Problem –In general finding the kth largest element of an unsorted list of numbers.
CS4413 Divide-and-Conquer
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
CSE 421 Algorithms Richard Anderson Lecture 12 Recurrences.
CSE 421 Algorithms Richard Anderson Lecture 13 Divide and Conquer.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
CSE 421 Algorithms Lecture 15 Closest Pair, Multiplication.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Divide and Conquer Strategy
CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 4.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11.
CSE 421 Algorithms Richard Anderson Lecture 13 Recurrences, Part 2.
Chapter 2. Divide-and-conquer Algorithms
Fundamentals of Algorithms MCS - 2 Lecture # 11
Algorithm Design & Analysis
Randomized Algorithms
Chapter 4 Divide-and-Conquer
Divide and Conquer – and an Example QuickSort
Divide-and-Conquer The most-well known algorithm design strategy:
Richard Anderson Lecture 14 Divide and Conquer
CSCE 411 Design and Analysis of Algorithms
Chapter 4: Divide and Conquer
Unit-2 Divide and Conquer
Randomized Algorithms
Punya Biswas Lecture 15 Closest Pair, Multiplication
Richard Anderson Lecture 13 Recurrences and Divide and Conquer
Divide-and-Conquer 7 2  9 4   2   4   7
Topic: Divide and Conquer
Richard Anderson Lecture 11 Recurrences
Richard Anderson Lecture 13 Divide and Conquer
Richard Anderson Lecture 12 Recurrences and Divide and Conquer
Richard Anderson Lecture 14 Inversions, Multiplication, FFT
Divide-and-Conquer The most-well known algorithm design strategy:
Lecture 29 CSE 331 Nov 8, 2017.
Richard Anderson Lecture 13 Recurrences, Part 2
Divide-and-Conquer The most-well known algorithm design strategy:
Lecture 31 CSE 331 Nov 14, 2012.
Merge and Count Merge and count step.
Merge and Count Merge and count step.
Merge and Count Merge and count step.
CSE 373 Data Structures and Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Lecture 30 CSE 331 Nov 12, 2012.
Lecture 15, Winter 2019 Closest Pair, Multiplication
Lecture 31 CSE 331 Nov 11, 2011.
CSC 380: Design and Analysis of Algorithms
David Kauchak cs161 Summer 2009
Lecture 30 CSE 331 Nov 9, 2011.
The Selection Problem.
Richard Anderson Lecture 13, Winter 2019 Recurrences, Part 2
Richard Anderson Lecture 14, Winter 2019 Divide and Conquer
Richard Anderson Lecture 14 Divide and Conquer
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture 15 Closest Pair, Multiplication
Richard Anderson Lecture 12, Winter 2019 Recurrences
CS200: Algorithm Analysis
Divide and Conquer Merge sort and quick sort Binary search
Richard Anderson Lecture 12 Recurrences
Richard Anderson Lecture 13 Divide and Conquer
Presentation transcript:

Richard Anderson Lecture 14 Divide and Conquer CSE 421 Algorithms Richard Anderson Lecture 14 Divide and Conquer

Announcements Review session, 2:30-4:30 pm, 5th floor breakout Midterm. Monday.

What you really need to know about recurrences Work per level changes geometrically with the level Geometrically increasing (x > 1) The bottom level wins Geometrically decreasing (x < 1) The top level wins Balanced (x = 1) Equal contribution

T(n) = aT(n/b) + nc Balanced: a = bc Increasing: a > bc T(n) = 4T(n/2) + n2 Increasing: a > bc T(n) = 9T(n/8) + n T(n) = 3T(n/4) + n1/2 Decreasing: a < bc T(n) = 5T(n/8) + n T(n) = 7T(n/2) + n3

Divide and Conquer Algorithms Split into sub problems Recursively solve the problem Combine solutions Make progress in the split and combine stages Quicksort – progress made at the split step Mergesort – progress made at the combine step D&C Algorithms Strassen’s Algorithm – Matrix Multiplication Inversions Median Closest Pair Integer Multiplication FFT

How to multiply 2 x 2 matrices with 7 multiplications Where: p1 = (b – d)(f + h) p2= (a + d)(e + h) p3= (a – c)(e + g) p4= (a + b)h p5= a(g – h) p6= d(f – e) p7= (c + d)e Multiply 2 x 2 Matrices: | r s | | a b| |e g| | t u| | c d| | f h| = r = p1 + p2 – p4 + p6 s = p4 + p5 t = p6 + p7 u = p2 - p3 + p5 - p7 Corrected version from AHU 1974

Strassen’s Algorithms Treat n x n matrices as 2 x 2 matrices of n/2 x n/2 submatrices Use Strassen’s trick to multiply 2 x 2 matrices with 7 multiplies Base case standard multiplication for single entries Recurrence: T(n) = 7 T(n/2) + cn2 Solution is O(7log n)= O(nlog 7) which is about O(n2.807)

Inversion Problem Let a1, . . . an be a permutation of 1 . . n (ai, aj) is an inversion if i < j and ai > aj Problem: given a permutation, count the number of inversions This can be done easily in O(n2) time Can we do better? 4, 6, 1, 7, 3, 2, 5

Application Counting inversions can be use to measure how close ranked preferences are People rank 20 movies, based on their rankings you cluster people who like that same type of movie

Counting Inversions Count inversions on lower half 11 12 4 1 7 2 3 15 9 5 16 8 6 13 10 14 Count inversions on lower half Count inversions on upper half Count the inversions between the halves

Count the Inversions 5 2 3 1 11 12 4 1 7 2 3 15 9 5 16 8 6 13 10 14 8 6 15 10 11 12 4 1 7 2 3 15 9 5 16 8 6 13 10 14 19 43 11 12 4 1 7 2 3 15 9 5 16 8 6 13 10 14

Problem – how do we count inversions between sub problems in O(n) time? Solution – Count inversions while merging 1 2 3 4 7 11 12 15 5 6 8 9 10 13 14 16 Standard merge algorithm – add to inversion count when an element is moved from the upper array to the solution

Use the merge algorithm to count inversions 1 4 11 12 2 3 7 15 5 8 9 16 6 10 13 14 Indicate the number of inversions for each element detected when merging

Inversions Counting inversions between two sorted lists O(1) per element to count inversions Algorithm summary Satisfies the “Standard recurrence” T(n) = 2 T(n/2) + cn x y z

Computing the Median Given n numbers, find the number of rank n/2 One approach is sorting Sort the elements, and choose the middle one Can you do better?

Problem generalization Selection, given n numbers and an integer k, find the k-th largest

Select(A, k) Select(A, k){ Choose element x from A S1 = {y in A | y < x} S2 = {y in A | y > x} S3 = {y in A | y = x} if (|S2| >= k) return Select(S2, k) else if (|S2| + |S3| >= k) return x else return Select(S1, k - |S2| - |S3|) } S1 S3 S2

Randomized Selection Choose the element at random Analysis can show that the algorithm has expected run time O(n)

Deterministic Selection What is the run time of select if we can guarantee that choose finds an x such that |S1| < 3n/4 and |S2| < 3n/4 in O(n) time

BFPRT Algorithm A very clever choose algorithm . . . Split into n/5 sets of size 5 M be the set of medians of these sets Let x be the median of M

BFPRT runtime |S1| < 3n/4, |S2| < 3n/4 Split into n/5 sets of size 5 M be the set of medians of these sets x be the median of M Construct S1 and S2 Recursive call in S1 or S2

BFPRT Recurrence T(n) <= T(3n/4) + T(n/5) + c n Prove that T(n) <= 20 c n