Lecture 5 Divide and Conquer: closest pair of points and numbers multiplication - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Lecture 5 Divide and Conquer: closest pair of points and numbers multiplication

Description:

Divide and conquer - closest points algorithm in time O(n log n) ... Solved Exercise 1 from the textbook, chapter 5 'Divide and Conquer' ... – PowerPoint PPT presentation

Number of Views:469
Avg rating:3.0/5.0
Slides: 14
Provided by: cscL6
Category:

less

Transcript and Presenter's Notes

Title: Lecture 5 Divide and Conquer: closest pair of points and numbers multiplication


1
Lecture 5Divide and Conquer closest pair of
points andnumbers multiplication
  • COMP 523 Advanced Algorithmic Techniques
  • Lecturer Dariusz Kowalski

2
Overview
  • Previous lecture
  • Sorting algorithms in (expected) time O(n log n)
  • Merge Sort
  • Quick Sort and randomisation
  • This lecture
  • Another kind of merging closest pair of points
  • Beyond ?(n log n) numbers multiplication

3
Problem
  • Closest pair of points
  • Input n points on the plane
  • Output two points having the closest distance
  • Remarks
  • Exhaustive search algorithm gives time O(n2)
  • Similar approach gives ALL pairs of closest points

4
Solution
  • Preprocessing
  • Sort points according to the first coordinate
    (obtain horizontal list H) and according the
    second coordinate (obtain vertical list V)
  • Main Algorithm
  • Partition list H input into halves in linear time
    (draw vertical line L containing medium point)
  • Solve the problem for each half of the input
    separately, obtaining two pairs of closest
    points let ? be the smallest distance from the
    obtained ones
  • Find if there is a pair which has distance
    smaller than ? - if yes then find the smallest
    distance pair

5
Main difficulty
  • Find if there is a pair which has distance
    smaller than ? -
  • if yes then find the smallest distance pair
  • How to do it in linear time?

?
?
?
6
Closest pair in the strip
  • 1. Find a sublist P of V containing all points
    with a distance at most ? from the line L - in
    linear time
  • 2. For each element in P check its distance to
    the next 8 elements and remember the closest pair
    ever found

?
?
?
L
7
Why to check only 8 points ahead?
  • 1. Partition the strip into squares of length ?/2
    as shown in the picture.
  • 2. Each square contains at most 1 point - by
    definition of ?.
  • 3. If there are at least 2 squares between points
    then they can not be the closest points.
  • 4. There are at most 8 squares to check.

?
?/2
?/2
?/2
?/2
?/2
?/2
L
8
Time complexity
  • Preprocessing sorting in time O(n log n)
  • Main Algorithm recursion in time O(n log n)
  • T(n) ? 2 T(n/2) 9.5n
  • T(2) 1

9
Beyond ?(n log n) - integers multiplication
  • Input two integers x and y, each consisting of n
    bits
  • Output multiply these integers
  • Naïve Algorithm
  • Multiply each bit i of x by y, add (i-1) zeroes
    at the end
  • Add the numbers
  • Time ?(n2) of bit operations

10
Divide and Conquer approach
  • Let x x1 2n/2 x2 and y y1 2n/2 y2.
  • Let p x1 x2 and q y1 y2. Then
  • x y (x1 2n/2 x2) (y1 2n/2 y2)
  • x1 y1 2n/2 (x2 y1 x1 y2) 2n x2 y2
  • x1 y1 2n/2 (pq - x1 y1- x2 y2) 2n x2 y2
  • Algorithm
  • Compute p and q in linear time
  • Compute recursively x1 y1 , x2 y2 , pq
  • Perform x1 y1 2n/2 (pq - x1 y1- x2 y2) 2n x2
    y2 in linear time

11
Time complexity
  • T(n) ? 3T(n/2) c n
  • T(1) ? c
  • Solution T(n) ? d nlog 3, for some constant d ?
    3c
  • Proof For n 1 straightforward. General case
  • T(n) ? 3T(n/2) c n ? 9T(n/4) 3c(n/2) c n
  • ? c n (1 3/2 (3/2)2 (3/2)log n)
  • c n 2(3/2)log n 1 ? 3c n nlog 3 - 1
  • 3c nlog 3 O(n1.59)

12
Conclusions
  • Divide and conquer - closest points algorithm in
    time O(n log n)
  • Beyond ?(n log n) O(nlog 3) time algorithm for
    integer multiplication

13
Textbook and Exercises
  • Read Chapter 5 from Section 5.4
  • How to add two n-bits integers in linear time?
  • Solved Exercise 1 from the textbook, chapter 5
    Divide and Conquer
  • Exercises 1, 6, 7 from the textbook, chapter 5
    Divide and Conquer
Write a Comment
User Comments (0)
About PowerShow.com