Rank Rank of an element is its position in ascending key order. [2,6,7,8,10,15,18,20,25,30,35,40] rank(2) = 0 rank(15) = 5 rank(20) = 7.

Slides:



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

A simple example finding the maximum of a set S of n numbers.
Prune-and-Search Method
Introduction to Algorithms Jiafen Liu Sept
1 Today’s Material Medians & Order Statistics – Ch. 9.
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.
Divide-and-Conquer The most-well known algorithm design strategy:
The 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.
Chapter 6 Divide and Conquer  Introduction  Binary Search  Mergesort  The Divide and Conquer Paradigm  Quicksort  Multiplication of Large Integers.
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.
CS4413 Divide-and-Conquer
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
Chapter 3: Divide and Conquer
Theory of Algorithms: Divide and Conquer
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Foundations of Algorithms, Fourth Edition
Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2004 Simonas Šaltenis E1-215b
1 Closest Points A famous algorithmic problem... Given a set of points in the plane (cities in the U.S., transistors on a circuit board, computers on a.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Median/Order Statistics Algorithms
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
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.
The Selection Problem. The selection problem Input: A set S of n elements Output: The k-th smallest element of S The median problem: to find the -th smallest.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Prune-and-search Strategy
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Administrivia, Lecture 5 HW #2 was assigned on Sunday, January 20. It is due on Thursday, January 31. –Please use the correct edition of the textbook!
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Lecture 6 Divide and Conquer for Nearest Neighbor Problem Shang-Hua Teng.
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 Rearrange n elements into ascending order. 7, 3, 6, 2, 1  1, 2, 3, 6, 7.
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.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
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 Sorting Small instance.  n
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.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
5 -1 Chapter 5 The Divide-and-Conquer Strategy A simple example finding the maximum of a set S of n numbers.
© The McGraw-Hill Companies, Inc., Chapter 6 Prune-and-Search Strategy.
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.
1 Prune-and-Search Method 2012/10/30. A simple example: Binary search sorted sequence : (search 9) step 1  step 2  step 3  Binary search.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
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 A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
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.
CS 361 – Chapter 11 Divide and Conquer ! Examples: –Merge sort √ –Ranking inversions –Matrix multiplication –Closest pair of points Master theorem –A shortcut.
Divide and Conquer Sorting
Rank Rank of an element is its position in ascending key order.
Chapter 4 Divide-and-Conquer
Divide-And-Conquer-And-Combine
Rank Rank of an element is its position in ascending key order.
Topic: Divide and Conquer
Divide-And-Conquer-And-Combine
Topic: Divide and Conquer
Given a list of n  8 integers, what is the runtime bound on the optimal algorithm that sorts the first eight? O(1) O(log n) O(n) O(n log n) O(n2)
Divide and Conquer Merge sort and quick sort Binary search
Chapter 7 : Sorting 교수 : 이상환 강의실 : 113,118호, 324호 연구실 : 과학관 204호
Presentation transcript:

Rank Rank of an element is its position in ascending key order. [2,6,7,8,10,15,18,20,25,30,35,40] rank(2) = 0 rank(15) = 5 rank(20) = 7

Selection Problem Given n unsorted elements, determine the k’th smallest element. That is, determine the element whose rank is k-1. Applications  Median score on a test. k = ceil(n/2).  Median salary of Computer Scientists.  Identify people whose salary is in the bottom 10%. First find salary at the 10% rank.

Selection By Sorting Sort the n elements. Pick up the element with desired rank. O(n log n) time.

Divide-And-Conquer Selection Small instance has n <= 1. Selection is easy. When n > 1, select a pivot element from out of the n elements. Partition the n elements into 3 groups left, middle and right as is done in quick sort. The rank of the pivot is the location of the pivot following the partitioning. If k-1 = rank(pivot), pivot is the desired element. If k-1 < rank(pivot), determine the k’th smallest element in left. If k-1 > rank(pivot), determine the (k-rank(pivot)-1)’th smallest element in right.

D&C Selection Example Use 3 as the pivot and partition. rank(pivot) = 5. So pivot is the 6’th smallest element. Find kth element of: a a

D&C Selection Example If k = 6 (k-1 = rank(pivot)), pivot is the element we seek. If k < 6 (k-1 < rank(pivot)), find k’th smallest element in left partition. If k > 6 (k-1 > rank(pivot)), find (k- rank(pivot)-1)’th smallest element in right partition a

Time Complexity Worst case arises when the partition to be searched always has all but the pivot.  O(n 2 ) Expected performance is O(n). Worst case becomes O(n) when the pivot is chosen carefully.  Partition into n/9 groups with 9 elements each (last group may have a few more)  Find the median element in each group.  pivot is the median of the group medians.  This median is found using select recursively.

Closest Pair Of Points Given n points in 2D, find the pair that are closest.

Applications We plan to drill holes in a metal sheet. If the holes are too close, the sheet will tear during drilling. Verify that no two holes are closer than a threshold distance (e.g., holes are at least 1 inch apart).

Air Traffic Control 3D -- Locations of airplanes flying in the neighborhood of a busy airport are known. Want to be sure that no two planes get closer than a given threshold distance.

Simple Solution For each of the n(n-1)/2 pairs of points, determine the distance between the points in the pair. Determine the pair with the minimum distance. O(n 2 ) time.

Divide-And-Conquer Solution When n is small, use simple solution. When n is large  Divide the point set into two roughly equal parts A and B.  Determine the closest pair of points in A.  Determine the closest pair of points in B.  Determine the closest pair of points such that one point is in A and the other in B.  From the three closest pairs computed, select the one with least distance.

Example Divide so that points in A have x-coordinate <= that of points in B. AB

Example Find closest pair in A. AB Let d 1 be the distance between the points in this pair. d1d1

Example Find closest pair in B. AB d1d1 Let d 2 be the distance between the points in this pair. d2d2

Example Let d = min{d 1, d 2 }. AB d1d1 Is there a pair with one point in A, the other in B and distance < d? d2d2

Example Candidates lie within d of the dividing line. AB Call these regions R A and R B, respectively. RARA RBRB

Example Let q be a point in R A. AB RARA RBRB q q need be paired only with those points in R B that are within d of q.y.

Example Points that are to be paired with q are in a d x 2d rectangle of R B (comparing region of q). AB RARA RBRB q Points in this rectangle are at least d apart. d 2d

Example AB RARA RBRB q So the comparing region of q has at most 6 points. So number of pairs to check is <= 6| R A | = O(n). d 2d

Time Complexity Create a sorted by x-coordinate list of points.  O(n log n) time. Create a sorted by y-coordinate list of points.  O(n log n) time. Using these two lists, the required pairs of points from R A and R B can be constructed in O(n) time. Let n < 4 define a small instance.

Time Complexity Let t(n) be the time to find the closest pair (excluding the time to create the two sorted lists). t(n) = c, n < 4, where c is a constant. When n >= 4, t(n) = t(ceil(n/2)) + t(floor(n/2)) + an, where a is a constant. To solve the recurrence, assume n is a power of 2 and use repeated substitution. t(n) = O(n log n).