You are on page 1of 38

Design and Analysis of Algorithms

Algorithms-II : CS345A

Lecture 4
• Divide and Conquer Paradigm - III
• Convex Hull
• Algorithm to multiply 2 polynomials

1
PROBLEM 3
Convex Hull

2
A Convex Polygon
Definition: A polygon is convex if for any two points belonging to the polygon,
the line segment joining them is inside the polygon.

3
Convex hull
Definition: Convex polygon of smallest area enclosing a set of points.

Problem: Design an efficient algorithm to compute the convex hull of .

Assumption: No three points are colinear.


4
Convex hull
Definition: Convex polygon of smallest area enclosing a set of points.

Problem: Design an efficient algorithm to compute the convex hull of .

Assumption: No three points are colinear.


5
Convex hull
Definition: Convex polygon of smallest area enclosing a set of points.

Problem: Design an efficient algorithm to compute the convex hull of .

Assumption: No three points are colinear.


6
Convex hull
Definition: Convex polygon of smallest area enclosing a set of points.

Problem: Design an efficient algorithm to compute the convex hull of .

Assumption: No three points are colinear.


7
Tool 1
Let .
Convex Hull of must be fully contained inside the convex hull of .

8
Tool 2

• Given a non-vertical line and a point =(), how to determine


whether lies above or below ?
time
𝑞=(𝑎1 , 𝑏1)

𝑦 =𝑚𝑥 +𝑐

𝑟 =(𝑎 2 , 𝑏2)

Corollary: Given two points and a line , it takes time to determine


whether they lie on the same side or different sides of .

9
Convex hull

10
Convex hull

11
Convex hull

12
Convex hull

13
Convex hull
O() time

Homework:
Prove the correctness of the algorithm.
14
DIVIDE AND CONQUER ALGORITHM

Convex Hull

15
The Divide Step

O() time

Left half set points points Right half set


16
Solving the subproblems recursively

2 T() time

Left half set points points Right half set


17
The Conquer step

Left half set points points Right half set


18
The Conquer step

Left half set points points Right half set


19
A simpler problem
The right half set is a point

20
The right half set is a point

21
The right half set is a point

Tool 1
𝑞
Up
p er
tan
ge
nt

𝑝
ent
ta ng
wer
Lo
𝑟

22
The right half set is a point

Upper Hull

Up
p er
tan
ge
nt

Lemma:
Upper tangent from will be incident on one point of the upper hull.
23
The right half set is a point

𝑝
t
gen
an
rt
owe
L

Lemma: Lower Hull


Lower tangent from 𝑝 will be incident on one point of the Lower hull.
24
How to compute the upper tangent ?

25
How to compute the upper tangent ?

O() time to find upper tangent


Tool 2 𝑠
𝑤 𝑞
𝑢
Can’t we improve
this this time
𝑡 𝑝
𝑟 complexity ?

Indeed, using careful insight in


the convex hull structure. 

Watch the next slides slowly and attentively

26
How to compute the upper tangent ?

𝑠
Consider successor of
𝑤 𝑞
𝑢

𝑡 𝑝
𝑟

Using convexity of the hull, show that


Continuing like this … we can discard all points of upper hull that follow
• lies above line (,).
• the other neighbor of (in this case ) lies below line (,). 27
How to compute the upper tangent ?

𝑠
Consider predecessor of
𝑤 𝑞
𝑢

𝑡 𝑝
𝑟

Using convexity of the hull, show that


each point of the upper hull that precedes can not form upper tangent from .
28
How to compute the upper tangent ?

𝑠
Does it remind you
𝑤 𝑞 Up something “very basic” you
p er learnt in the past ?
tan
ge
nt

𝑡 𝑝
𝑟
Binary Search

We perform binary search on the upper hull to find the upper tangent from

29
O(log ) time to find upper tangent
Going back to the original problem

Conquer Step

30
The Conquer step

gent
Upper tan

Lower tangen
t

points points 31
How to compute the upper tangent ?

O(log ) time

𝑤 𝑥
𝑣

𝑢
𝑦

points points 32
Running time of the algorithm
What is the recurrence for running time?

T() = c log + 2 T(/2)



T() = O( )

c
Theorem:
There exists an O( ) time algorithm to compute the convex hull of points in
plane.

33
Homework

1. Provide complete details of the “conquer” part of the algorithm.

2. What data structure will be used to store convex hull ?

3. Try to modify the algorithm to achieve O( ) time complexity.

34
Problem 4
An algorithm for
multiplying two polynomials

35
Multiplying two polynomials
Definition (polynomial of degree less than )

Problem : Given two polynomial of degree less than ,

Compute

where =
𝑎0 𝑏𝑖 +𝑎1 𝑏 𝑖−1 +𝑎2 𝑏𝑖 −2 +…+𝑎𝑖−1 𝑏1 +𝑎𝑖 𝑏0 O() time
Trivial algorithm: O( )
Aim: O( )
Applications:
• Signal processing (Discrete Fourier Transform)
• As practical as sorting and searching
• Multiplication of two integers.
36
Divide Step
Attempt 1
𝑨 ( 𝑥 ) =𝑎 0 +𝑎 1 𝑥+ 𝑎2 𝑥2 +… 𝑎 𝑛− 1 𝑥𝑛 −1

( ) ( ( ))
𝑛 𝑛 𝑛 𝑛
−1 −1
First terms
𝑎 0 +…+𝑎 𝑛 of
𝑥 2 2 Last
𝑥𝑎 𝑛⋅ 𝑥𝑎 +…
2terms
+
𝑛 …𝑎𝑎𝑛of
𝑛 − 1𝑥
−1
𝑥 𝑛2
−1
−1
2 2 2

𝑨 first ( 𝑥 ) 𝑨 second ( 𝑥 )

37
Homework
for those aiming beyond A*

1. Design an time algorithm based on divide and conquer ?

Hint:
Recall ESO207

38

You might also like