Divide and Conquer Mergesort Quicksort Binary Search Selection

Slides:



Advertisements
Similar presentations
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.
Advertisements

Introduction to Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1 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.
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.
CS4413 Divide-and-Conquer
Algorithm Design Techniques: Divide and Conquer. Introduction Divide and conquer – algorithm makes at least two recursive calls Subproblems should not.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Theory of Algorithms: Divide and Conquer
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Foundations of Algorithms, Fourth Edition
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
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.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
DIVIDE & CONQUR ALGORITHMS Often written as first as a recursive algorithm Master’s Theorem: T(n) = aT(n/b) + cn i, for some constant integer i, and constants.
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort.
Divide and Conquer Strategy
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
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.
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Chapter 4 Divide-and-Conquer
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull
Chapter 5 Divide & conquer
Randomized Algorithms
Chapter 4 Divide-and-Conquer
Divide and Conquer.
CSCE350 Algorithms and Data Structure
Algorithm Design Methods
Divide-and-Conquer The most-well known algorithm design strategy:
CSCE 411 Design and Analysis of Algorithms
Chapter 4: Divide and Conquer
Complexity Present sorting methods. Binary search. Other measures.
Divide-and-Conquer The most-well known algorithm design strategy:
Unit-2 Divide and Conquer
Randomized Algorithms
Topic: Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
CS 1114: Sorting and selection (part two)
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Topic: Divide and Conquer
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Lecture 15, Winter 2019 Closest Pair, Multiplication
Richard Anderson Lecture 14 Divide and Conquer
Introduction to Algorithms
CSC 380: Design and Analysis of Algorithms
The Selection Problem.
Richard Anderson Lecture 14, Winter 2019 Divide and Conquer
Richard Anderson Lecture 14 Divide and Conquer
Divide-and-Conquer Divide: the problem into subproblems
Presentation transcript:

Divide and Conquer Mergesort Quicksort Binary Search Selection Matrix Multiplication Convex Hull

Selection Find the kth smallest (largest) item in a list. Easy if it is the smallest item or largest. Hardest if towards the middle - median (?) How might we approach? Sort the list and find the kth element (complexity O(nlogn)) Or … better complexity with a similar method to quicksort.

Quicksort Partition Choose a pivot element. Move all items less of the pivot to the left. Move all items greater than the pivot to the right. Put the pivot in place, say position p. Quicksort: recursive call with left and right. Selection: recursive call with _______?

Selection(Find kth) Strategy Partition: places pivot element in position p in O(n) time. 1. Suppose k = p. Return pivot element. 2. Suppose k < p. Only left side to consider. 3. Suppose k > p. Only right side to consider.

Selection (A, left, right,k) 1. Pivot = ChoosePivot( A, left, right); 2. PivotPos = Partition (A, left, right, Pivot); 3. If (PivotPos == k) return A[k]; Else If (PivotPos > k) Selection (A, left, PivotPos-1,k); Else Selection (A,PivotPos+1,right,k);

Complexity of Selection Divide ChoosePivot = O(1). Partition = O(n) Combine = O(1) - no recombining. Number of Subproblems:1 Size of Subproblem  n/2 (hopefully). T(n) = n + T(n/2) = __________ (pg. 187)

Matrix Multiplication 1 2 3 -1 1 5 -2 4 10 0 1 2 X -2 0 1 = -2 2 3 1 4 -1 1 1 1 -10 0 8 for I = 1 to n for J = 1 to n for K = 1 to n C[I][J] += A[I][K] * B[K][J]

Divide Each Matrix Into 4 Matrices Divide and Conquer - #1 Divide Each Matrix Into 4 Matrices A12 B12 C12 A11 B11 C11 A21 A22 B21 B22 C21 C22 C11 = A11*B11 + A12*B21 ...

Matrix Mult: Divide and Conquer. C11 = A11 * B11 + A12 * B21 C12 = A12 * B12 + A12 * B22 C21 = A21 * B11 + A22 * B21 C22 = A21 * B12 + A22 * B22 Cost of Dividing = O(1) Cost of Combining = 4 Matrix Adds: O(n^2) Number of Subproblems = 8 Size(n/2)

Complexity of D-and-C(1) T(n) = 8T(n/2) + O(n^2). Complexity = _______________ RATS

Strassen Matrix Multiply P= (A11+A22) (B11 + B22 ) Q= (A21+A22) B11 R = A11 (B12 - B22) S = A22 ( B21 - B11) T = (A11 + A12) B22 U = (A21 - A11) (B11 + B12) V = (A12 - A22) (B21 - B22)

Strassen- cont. C11 = P + S - T + V C12 = R + T C21 = Q + S C22 = P + R - Q + U Divide = O(1), Combine = 18 add/sub O(n^2) Subproblems = 7 multiplies. Size = n/2 T(n) = 7 T (n/2) + 18n^2. ___________

Convex Hull A convex polygon containing all the points.

Algorithm #1 The points on the hull are the points that are not inside any triangle. For point p = 1 to n Try every group of 3 points and see if p is inside of the triangle. /* testing to see if a point is inside of a triangle can be done in constant time */ Complexity: O(n^4)

Quickhull Algorithm 1. Choose the points with smallest and largest x coordinates. (Points: p1, p2) 2. Divide the rest of the points into those in the upper part of the hull and those in the lower part. 3. Recursively do the following for each part:

Convex Hull (cont) 1. Find a point p3, that maximizes the triangular area (p1,p2,p3). It will be on the hull. 2. For each other point, determine if it is A) inside the triangle (disregard it). B) outside the left C) outside the right 3. Call recursively with left and right points

Convex Hull p3 p2 p1

Complex. Convex Hull Divide Part: (Given points p1, p2) for each other point p: find tri. area p1,p2,p p3 = maximum area point is p inside the triangle p1, p2, p3, or to the left or to the right. COMPLEXITY OF DIVIDE = O(n)

Convex Hull Complexity Divide Cost: O(n) Combine cost: O(1) Number of Subproblems: 2 Size of Subproblems: ??? Maybe n/2 Anyway: complexity similar to quicksort. O(nlogn) average case, probably.

Homework for Sept. 16 Implement: Finding the closest pair of points divide&conquer algorithm. (p1040-1044 3rd ed,p.957-961 2nd ed). Study the big picture of the algorithm. Understand the complexity of the algorithm. Be very careful to maintain the big oh in your implementation. Sloppy implementation can add more than a constant to the code.