Closest-Pair Problem: Divide and Conquer

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
Closest Pair Given a set S = {p1, p2,..., pn} of n points in the plane find the two points of S whose distance is the smallest. Images in this presentation.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 6 Divide and Conquer  Introduction  Binary Search  Mergesort  The Divide and Conquer Paradigm  Quicksort  Multiplication of Large Integers.
Divide-and-Conquer Dr. Yingwu Zhu P65-74, p83-88, p93-96, p
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
Reminder: Closest-Pair Problem Given a collection P of n points in  × , obtain the two points p1 and p2 such that their distance is less or equal that.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer.
Divide and Conquer. Divide and Conquer is a technique for designing the algorithms that consists of decomposing the instance to be solved into a number.
Part 5. Computational Complexity (3)
Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2004 Simonas Šaltenis E1-215b
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Wednesday, 11/25/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/25/02  QUESTIONS??  Today:  More on sorting. Advanced sorting algorithms.  Complexity:
Lecture 33 CSE 331 Nov 20, Homeworks Submit HW 9 by 1:10PM HW 8 solutions at the end of the lecture.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
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.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Quick Sort By: HMA. RECAP: Divide and Conquer Algorithms This term refers to recursive problem-solving strategies in which 2 cases are identified: A case.
1 Closest Pair of Points (from “Algorithm Design” by J.Kleinberg and E.Tardos) Closest pair. Given n points in the plane, find a pair with smallest Euclidean.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Bubble Sort Example
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. Outline Introduction Merge Sort Quick Sort Closest pair of points Large integer multiplication.
1 Parallel Sorting Algorithm. 2 Bitonic Sequence A bitonic sequence is defined as a list with no more than one LOCAL MAXIMUM and no more than one LOCAL.
CSE 340: Review (at last!) Measuring The Complexity Complexity is a function of the size of the input O() Ω() Θ() Complexity Analysis “same order” Order.
CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
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.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Mergesort and Quicksort Opening Discussion zWhat did we talk about last class? zDo you have any questions about assignment #4? Have you thought.
FASTER SORT using RECURSION : MERGE SORT COMP 103.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Advanced Algorithms Analysis and Design
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Lecture 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Divide and Conquer.
Algorithm Design Methods
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer / Closest Pair of Points Yin Tat Lee
Merge sort merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: Divide.
Unit-2 Divide and Conquer
Chapter 5 Divide and Conquer
Topic: Divide and Conquer
Richard Anderson Lecture 13 Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Chapter 4.
Divide-and-Conquer The most-well known algorithm design strategy:
CSE 373 Data Structures and Algorithms
CSC 380: Design and Analysis of Algorithms
Topic: Divide and Conquer
Merge sort merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: Divide.
Divide & Conquer Sorting
Richard Anderson Lecture 14 Divide and Conquer
Presentation transcript:

Closest-Pair Problem: Divide and Conquer Brute force approach requires comparing every point with every other point Given n points, we must perform 1 + 2 + 3 + … + n-2 + n-1 comparisons. Brute force  O(n2) The Divide and Conquer algorithm yields  O(n log n) Reminder: if n = 1,000,000 then n2 = 1,000,000,000,000 whereas n log n = 20,000,000

Closest-Pair Algorithm Given: A set of points in 2-D

Closest-Pair Algorithm Step 1: Sort the points in one D

Closest-Pair Algorithm Lets sort based on the X-axis O(n log n) using quicksort or mergesort 4 9 13 6 2 14 11 3 7 10 5 1 8 12

Closest-Pair Algorithm Step 2: Split the points, i.e., Draw a line at the mid-point between 7 and 8 4 9 13 6 2 14 11 3 7 10 5 1 8 12 Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm Advantage: Normally, we’d have to compare each of the 14 points with every other point. (n-1)n/2 = 13*14/2 = 91 comparisons 4 9 13 6 2 14 11 3 7 10 5 1 8 12 Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm Advantage: Now, we have two sub-problems of half the size. Thus, we have to do 6*7/2 comparisons twice, which is 42 comparisons solution d = min(d1, d2) 4 9 d1 13 6 2 d2 14 11 3 7 10 5 1 8 12 Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm Advantage: With just one split we cut the number of comparisons in half. Obviously, we gain an even greater advantage if we split the sub-problems. d = min(d1, d2) 4 9 d1 13 6 2 d2 14 11 3 7 10 5 1 8 12 Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm Problem: However, what if the closest two points are each from different sub-problems? 4 9 d1 13 6 2 d2 14 11 3 7 10 5 1 8 12 Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm Here is an example where we have to compare points from sub-problem 1 to the points in sub-problem 2. 4 d1 13 6 2 d2 9 14 11 7 3 10 8 5 1 12 Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm However, we only have to compare points inside the following “strip.” d = min(d1, d2) 4 9 d1 d d 13 6 2 d2 14 11 3 7 10 5 1 8 12 Sub-Problem 1 Sub-Problem 2

Closest-Pair Algorithm Step 3: But, we can continue the advantage by splitting the sub-problems. 4 9 13 6 2 14 11 3 7 10 5 1 8 12

Closest-Pair Algorithm Step 3: In fact we can continue to split until each sub-problem is trivial, i.e., takes one comparison. 4 9 13 6 2 14 11 3 7 10 5 1 8 12

Closest-Pair Algorithm Finally: The solution to each sub-problem is combined until the final solution is obtained 4 9 13 6 2 14 11 3 7 10 5 1 8 12

Closest-Pair Algorithm Finally: On the last step the ‘strip’ will likely be very small. Thus, combining the two largest sub-problems won’t require much work. 4 9 13 6 2 14 11 3 7 10 5 1 8 12

Closest-Pair Algorithm In this example, it takes 22 comparisons to find the closets-pair. The brute force algorithm would have taken 91 comparisons. But, the real advantage occurs when there are millions of points. 4 9 13 6 2 14 11 3 7 10 5 1 8 12

Closest-Pair Problem: Divide and Conquer Here is another animation: http://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairApplet/ClosestPairApplet.html