0% found this document useful (0 votes)
8 views7 pages

Lab 03

Đề ôn tham khảo lab3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Lab 03

Đề ôn tham khảo lab3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Code: TT/P.

KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

SUBJECT: DESIGN AND ANALYSIS OF ALGORITHMS

CODE: 503040

Duration: 150 minutes

Allowed to use materials.

LAB 03: Brute-force Algorithms


I. Objectives
Understand the properties of brute-force algorithm design technique
Be able to design, implement, and analyze brute-force algorithms solving common problems.
II. Properties of brute-force algorithm design technique
Following Brute-force approach means we try to solve problems without worrying about the cost
(running time, space). We directly solve the problem based on its statements, definitions of
related concepts.
Brute-force approach provides a correct (usually ineffective) solution.
This type of algorithms is easy to understand and easy to implement as it is close to
intuition.
III. Examples of problem solving with brute-force technique
1. Example 1
Factorial of a positive integer is defined as follows:
n !=
{ 1 if n=1
n∗( n−1 )∗…∗1 if n> 1
a. Design a brute-force algorithm (presented in Python) to solve the problem
b. Analyze the complexity of the proposed algorithm.

Python code

Thien Nguyen PAGE 1


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

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.

A program to find the closest pair of points

Thien Nguyen PAGE 2


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

The output of the program: [[5.0, 1.0], [3.0, 4.0]]


Analysis
Function: squared_distance:
1. Input size is the number of dimensions of points (d)
2. Basic operation is multiplication on line 13
3. There is no worst case
4. T ( d )=d
Function closest_pair:
1. Input size is a tuple of n , d , where n- the number of points, d – number of dimensions.
2. The most time-consuming part is the call to squared_distance() on line 25.
3. There is no worst case

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

Thien Nguyen PAGE 3


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

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

More challenging problems


d. Nearest pair (closest pair)
In the Space with dimension d , 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. You are required to find the closest pair.
e. Convex hull
A shape S is convex if for any points P, Q in the shape, every point in the line joining P and Q is
also in S. That is, for all λ with 0 ≤ λ ≤ 1: λP + (1 − λ)Q ∈ S.
Convex Hull of a set of points (at least three). Smallest convex shape S which contains the

That is, for all convex S′, which contain the points, S ⊆ S′.
points.

Thien Nguyen PAGE 4


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

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:

Thien Nguyen PAGE 5


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

(Image from geeksforgeeks.org)

A testcase from geeksforgeeks.org

points[0]=new Point(0, 3);

points[1]=new Point(2, 3);

points[2]=new Point(1, 1);

points[3]=new Point(2, 1);

points[4]=new Point(3, 0);

points[5]=new Point(0, 0);

points[6]=new Point(3, 3);

convex hull is

(0, 3)

(0, 0)

(3, 0)

(3, 3)

f. Travelling Salesman Problem


Input a weighted graph G=(V , E)(undirected). Find a simple circuit which goes through all the
vertices and has minimum weight.

Thien Nguyen PAGE 6


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

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).

Thien Nguyen PAGE 7

You might also like