Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 11.

Slides:



Advertisements
Similar presentations
Comments We consider in this topic a large class of related problems that deal with proximity of points in the plane. We will: 1.Define some proximity.
Advertisements

Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Advanced Algorithms Piyush Kumar (Lecture 12: Parallel Algorithms) Welcome to COT5405 Courtesy Baker 05.
Lecture 4 Divide and Conquer for Nearest Neighbor Problem
DIVIDE AND CONQUER. 2 Algorithmic Paradigms Greedy. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. 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.
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
CS4413 Divide-and-Conquer
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Algorithm Design Techniques: Divide and Conquer. Introduction Divide and conquer – algorithm makes at least two recursive calls Subproblems should not.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
1 Divide-and-Conquer Divide-and-conquer. n Break up problem into several parts. n Solve each part recursively. n Combine solutions to sub-problems into.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Spring 2015 Lecture 5: QuickSort & Selection
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
1 Lecture 8: Voronoi Diagram Computational Geometry Prof. Dr. Th. Ottmann Voronoi Diagrams Definition Characteristics Size and Storage Construction Use.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
Mergesort, Analysis of Algorithms Jon von Neumann and ENIAC (1945)
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Lecture 4: Divide and Conquer III: Other Applications and Examples Shang-Hua Teng.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Linear Time Selection These lecture slides are adapted from CLRS 10.3.
Closest Pair of Points Computational Geometry, WS 2006/07 Lecture 9, Part II Prof. Dr. Thomas Ottmann Khaireel A. Mohamed Algorithmen & Datenstrukturen,
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.
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.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 12.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURES Divide.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
File Organization and Processing Week 13 Divide and Conquer.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
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 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
CSE 421 Algorithms Lecture 15 Closest Pair, Multiplication.
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11.
CSCI 256 Data Structures and Algorithm Analysis Lecture 10 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Advanced Algorithms Analysis and Design
Chapter 2 Divide-and-Conquer algorithms
Chapter 5 Divide and Conquer
分治法.
Chapter 2 Divide-and-Conquer algorithms
Chapter 2 Divide-and-Conquer algorithms
CS 3343: Analysis of Algorithms
Rank Rank of an element is its position in ascending key order.
Divide and Conquer / Closest Pair of Points Yin Tat Lee
Chapter 5 Divide and Conquer
Richard Anderson Lecture 13 Divide and Conquer
Divide-and-Conquer.
Divide and Conquer / Closest Pair of Points Yin Tat Lee
CSE 373 Data Structures and Algorithms
CSCI 256 Data Structures and Algorithm Analysis Lecture 12
CSC 380: Design and Analysis of Algorithms
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)
Richard Anderson Lecture 13 Divide and Conquer
Presentation transcript:

Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 11

2 Divide-and-Conquer Divide-and-conquer. n Break up problem into several parts. n Solve each part recursively. n Combine solutions to sub-problems into overall solution. Most common usage. n Break up problem of size n into two equal parts of size ½n. n Solve two parts recursively. n Combine two solutions into overall solution in linear time. Consequence. n Brute force: n 2. n Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici. - Julius Caesar

5.1 Mergesort

Sorting. Given n elements, rearrange in ascending order. 4 Obvious sorting applications. List files in a directory. Organize an MP3 library. List names in a phone book. Display Google PageRank results. Problems become easier once sorted. Find the median. Find the closest pair. Binary search in a database. Identify statistical outliers. Find duplicates in a mailing list. Non-obvious sorting applications. Data compression. Computer graphics. Interval scheduling. Computational biology. Minimum spanning tree. Supply chain management. Simulate a system of particles. Book recommendations on Amazon. Load balancing on a parallel computer.... Sorting

5 Mergesort Mergesort. n Divide array into two halves. n Recursively sort each half. n Merge two halves to make sorted whole. merge sort divide ALGORITHMS ALGORITHMS AGLORHIMST AGHILMORST Jon von Neumann (1945) O(n) 2T(n/2) O(1)

6 Merging Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently? n Linear number of comparisons. n Use temporary array. Challenge for the bored. In-place merge. [Kronrud, 1969] AGLORHIMST AGHI using only a constant amount of extra storage

7 A Useful Recurrence Relation Def. T(n) = number of comparisons to mergesort an input of size n. Mergesort recurrence. Solution. T(n) = O(n log 2 n). Assorted proofs. We describe several ways to prove this recurrence. Initially we assume n is a power of 2 and replace  with =.

8 Proof by Recursion Tree T(n) T(n/2) T(n/4) T(2) n T(n / 2 k ) 2(n/2) 4(n/4) 2 k (n / 2 k ) n/2 (2)... log 2 n n log 2 n

9 Proof by Telescoping Claim. If T(n) satisfies this recurrence, then T(n) = n log 2 n. Pf. For n > 1: assumes n is a power of 2

10 Proof by Induction Claim. If T(n) satisfies this recurrence, then T(n) = n log 2 n. Pf. (by induction on n) n Base case: n = 1. n Inductive hypothesis: T(n) = n log 2 n. n Goal: show that T(2n) = 2n log 2 (2n). assumes n is a power of 2

11 Analysis of Mergesort Recurrence Claim. If T(n) satisfies the following recurrence, then T(n)  n  lg n . Pf. (by induction on n) n Base case: n = 1. n Define n 1 =  n / 2 , n 2 =  n / 2 . n Induction step: assume true for 1, 2,..., n–1. log 2 n

5.3 Counting Inversions

13 Music site tries to match your song preferences with others. n You rank n songs. n Music site consults database to find people with similar tastes. Similarity metric: number of inversions between two rankings. n My rank: 1, 2, …, n. n Your rank: a 1, a 2, …, a n. n Songs i and j inverted if i a j. Brute force: check all  (n 2 ) pairs i and j. You Me ABCDE Songs Counting Inversions Inversions 3-2, 4-2

14 Applications Applications. n Voting theory. n Collaborative filtering. n Measuring the "sortedness" of an array. n Sensitivity analysis of Google's ranking function. n Rank aggregation for meta-searching on the Web. n Nonparametric statistics (e.g., Kendall's Tau distance).

15 Counting Inversions: Divide-and-Conquer Divide-and-conquer

16 Counting Inversions: Divide-and-Conquer Divide-and-conquer. n Divide: separate list into two pieces Divide: O(1).

17 Counting Inversions: Divide-and-Conquer Divide-and-conquer. n Divide: separate list into two pieces. n Conquer: recursively count inversions in each half blue-blue inversions8 green-green inversions Divide: O(1). Conquer: 2T(n / 2) 5-4, 5-2, 4-2, 8-2, , 9-3, 9-7, 12-3, 12-7, 12-11, 11-3, 11-7

18 Counting Inversions: Divide-and-Conquer Divide-and-conquer. n Divide: separate list into two pieces. n Conquer: recursively count inversions in each half. n Combine: count inversions where a i and a j are in different halves, and return sum of three quantities blue-blue inversions8 green-green inversions Divide: O(1). Conquer: 2T(n / 2) Combine: ??? 9 blue-green inversions 5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7 Total = = 22.

19 13 blue-green inversions: Counting Inversions: Combine Combine: count blue-green inversions n Assume each half is sorted. n Count inversions where a i and a j are in different halves. n Merge two sorted halves into sorted whole. Count: O(n) Merge: O(n) to maintain sorted invariant

20 Counting Inversions: Implementation Pre-condition. [Merge-and-Count] A and B are sorted. Post-condition. [Sort-and-Count] L is sorted. Sort-and-Count(L) { if list L has one element return 0 and the list L Divide the list into two halves A and B (r A, A)  Sort-and-Count(A) (r B, B)  Sort-and-Count(B) (r B, L)  Merge-and-Count(A, B) return r = r A + r B + r and the sorted list L }

Q1: Finding modes You are given an array A with n entries; each entry is a distinct number. You are told that the sequence A[1],…,A[n] is unimodal. That is, for some index p between 1 and n, values in the array increase up to position p in A and then decrease the rest of the way up to position n. Give a O(log n)-time algorithm to find the “peak entry” of the array. 21

Q2: Significant inversions Let’s “relax” the inversion-counting problem a bit. Call a pair of numbers a i, a j a significant inversion if i 2 a j. Give an O(n log n) algorithm to count the number of significant inversions between two orderings. 22

5.4 Closest Pair of Points

24 Closest Pair of Points Closest pair. Given n points in the plane, find a pair with smallest Euclidean distance between them. Fundamental geometric primitive. n Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. n Special case of nearest neighbor, Euclidean MST, Voronoi. Brute force. Check all pairs of points p and q with  (n 2 ) comparisons. 1-D version. O(n log n) easy if points are on a line. Assumption. No two points have same x coordinate. to make presentation cleaner fast closest pair inspired fast algorithms for these problems

25 Closest Pair of Points: First Attempt Divide. Sub-divide region into 4 quadrants. L

26 Closest Pair of Points: First Attempt Divide. Sub-divide region into 4 quadrants. Obstacle. Impossible to ensure n/4 points in each piece. L

27 Closest Pair of Points Algorithm. n Divide: draw vertical line L so that roughly ½n points on each side. L

28 Closest Pair of Points Algorithm. n Divide: draw vertical line L so that roughly ½n points on each side. n Conquer: find closest pair in each side recursively L

29 Closest Pair of Points Algorithm. n Divide: draw vertical line L so that roughly ½n points on each side. n Conquer: find closest pair in each side recursively. n Combine: find closest pair with one point in each side. n Return best of 3 solutions L seems like  (n 2 )

30 Closest Pair of Points Find closest pair with one point in each side, assuming that distance <   = min(12, 21) L

31 Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . n Observation: only need to consider points within  of line L  L  = min(12, 21)

 Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . n Observation: only need to consider points within  of line L. n Sort points in 2  -strip by their y coordinate. L  = min(12, 21)

 Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . n Observation: only need to consider points within  of line L. n Sort points in 2  -strip by their y coordinate. n Only check distances of those within 11 positions in sorted list! L  = min(12, 21)

34 Closest Pair of Points Def. Let s i be the point in the 2  -strip, with the i th smallest y-coordinate. Claim. If |i – j|  12, then the distance between s i and s j is at least . Pf. n No two points lie in same ½  -by-½  box. n Two points at least 2 rows apart have distance  2(½  ). ▪ Fact. Still true if we replace 12 with 7.   ½½ 2 rows ½½ ½½ 39 i j

35 Closest Pair Algorithm Closest-Pair(p 1, …, p n ) { Compute separation line L such that half the points are on one side and half on the other side.  1 = Closest-Pair(left half)  2 = Closest-Pair(right half)  = min(  1,  2 ) Delete all points further than  from separation line L Sort remaining points by y-coordinate. Scan points in y-order and compare distance between each point and next 11 neighbors. If any of these distances is less than , update . return . } O(n log n) 2T(n / 2) O(n) O(n log n) O(n)

36 Closest Pair of Points: Analysis Running time. Q. Can we achieve O(n log n)? A. Yes. Don't sort points in strip from scratch each time. n Each recursive returns two lists: all points sorted by y coordinate, and all points sorted by x coordinate. n Sort by merging two pre-sorted lists.