100% found this document useful (1 vote)
201 views

Advanced Algorithms Analysis and Design - CS702 Power Point Slides Lecture 13

This document discusses algorithms for finding the closest pair of points and maximal points using brute force and divide-and-conquer approaches. It begins by defining the closest pair problem in 2 dimensions and presenting a brute force algorithm that has O(n^2) runtime. It then improves this to O(nlogn) by sorting points first. The document generalizes these algorithms to 3 and higher dimensions, and also presents algorithms for finding maximal points in n dimensions using brute force in O(n^3) time. Finally, it briefly mentions using divide-and-conquer as a general approach.

Uploaded by

Sajjad Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
201 views

Advanced Algorithms Analysis and Design - CS702 Power Point Slides Lecture 13

This document discusses algorithms for finding the closest pair of points and maximal points using brute force and divide-and-conquer approaches. It begins by defining the closest pair problem in 2 dimensions and presenting a brute force algorithm that has O(n^2) runtime. It then improves this to O(nlogn) by sorting points first. The document generalizes these algorithms to 3 and higher dimensions, and also presents algorithms for finding maximal points in n dimensions using brute force in O(n^3) time. Finally, it briefly mentions using divide-and-conquer as a general approach.

Uploaded by

Sajjad Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Advanced Algorithms Analysis and

Design

By

Dr. Nazir Ahmad Zafar

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Lecture No. 13

Designing Algorithms using


Brute Force and
Divide & Conquer Approaches

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Today Covered
Brute Force
• Finding closest pair in 2-D
• Improved version finding closest pair in 2-D
• Generalization in 3-D and then n-D
• Finding maximal points in n-D
Divide and Conquer
• A General Divide and Conquer approach
• Merge Sort algorithm
• Finding Maxima in 1-D, and 2-D
• Finding Closest Pair in 2-D

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


The Closest Pair Problem

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Finding Closest Pair in 2-D
Problem
The closest pair problem is defined as follows:
• Given a set of n points, determine the two points
that are closest to each other in terms of distance.
Furthermore, if there are more than one pair of
points with the closest distance, all such pairs
should be identified.
Input :
is a set of n points
Output
• is a pair of points closest to each other,
• there can be more then one such pairs

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Definition: Closest Pair
Distance
• In mathematics, particular in geometry, distance
on a given set M is a function d: M × M → R,
where R denotes the set of real numbers, that
satisfies the following conditions:

1. d(x, y) ≥ 0,
2. d(x, y) = 0 if and only if x = y.
3. Symmetric i.e.
d(x, y) = d(y, x).
4. Triangle inequality:
d(x, z) ≤ d(x, y) + d(y, z).
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Finding Closest Pair in 2-D
Closest Pair Problem in 2-D
• A point in 2-D is an ordered pair of values (x, y).
• The Euclidean distance between two points
Pi = (xi, yi) and Pj = (xj, yj) is
d(pi, pj) = sqr((xi − xj)2 + (yi − yj)2)
• The closest-pair problem is finding the two closest
points in a set of n points.
• The brute force algorithm checks every pair of points.
• Assumption: We can avoid computing square roots by
using squared distance.
– This assumption will not loose correctness of the problem.

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Brute Force Approach: Finding Closest Pair in 2-D
ClosestPairBF(P)
1. mind  ∞ Time Complexity
2. for i  1 to n n n
3. do   c
4. for j  1 to n i 1 j1
5. if i  j n
6. do   cn
7. d  ((xi − xj)2 + (yi − yj)2) i 1
8. if d < mind then
8. mind  d  cn 2

9. mini  i
10.minj  j  ( n ) 2

11.return mind, p(mini, minj)


Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Improved Version
(The Closest Pair Problem)

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Improved Version: Finding Closest Pair in 2-D
ClosestPairBF(P) Time Complexity
n 1
1. mind  ∞
n
  c
2. for i  1 to n − 1 i 1 ji 1
n 1
3. do   c(n  i )
4. for j  i + 1 to n i 1
n 1 n 1
5. do  c ( n   i )
6. d  ((xi − xj)2 + (yi − yj)2) i 1 i 1

(n  1)n
7. if d < mind then  cn(n  1)  c
2
8. mind  d 2
9. mini  i
n n
 c(n  n   )
2

10.minj  j 2 2
n2 n
11.return mind, p(mini, minj)  c (  )  ( n 2 )
2 2
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
The Closest Pair Problem 3-D

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Finding Closest Pair in 3-D
ClosestPairBF(P) Time Complexity
1. mind  ∞ n 1 n
2. for i  1 to n − 1  
i 1 j i 1
c
3. do
n 1
4. for j  i + 1 to n
5. do

 c(n  i )
i 1

6. d  ((xi − xj)2 + (yi − yj)2 + (zi − zj)2)


7. if d < minn then n 1 n 1
8. mind  d  c ( n   i )
9. mini  i i 1 i 1

10.minj  j  ( n 2 )
11.return mind, p(mini), p(minj)
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
The Closest Pair Problem n-D

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Finding Closest Pair in n-D
ClosestPairBF(P) Time Complexity
1. mind  ∞ n 1 n
2. for i  1 to n − 1  
i 1 ji 1
cn
3. do
n 1
4. for j  i + 1 to n
5. do

 cn(n  i )
i 1

6. d  ((xi1 − xj1)2 + (xi2 − xj2)2 + . . .+(xin − xjn)2)


7. if d < minn then n 1 n 1

8. mind  d  c( n 2   in)


i 1 i 1
9. mini  i
10.minj  j  ( n 3 )
11.return mind, p(mini), p(minj)
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Finding Maximal in n-dimension

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Maximal Points
• Maximal Points in 2-D
A point p is said to be dominated by q if
p.x ≤ q.x and p.y ≤ q.y
A point p is said to be maximal if
p.x > q.x OR p.y > q.y
• Maximal Points in n-D
A point p is said to be dominated by q if
p.xi ≤ q.xi  i = 1,. . ., n
A point p is said to be maximal if
 i = 1,. . ., n, p.xi > q.xi
A point is said to be maximal if it is not
dominated by any other point.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Example: Maximal Points in 2-Dimension
11
(4,10)
10
9 (2,8) (8,8)
8
7
(7,6)
6
5
(4,4) (11,4)
4 (1,3)
3
(5,2)
2 (9,1)
1

1 2 3 4 5 6 7 8 9 10 11 12
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Problem Statement
Problem Statement:
Given a set of m points, P = {p1, p2, . . . , pm}, in n-
dimension. Our objective is to compute a set of
maximal points i.e. set of points which are not
dominated by any one in the given list.

Mathematical Description:
Maximal Points =

{ p  P |  q  {p1, . . . , pm}, q  p,  i  {1,. . . , n} &


p.xi ≥ q.xj}

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Brute Force Algorithm in n-dimension
MAXIMAL-POINTS (int n, Point P[1. . . m])
0 A = ;
1 for i 1 to m \\ m used for number of points
2 do maximal  true
3 for j  1 to m
4 do
5 if (i  j) &
6 for k  1 to n \\ n stands for dimension
7 do
8 P[i].x[k]  P[j].x[k]
9 then maximal  false; break
10 if maximal
11 then A = A  P[i]
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Conclusion
• Designing Algorithms using Brute Force approach is
discussed
• For Brute Force, formally, the output of any sorting
algorithm must satisfy the following two conditions:
– Output is in decreasing/increasing order and
– Output is a permutation, or reordering, of input.
• Algorithms computing maximal points can be
considered as generalization of sorting algorithms
• Maximal points are useful in Computer Sciences
and Mathematics in which at least one component
of every point is dominated over all points.
• In fact we put elements in a certain order

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

You might also like