Aaa 7
Aaa 7
Today Covered
3,0,6,1,5,2 0,1,2,3,5,6
Sorting Algorithm
Sorting Algorithm: Brute Force Approach
Sort the array [2, 4, 1, 3] in increasing order
s1 = [4,3,2,1], s2 = [4,3,1,2], s3 = [4,1,2,3]
s4 = [4,2,3,1], s5 = [4,1,3,2], s6 = [4,2,1,3]
s7 = [3,4,2,1], s8 = [3,4,1,2], s9 = [3,1,2,4]
s10 = [3,2,4,1], s11 = [3,1,4,2], s12 = [3,2,1,4]
s13 = [2,3,4,1], s14 = [2,3,1,4], s15 = [2,1,4,3]
s16 = [2,4,3,1], s17 = [2,1,3,4], s18 = [2,4,1,3]
s19 = [1,3,2,4], s20 = [1,3,1,4], s21 = [1,4,2,3]
s22 = [1,2,3,4], s23 = [1,4,3,2], s24 = [1,2,4,3]
There are 4! = 24 number of permutations.
For n number of elements there will be n! number of
permutations. Hence cost of order n! for sorting.
Generating Permutations
Permute (i) \\initial call Permute(1)
if i == N
output A[N]
else
for j = i to N do
swap(A[i], A[j])
permute(i+1)
swap(A[i], A[j])
• There are 4! = 24 number of permutations.
• For n number of elements there will be n! number of
permutations. Hence cost of order n! for sorting.
Generating Permutations
Theorem
• Prove, by mathematical induction, that computational
cost of generating permutations is n!.
6!
Proof 6*5
5!*6
Example:
item weight value knapsack capacity W = 16
1 2 20
2 5 30
3 10 50
4 5 10
0-1 Knapsack Problem Example
Subset Total weight Total value
1. 0 0 # W V
2. {1} 2 20 1 2 20
3. {2} 5 30 2 5 30
4. {3} 10 50 3 10 50
5. {4} 5 10 4 5 10
6. {1,2} 7 50
7. {1,3} 12 70
8. {1,4} 7 30
9. {2,3} 15 80
10. {2,4} 10 40
11. {3,4} 15 60
12. {1,2,3} 17 not feasible
13. {1,2,4} 12 60
14. {1,3,4} 17 not feasible
15. {2,3,4} 20 not feasible
16. {1,2,3,4} 22 not feasible
0-1 Knapsack Algorithm
Knapsack-BF (n, V, W, C)
Compute all subsets, s, of S = {1, 2, 3, 4}
forall s S
weight = Compute sum of weights of these items
if weight > C, not feasible
new solution = Compute sum of values of these items
solution = solution {new solution}
Return maximum of solution
0-1 Knapsack Algorithm Analysis
Approach
• In brute force algorithm, we go through all
combinations and find the one with maximum
value and with total weight less or equal to W = 16
Complexity
• Cost of computing subsets O(2n) for n elements
• Cost of computing weight = O(2n)
• Cost of computing values = O(2n)
• Total cost in worst case: O(2n)
The Closest Pair Problem
Finding Closest Pair
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
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).
Finding Closest Pair
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.
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 < minn then
8. mind d = cn 2
9. mini i
10.minj j = ( n ) 2
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 )
i =1 i =1
6. d ((xi − xj)2 + (yi − yj)2)
(n − 1)n
7. if d < minn then = cn(n − 1) − c
2
8. mind d n2 n
9. mini i = c(n − n − + )
2
2 2
10.minj j n2 n
11.return mind, p(mini, minj) = c ( − ) = ( n 2 )
2 2
Brute Force Approach: 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
10.minj j = ( n 2 )
11.return mind, p(mini), p(minj)
Brute Force Approach: Finding Closest Pair in n-D
ClosestPairBF(P)
Time Complexity
1. mind ∞
n −1 n
2. for i 1 to n − 1
3. do
=
i =1 j= i +1
cn
4. for j i + 1 to n n −1
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)
9. mini i i =1 i =1
10.minj j = ( n 3 )
11.return mind, p(mini), p(minj)
Finding Maximal in n-dimension
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.
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
Example: Buying a Car
Suppose we want to buy a car which is
– Fastest and
– Cheapest
• Fast cars are expensive. We want cheapest.
• We can’t decide which one is more important
– Speed or
– Price.
• Of course fast and cheap dominates slow and
expensive car.
• So, given a collection of cars, we want the car
which is not dominated by any other.
Example: Buying a Car
Formal Problem: Problem can be modeled as:
• For each car C, we define C (x, y) where
x = speed of car and
y = price of car
• This problem can not be solved using maximal
point algorithm.
Redefine Problem:
• For each car C’, we define C’ (x’, y’) where
x’ = speed of car and
y’ = negation of car price
• This problem is reduced to designing maximal
point algorithm
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 =