Lab 03
Lab 03
KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......
CODE: 503040
Python code
Analysis
1. Basic operation is multiplication on line 8
2. The worst case is the average case too, because the algorithm runs the same in all situations.
3. The total number of basic operations is:
T ( n )=n
4. So the complexity of the algorithm is Θ(n)
2. Example 2
In the space with d dimension, the Euclidean distance between two points A ( x 1 , x 2 ,… , x d ) and
B( y 1 , y2 , … , y d ) is determined by the formula:
D ( A , B ) =√ (x 1− y 1)2 +( x 2− y 2)2 +…+(x d − y d )2
Suppose there aren points in the space.
Propose a brute-force algorithm to find the closest pair of points.
a. Design a brute-force algorithm (presented in Swift) to solve the problem
b. Analyze the complexity of the proposed algorithm.
4. Count:
when i=0 , ( n−1 ) × d ops
when i=1, ( n−2 ) × d ops
…
when i=n−2, 1 ×d ops
T ( n , d ) =d (1+…+ n−2+n−1)
( n−1 ) n
T ( n , d ) =d
2
2
T ( n , d ) ∈Θ( d n )
IV. Exercises
For each of the problems in this section:
1) Design a brute-force algorithm (presented in Python) to solve the problem.
2) Analyze the complexity of the proposed algorithm.
Warm-up problems
a. Exponential power
Exponential power of a positive integera n (a , n ≥ 0) is defined as follows:
a=
n
{a∗a∗…∗a
1 if n=0
if n> 0
b. Combination
Combination is determined by the following formula
kn!
C n=
k ! ( n−k ) !
2 6! 6!
(example) C 6= = =15
2! ( 6−2 ) ! 2! 4 !
c. Matrix multiplication
Multiply two square matrices A [n × n] and B[n ×n] of size n
n
C ij =∑ A ik × B kj , for i=1 , … , n∧ j=1 , … ,n
k=1
That is, for all convex S′, which contain the points, S ⊆ S′.
points.
Theorem: For any finite set of points, Convex Hull is a convex polygon, and its vertices are
included in the set of points given
Hence, we just need to find the extreme pairs of points. The polygon formed using the line
segments joining these pair of points will give the convex hull.
Extreme: All other points are on the same side of the line joint the pair of points.
For ease, we assume no triplets of points are colinear (at least not in the boundary of the convex
hull).
Line passing through (x 1 , y 1 ), (x2 , y 2)
y 2− y 1
y− y 1= ( x−x 1 )
x2− x1
Rearrange to put it in form ax +by + c=0.
(x 2−x 1)( y− y 1)=( y 2− y 1)(x−x 1)
x 2 y −x 2 y 1−x 1 y + x 1 y 1= y 2 x− y 2 x 1− y 1 x + x 1 y1
x 2 y −x 2 y 1−x 1 y + x 1 y 1− y 2 x + y 2 x1 + y 1 x−x 1 y 1=0
( y 1− y 2) x + ( x 2−x 1 ) y+ x 1 y 1+ x 1 y 2−x 2 y 1=0
a= y 1− y 2
b=x 2−x 1
c=x 1 y 2−x 2 y 1
Distance of a point ( x 0 , y 0 )from a line ax +by + c=0 is given by
a x 0+ b y 0 +c
√ a2 + b2
Count the number of times line equation is evaluated, and distance is calculated.
Hints:
convex hull is
(0, 3)
(0, 0)
(3, 0)
(3, 3)
Hints:
Brute Force/Exhaustive approach:
For each possible order of the vertices (there are n ! of them!):
Find the weight of the circuit formed when the vertices are traversed in that order. Then find the
one with minimal weight.
g. Knapsack Problem
A set of n items, each having weight and value W [0 :n−1], V [0: n−1], and a knapsack size K .
Find a subset S ⊆ { 0 , 1, . . . ,n−1 }, such that Σ i ∈S W ( i ) ≤ K and Σ i ∈S V (i)is maximised.
Hints:
Exhaustive approach: Consider all possible subsets of {0 ,1 , ... ,n−1 } (there are 2n of them!).
For each subset as above, check if Σ i ∈S W (i)≤ K , and if so this is a feasible set.
Among all feasible sets, choose the one which maximizes Σ i ∈ S V (i).