SlideShare a Scribd company logo
1 of 36
Download to read offline
8/31/2020
1
UNIT-II
Convex Hulls
Material prepared by Dr. N. Subhash Chandra
from Web resources
1
Outline
Introduction: Background & Definition of convex hull
• Three algorithms
Graham’s Scan
Quick hull
Divide and Conquer
• Applications
2
1
2
8/31/2020
2
Prerequisites: Polar angle
In the plane, the polar angle θ is the counterclockwise angle from x-axis at which
a point in the x-y plane lies.
3
Orientation
• Calculating Orientation
• Three kinds of orientation for three points
(a, b, c)
• Clockwise (CW): right turn
• Counterclockwise (CCW): left turn
• Collinear (COLL): no turn
• The orientation can be characterized by the
sign of the determinant △(a,b,c)
• If △(a,b,c)<0 ⇒ clockwise
• If △(a,b,c)=0 ⇒ collinear
• If △(a,b,c)>0 ⇒ counterclockwise
CW
a
b
c
CCW
a b
c
COLL
a
b
c
4
3
4
8/31/2020
3
Convexity
A shape or set is convex :
If for any two points that are part of the shape, the whole connecting line
segment is also part of the shape.
convex non-convex
5
• Convex hull is defined as a set of given object
• Convex hull of a set Q of points, denoted by CH(Q)is the
smallest convex polygon P for which each points in Q is
either on the boundary of P or in its interior.
Convex hull
6
5
6
8/31/2020
4
Convex hull problem
Give an algorithm that computes the convex hull of any given set of n points in
the plane efficiently.
• Inputs: location of n points
• Outputs: a convex polygon ⇒ a sorted sequence of the points, clockwise (CW)
or counterclockwise (CCW) along the boundary
inputs= a set of point
p1,p2,p3,p4,p5,p6,p7,p8,p9
outputs= representation of the convex hull
p4,p5,p8,p2,p9
7
Convexhull Applications
1. computer visualization, ray tracing
(e.g. video games, replacement of bounding boxes)
● Path finding
(e.g. embedded AI of Mars mission rovers)
● Geographical Information Systems (GIS)
(e.g. computing accessibility maps)
● visual pattern matching
(e.g. detecting car license plates)
● verification methods
(e.g. bounding of Number Decision Diagrams)
● geometry
(e.g. diameter computation)
8
7
8
8/31/2020
5
Quickhull Algorithm
9
Quickhull Algorithms
• Quickhull is a method of computing the convex hull of a
finite set of points in the plane.
• It uses a divide and conquer approach similar to that of
quicksort, from which its name derives.
• Quick Hull was published by C. Barber and D. Dobkin in
1995.
10
9
10
8/31/2020
6
11
QuickHull Algorithm
Inspired by Quicksort compute Convex Hull:
1. Assume points are sorted by x-coordinate values
Identify extreme points P1 and P2 (part of hull)
2. The set S of points is divided in two subsets S1 and
S2 Compute Convex Hull for S1
Compute Convex Hull for S2 in a similar manner
12
P1
P2
Pmax
11
12
8/31/2020
7
QuickHull Algorithm (2)
3. How to compute the Convex (upper) Hull for S1
find point Pmax that is farthest away from line P1P2
compute the hull of the points to the left of line P1Pmax
compute the hull of the points to the left of line PmaxP2
4. How to find the Pmax point
Pmax maximizes the area of the triangle PmaxP1P2
if tie, select the Pmax that maximizes the angle PmaxP1P2
The points inside triangle PmaxP1P2 can be excluded from further consideration
How to find geometrically the point Pmax and the points to the left of line
P1Pmax 13
QuickHull Algorithm (3)
How to find geometrically the point Pmax and the points
to the left of line P1P2
Given points P1(x1,y1), P2(x2,y2), Pmax(xmax,ymax)
The area of the triangle is half of the magnitude of the
determinant
=x1y2+xmaxy1+x2ymax–xmaxy2–x2y1-x1ymax
The sign of the expression is positive if and only if the
point Pmax is to the left of the line P1P2
x1 y1 1
x2 y2 1
xmax ymax 1
14
13
14
8/31/2020
8
Example
Step 1 Step 2-5 Step 6
15
Quickhull Algorithm Pseudocode
Input = a set S of n points
Assume that there are at least 2 points in the input set S of points
QuickHull (S)
{
// Find convex hull from the set S of n points
Convex_Hull := {}
Find left and right most points, say P1 & P2 Add P1 & P2 to
convex hull
Segment P1 P2 divides the remaining (n-2) points into 2 groups
S1 and S2 where S1 are points in S that are on the right side of
the oriented line from P1 to P2 , and S2 are points in S that are
on the right side of the oriented line from P2 to P1
FindHull (S1, P1, P2)
FindHull (S2, P2, P1)
}
FindHull (Sk, P, Q)
{
// Find points on convex hull from the set Sk of points
// that are on the right side of the oriented line from P to Q
If Sk has no point, then return.
From the given set of points in Sk, find farthest point, say C,
from segment PQ
Add point C to convex hull at the location between P and Q .
Three points P, Q and C partition the remaining points of Sk
into 3 subsets: S0, S1, and S2 , where S0 are points inside
triangle PCQ,
S1 are points on the right side of the oriented line from P to C
S2 are points on the right side of the oriented line from C to Q
FindHull(S1, P, C)
FindHull(S2, C, Q)
}
Output = Convex Hull
16
15
16
8/31/2020
9
Efficiency of QuickHull algorithm
Finding point farthest away from line P1P2 can be done in linear time
This gives same efficiency as quicksort:
If points are not initially sorted by x-coordinate value, this can
be accomplished in Θ(n log n) no increase in asymptotic
efficiency class
17
Graham's Scan Algorithm
18
17
18
8/31/2020
10
Graham's Scan Algorithm
Graham's Scan Algorithm is an efficient algorithm for finding
the convex hull of a finite set of points in the plane with time
complexity O(N log N). The algorithm finds all vertices of the
convex hull ordered along its boundary. It uses a stack to
detect and remove concavities in the boundary efficiently.
Step 1. Find the lowest point p (guaranteed to be in)
Step 2. Sort points around p (in polar angle) in increasing angle
Step 3. Walk around to remove concave angle.
(keep points with left turns, & drop those with right turns)
19
• Graham scan is a method of computing the
convex hull of a finite set of points in the plane
with time complexity O(n logn).
• The algorithm finds all vertices of the convex
hull ordered along its boundary
• Graham's scan solves the convex-hull problem
by maintaining a stack S of candidate points.
Each point of the input set Q is pushed once onto
the stack.
Graham's Scan Algorithm
20
19
20
8/31/2020
11
• And the points that are not vertices of CH(Q) are
eventually popped from the stack. When the
algorithm terminates, stack S contains exactly the
vertices of CH(Q), in counterclockwise order of their
appearance on the boundary.
• When we traverse the convex hull counter
clockwise, we should make a left turn at each
vertex.
• Each time the while loop finds a vertex at which we
make a non left turn ,the vertex is popped from the
vertex.
Graham's Scan Algorithm
21
22
21
22
8/31/2020
12
Example Graham's Scan
23
Step1: Select a start point
Start point: with minimum y-coordinate and
leftmost
• Select a extreme point as a start
point with the minimum y-
coordinate.
• If there are more than one points
have minimum y-coordinate, select
the leftmost one.
24
23
24
8/31/2020
13
Y
X0
1
2
3
4
5
6
Step 2: Sort points by polar angle
• Sort the points by polar angle in
counterclockwise order.
• If more than one point has the
same angle, remove all but the
one that is farthest from the
start point.
• This algorithm will work in this
sorting order.
• Angle can be calculated by
vector cosine law
7
25
0
1
2
3
4
5
Step3: Push first 3 points
• Add first 3 points into the result
set.
Let S be the result set.
• Push(p0, S)
• Push(p1, S)
• Push(p2, S)
6
7
Stack S: <p0, p1, p2>
26
25
26
8/31/2020
14
0
1
2
4
5
Step4: Work around all points by sorting
order
• 1->2->3 is left turn.
• Push (p3, S)
3
6
7
Next -to -top[S]
top[S]
Stack S: <p0, p1, p2>
27
Y
0
1
2
4
5
Step4: Work around all points by sorting
order
3
6• 2->3->4 is left turn.
• Push (p4, S)
7
Stack S: <p0, p1, p2, p3>
28
27
28
8/31/2020
15
Y
0
1
2
4
5
Step4: Work around all points by sorting
order
3
6• 3->4->5 is left turn.
• Push (p5, S)
7
Stack S: <p0, p1, p2, p3, p4>
29
0
1
2
4
5
Step4: Work around all points by sorting
order
3
6• 4->5->6 is non-left turn.
• Pop (p5, S)
7
Stack S: <p0, p1, p2, p3, p4, p5>
30
29
30
8/31/2020
16
0
1
2
4
5
Step4: Work around all points by sorting
order
3
6• 3->4->6 is non-left turn.
• Pop (p4, S)
7
Stack S: <p0, p1, p2, p3, p4>
31
Step4: Work around all points by sorting
order
0
1
2
4
5
3
6• 2->3->6 is non-left turn.
• Pop (p3, S)
7
Collinear
Stack S: <p0, p1, p2, p3>
32
31
32
8/31/2020
17
Step4: Work around all points by sorting
order
0
1
2
4
5
3
6• 2->6->7 is left turn.
• Push (p6, S)
7
Stack S: <p0, p1, p2>
33
Step4: Work around all points by sorting
order
0
1
2
4
5
3
6• 6->7->0 is left turn.
• Push (p7, S)
7
Set S: <p0, p1, p2, p6, p7>
34
33
34
8/31/2020
18
0
1
2
4
5
3
6• Finally, we get a Convex hull.
7
35
p0
p1
p3
p2
p4
p5
p6
p7
p8
p11
p12
p10
p9
36
35
36
8/31/2020
19
p0
p1
p3
p2
p4
p5
p6
p7
p8
p11
p12
p10
p9
37
p0
p1
p3
p2
p4
p5
p6
p7
p8
p11
p12
p10
p9
38
37
38
8/31/2020
20
p0
p1
p3
p2
p4
p5
p6
p7
p8
p11
p12
p10
p9
39
p0
p1
p3
p2
p4
p5
p6
p7
p8
p11
p12
p10
p9
40
39
40
8/31/2020
21
p0
p1
p3
p2
p4
p5
p6
p7
p8
p11
p12
p10
p9
41
p0
p1
p3
p2
p4
p5
p6
p7
p8
p11
p12
p10
p9
42
41
42
8/31/2020
22
p0
p1
p3
p2
p4
p5
p6
p7
p8
p9
p11
p12
p10
43
p0
p1
p3
p2
p4
p5
p6
p7
p8
p9
p11
p12
p10
44
43
44
8/31/2020
23
12/2/2015 39
p0
p1
p3
p2
p4
p5
p6
p7
p8
p9
p11
p12
p10
45
p0
p1
p3
p2
p4
p5
p6
p7
p8
p9
p11
p12
p10
46
45
46
8/31/2020
24
p0
p1
p3
p2
p4
p5
p6
p7
p8
p9
p11
p12
p10
47
p0
p1
p3
p2
p4
p5
p6
p7
p8
p9
p11
p12
p10
48
47
48
8/31/2020
25
49
Complexity of Graham’s Scan
Phase 2: Sorting
O(n*log n)
Phase 3: O(n)
● Each point is inserted into
the sequence exactly
once, and
● Each point is removed
from the sequence at
most once
O(n*logn)
Phase 1: select a start
point O(n)
50
49
50
8/31/2020
26
The correctness of Graham’s scan lies in two situations.
● Part 1 deals with nonleft turns;
● Part 2 deals with left turns.
Correctness of Graham’s Scan
Proof: The claim holds
immediately after line 5.
Points p0, p1, and p2 form a
triangle.
Claim 1: The points on stack always form the vertices of a convex polygon.
p2
p1
p0
51
Correctness of Graham’s Scan
Proof (cont’d): Note stack changes in two ways:
Case i: When points are popped from the stack S.
Case ii: When points are pushed onto the stack S.
Case i. Here we rely on the geometric property: If a
vertex is removed from a convex polygon, then the
resulting polygon is convex.
p0
p1
p2
pk
pj
pi
pj
pk
...
p1
p0
pk
...
p1
p0 52
51
52
8/31/2020
27
Correctness of Graham’s Scan
Proof (cont’d): Note stack changes in two ways:
Case i: When points are popped from the stack S.
Case ii: When points are pushed onto the stack S.
Case ii. If pi is pushed onto the stack:
● By the choice of p0 and p1, pi lies above the
extension of the line p0p1.
● By the fact, a left turn occurs at pj, pi lies below the
extension of the line joining pk and pj.
● By the order of polar angle, pi lies left of the line
joining p0 and pj. pj
pk
...
p1
p0
pi
pj
pk
...
p1
p0
53
Correctness of Graham’s Scan
Claim 2: Each point popped from the stack is not a vertex of CH(P) and lies
inside new convex hull.
Proof: Suppose that point pj is popped from the stack because
angle pkpjpi makes a nonleft turn as shown in the figure.
Since points are ordered in increasing polar angle around anchor
p0, there exists a triangle Δp0pipk with pj either in the interior of the
triangle or on the line segment joining pi and pk. In either case,
point pj cannot be a vertex of CH(P). pj
pk
...
p1
p0
pk
...
p1
p0
pi
pk
...
p1
p0 54
53
54
8/31/2020
28
Correctness of Graham’s Scan
Conclusion
Since each point popped from the stack is not a
vertex of CH(P) (by Claim 2). And the points on
stack always form the vertices of a convex
polygon (by Claim 1). Therefore, Graham’s Scan
is correct.
55
Divide and Conquer method
56
55
56
8/31/2020
29
57
Convex Hull: Divide & Conquer
 Preprocessing: sort the points by x-coordinate
 Divide the set of points into two sets A and B:
 A contains the left n/2 points,
 B contains the right n/2 points
Recursively compute the convex hull of A
Recursively compute the convex hull of B
 Merge the two convex hulls A B
Divide & Conquer pseudo code for Convex hull
hull(S) = smallest convex set that contains S (points are
stored in ccw order)
1. Sort all points of S with increasing x-coordinates
2. Algorithm conv(S, n)
if n < 4, then trivially solved
else
DIVIDE: A & B
RECUR: conv(A, n/2), conv(B, n/2)
MERGE: combine hull(A) and hull(B)
58
57
58
8/31/2020
30
Why n<4 ?
Algorithm conv(S, n)
if n < 4, then trivially solved
else
DIVIDE: A & B
RECUR: conv(A, n/2), conv(B, n/2)
MERGE: combine hull(A) and hull(B)
59
60
59
60
8/31/2020
31
61
62
61
62
8/31/2020
32
Lower Tangent Example •Initially, T=(4, 7) is only a lower
tangent for A. The A loop does not
execute, but the B loop increments b
to 11.
•But now T=(4, 11) is no longer a
lower tangent for A, so the A loop
decrements a to 0.
•T=(0, 11) is not a lower tangent for B,
so b is incremented to 12.
• T=(0, 12) is a lower tangent for both
A and B, and T is returned.
63
Convex Hull: Complexity
 Preprocessing: sort the points by x-coordinate
 Divide the set of points into two sets A and B:
 A contains the left n/2 points,
 B contains the right n/2 points
Recursively compute the convex hull of A
Recursively compute the convex hull of B
 Merge the two convex hulls
O(n log n) just once
O(1)
T(n/2)
T(n/2)
O(n)
64
63
64
8/31/2020
33
Divide & Conquer
T(n) = 2T(n/2) + cn
Using Masters theorem
a=2, b=2
f(n) = cn ∈ ⊝(n) d=1
T(n)=⊝(n log n) (a=bd =2)
Best, Average, Worst case complexity O(n logn)
Space Complexity O(n)
65
Stable and Unstable Algorithms
Prepared by Dr. N. Subhash Chandra
Source: Web resources
66
65
66
8/31/2020
34
Stable sorting
A sorting algorithm is said to be stable if the order of the same values in the output
remains the same as in input array.
Stable Sorting algorithms: Insertion sort, Merge Sort, Bubble Sort
This is an example of stable sorting, element 12 appears twice at index 5 and at index 7. In
the output array after sorting is done the order is maintained 12 which was at index 5
appears first and 12 which was at index 7 appears later.
Application of stability becomes more important when we have key value pairs where keys
can be duplicated. 67
Unstable sorting
In an unstable sorting algorithm the ordering of the same values is not
necessarily preserved after sorting.
Unstable sorting: Selection sort, Shell sort, Quicksort, Heapsort
Here you can see that the ordering for the same element 12 is not maintained
after sorting.
68
67
68
8/31/2020
35
Example: Stable vs Unstable Algorithm
Suppose you need to sort following key-value pairs in the increasing order of keys:
INPUT: (4,5), (3, 2) (4, 3) (5,4) (6,4)
Now, there is two possible solution for the two pairs where the key is the same i.e. (4,5) and (4,3) as shown
below:
OUTPUT1: (3, 2), (4, 5), (4,3), (5,4), (6,4)
OUTPUT2: (3, 2), (4, 3), (4,5), (5,4), (6,4)
Stable sorting: Ex OUTPUT 1
because the original order of equal keys are maintained, you can see that (4, 5) comes before (4,3) in the
sorted order, which was the original order i.e. in the given input, (4, 5) comes before (4,3) .
Unstable sorting: Ex OUTPUT 2:
because the order of objects with the same key is not maintained in the sorted order. You can see that in the
second output, the (4,3) comes before (4,5) which was not the case in the original input.
69
Example
70
69
70
8/31/2020
36
When stability required?
Stable Sorting Is Important, Sometimes:
We don’t always need stable sorting. Stability is not a concern if:
•equal elements are indistinguishable, or
•all the elements in the collection are distinct
When equal elements are distinguishable, stability is imperative. For instance, if the
collection already has some order, then sorting on another key must preserve that order.
For example, let’s say we are computing the word count of each distinct word in a text file.
Now, we need to report the results in decreasing order of count, and further sorted
alphabetically in case two words have the same count.
Distinguishing Between Equal Elements:
All sorting algorithms use a key to determine the ordering of the elements in the collection, called
the sort key.
If the sort key is the (entire) element itself, equal elements are indistinguishable, such as integers or
strings.
On the other hand, equal elements are distinguishable if the sort key is made up of one or more, but
not all attributes of the element, such as age in an Employee class.
71
Example: Compute the word count:
Input:
how much wood would woodchuck
chuck if woodchuck could chuck wood
Output:
how 1
much 1
wood 2
would 1
woodchuck 2
chuck 2
if 1
could 1
Second step – sort the whole
list lexicographically, then by
word count:
First pass, sorted
lexicographically:
(chuck, 2)
(could, 1)
(how, 1)
(if, 1)
(much, 1)
(wood, 2)
(woodchuck, 2)
(would, 1)
Second pass, sorted
by count using an
unstable sort:
(wood, 2)
(chuck, 2)
(woodchuck, 2)
(could, 1)
(how, 1)
(if, 1)
(would, 1)
(much, 1)
Second pass, sorted by count
using a stable sort:
(chuck, 2)
(wood, 2)
(woodchuck, 2)
(could, 1)
(how, 1)
(if, 1)
(much, 1)
(would, 1)
As we have used an unstable sort, the second pass does not maintain the lexicographical order.
That’s where the stable sort comes into the picture. Since we already had sorted the list lexicographically, using a stable sort to by word count
does not change the order of equal elements anymore. As a result words with the same word count remain sorted lexicographically.
72
71
72

More Related Content

What's hot

4.1 quadratic functions and transformations
4.1 quadratic functions and transformations4.1 quadratic functions and transformations
4.1 quadratic functions and transformationsleblance
 
April 14, 2015
April 14, 2015April 14, 2015
April 14, 2015khyps13
 
3 rd quarter projectin math
3 rd quarter projectin math3 rd quarter projectin math
3 rd quarter projectin mathiam_soojin
 
Trigonometric Functions and their Graphs
Trigonometric Functions and their GraphsTrigonometric Functions and their Graphs
Trigonometric Functions and their GraphsMohammed Ahmed
 
Grafica funciones cuadráticas
Grafica funciones cuadráticasGrafica funciones cuadráticas
Grafica funciones cuadráticasbibliotecalcr
 
Higher Maths 1.2.3 - Trigonometric Functions
Higher Maths 1.2.3 - Trigonometric FunctionsHigher Maths 1.2.3 - Trigonometric Functions
Higher Maths 1.2.3 - Trigonometric Functionstimschmitz
 
5 1 quadratic transformations
5 1 quadratic transformations5 1 quadratic transformations
5 1 quadratic transformationslothomas
 
6. 1 graphing quadratics
6. 1 graphing quadratics6. 1 graphing quadratics
6. 1 graphing quadraticsJessica Garcia
 
4.1 quadratic functions and transformations
4.1 quadratic functions and transformations4.1 quadratic functions and transformations
4.1 quadratic functions and transformationsleblance
 
Applications of integration
Applications of integrationApplications of integration
Applications of integration18902
 
April 8, 2014
April 8, 2014April 8, 2014
April 8, 2014khyps13
 
คู่มือการใช้ Casiofx5800 p surveyingprograms
คู่มือการใช้ Casiofx5800 p surveyingprogramsคู่มือการใช้ Casiofx5800 p surveyingprograms
คู่มือการใช้ Casiofx5800 p surveyingprogramsTherdkeat Khuonhat
 
Advanced Trigonometry
Advanced TrigonometryAdvanced Trigonometry
Advanced Trigonometrytimschmitz
 
Transformación de coordenadas
Transformación de coordenadasTransformación de coordenadas
Transformación de coordenadasAndres32698
 

What's hot (20)

4.1 quadratic functions and transformations
4.1 quadratic functions and transformations4.1 quadratic functions and transformations
4.1 quadratic functions and transformations
 
Triangulaciones irreducibles en el toro perforado
Triangulaciones irreducibles en el toro perforadoTriangulaciones irreducibles en el toro perforado
Triangulaciones irreducibles en el toro perforado
 
Curve clipping
Curve clippingCurve clipping
Curve clipping
 
April 14, 2015
April 14, 2015April 14, 2015
April 14, 2015
 
3 rd quarter projectin math
3 rd quarter projectin math3 rd quarter projectin math
3 rd quarter projectin math
 
Trigonometric Functions and their Graphs
Trigonometric Functions and their GraphsTrigonometric Functions and their Graphs
Trigonometric Functions and their Graphs
 
1. ejercicios
1. ejercicios1. ejercicios
1. ejercicios
 
2. la recta
2. la recta2. la recta
2. la recta
 
1. vectores
1. vectores1. vectores
1. vectores
 
Grafica funciones cuadráticas
Grafica funciones cuadráticasGrafica funciones cuadráticas
Grafica funciones cuadráticas
 
Higher Maths 1.2.3 - Trigonometric Functions
Higher Maths 1.2.3 - Trigonometric FunctionsHigher Maths 1.2.3 - Trigonometric Functions
Higher Maths 1.2.3 - Trigonometric Functions
 
5 1 quadratic transformations
5 1 quadratic transformations5 1 quadratic transformations
5 1 quadratic transformations
 
6. 1 graphing quadratics
6. 1 graphing quadratics6. 1 graphing quadratics
6. 1 graphing quadratics
 
4.1 quadratic functions and transformations
4.1 quadratic functions and transformations4.1 quadratic functions and transformations
4.1 quadratic functions and transformations
 
Cg
CgCg
Cg
 
Applications of integration
Applications of integrationApplications of integration
Applications of integration
 
April 8, 2014
April 8, 2014April 8, 2014
April 8, 2014
 
คู่มือการใช้ Casiofx5800 p surveyingprograms
คู่มือการใช้ Casiofx5800 p surveyingprogramsคู่มือการใช้ Casiofx5800 p surveyingprograms
คู่มือการใช้ Casiofx5800 p surveyingprograms
 
Advanced Trigonometry
Advanced TrigonometryAdvanced Trigonometry
Advanced Trigonometry
 
Transformación de coordenadas
Transformación de coordenadasTransformación de coordenadas
Transformación de coordenadas
 

Similar to Unit ii divide and conquer -4

Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in indiaEdhole.com
 
Graphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsGraphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsThirunavukarasu Mani
 
Graphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsGraphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsKetan Jani
 
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)Austin Benson
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graphgetacew
 
Plano numerico
Plano numericoPlano numerico
Plano numericoroxi13
 
Site Surveying Traversing
Site Surveying TraversingSite Surveying Traversing
Site Surveying TraversingDarren Lee
 
Jee advanced-2020-paper-1-solution
Jee advanced-2020-paper-1-solutionJee advanced-2020-paper-1-solution
Jee advanced-2020-paper-1-solutionAnkitBiswas17
 
ROOT-LOCUS METHOD, Determine the root loci on the real axis /the asymptotes o...
ROOT-LOCUS METHOD, Determine the root loci on the real axis /the asymptotes o...ROOT-LOCUS METHOD, Determine the root loci on the real axis /the asymptotes o...
ROOT-LOCUS METHOD, Determine the root loci on the real axis /the asymptotes o...Waqas Afzal
 
root locus 1.pdf
root locus 1.pdfroot locus 1.pdf
root locus 1.pdfMarkHark1
 
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsShanmuganathan C
 
Autonomous Perching Quadcopter
Autonomous Perching QuadcopterAutonomous Perching Quadcopter
Autonomous Perching QuadcopterYucheng Chen
 
TRAVERSE in land surveying and technique
TRAVERSE in land surveying and techniqueTRAVERSE in land surveying and technique
TRAVERSE in land surveying and techniquezaphenathpaneah1
 

Similar to Unit ii divide and conquer -4 (20)

Convex hull
Convex hullConvex hull
Convex hull
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in india
 
Graphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsGraphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygons
 
Graphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsGraphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygons
 
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graph
 
Geometric algorithms
Geometric algorithmsGeometric algorithms
Geometric algorithms
 
CG-Lecture3.pptx
CG-Lecture3.pptxCG-Lecture3.pptx
CG-Lecture3.pptx
 
Plano numerico
Plano numericoPlano numerico
Plano numerico
 
Site Surveying Traversing
Site Surveying TraversingSite Surveying Traversing
Site Surveying Traversing
 
Jee advanced-2020-paper-1-solution
Jee advanced-2020-paper-1-solutionJee advanced-2020-paper-1-solution
Jee advanced-2020-paper-1-solution
 
convex hull
convex hullconvex hull
convex hull
 
ROOT-LOCUS METHOD, Determine the root loci on the real axis /the asymptotes o...
ROOT-LOCUS METHOD, Determine the root loci on the real axis /the asymptotes o...ROOT-LOCUS METHOD, Determine the root loci on the real axis /the asymptotes o...
ROOT-LOCUS METHOD, Determine the root loci on the real axis /the asymptotes o...
 
Vectors and 3 d
Vectors and 3 dVectors and 3 d
Vectors and 3 d
 
Lecture24
Lecture24Lecture24
Lecture24
 
root locus 1.pdf
root locus 1.pdfroot locus 1.pdf
root locus 1.pdf
 
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
 
Autonomous Perching Quadcopter
Autonomous Perching QuadcopterAutonomous Perching Quadcopter
Autonomous Perching Quadcopter
 
TRAVERSE in land surveying and technique
TRAVERSE in land surveying and techniqueTRAVERSE in land surveying and technique
TRAVERSE in land surveying and technique
 
Chapter 8.pptx
Chapter 8.pptxChapter 8.pptx
Chapter 8.pptx
 

More from subhashchandra197

Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.pptUnit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.pptsubhashchandra197
 
Unit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptxUnit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptxsubhashchandra197
 
Data Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptxData Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptxsubhashchandra197
 
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...subhashchandra197
 
Data science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptxData science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptxsubhashchandra197
 
Unit ii divide and conquer -3
Unit ii divide and conquer -3Unit ii divide and conquer -3
Unit ii divide and conquer -3subhashchandra197
 
Unit ii divide and conquer -2
Unit ii divide and conquer -2Unit ii divide and conquer -2
Unit ii divide and conquer -2subhashchandra197
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1subhashchandra197
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutionssubhashchandra197
 
P,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problemsP,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problemssubhashchandra197
 

More from subhashchandra197 (17)

Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.pptUnit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
 
Unit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptxUnit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptx
 
Data Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptxData Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptx
 
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
 
Data science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptxData science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptx
 
Unit ii divide and conquer -3
Unit ii divide and conquer -3Unit ii divide and conquer -3
Unit ii divide and conquer -3
 
Unit ii divide and conquer -2
Unit ii divide and conquer -2Unit ii divide and conquer -2
Unit ii divide and conquer -2
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
P,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problemsP,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problems
 
Turing machine material
Turing machine materialTuring machine material
Turing machine material
 
Turing machine types
Turing machine typesTuring machine types
Turing machine types
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Disjoint sets union, find
Disjoint sets  union, findDisjoint sets  union, find
Disjoint sets union, find
 

Recently uploaded

Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...gragchanchal546
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfsumitt6_25730773
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Ramkumar k
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxpritamlangde
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptAfnanAhmad53
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 

Recently uploaded (20)

Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 

Unit ii divide and conquer -4

  • 1. 8/31/2020 1 UNIT-II Convex Hulls Material prepared by Dr. N. Subhash Chandra from Web resources 1 Outline Introduction: Background & Definition of convex hull • Three algorithms Graham’s Scan Quick hull Divide and Conquer • Applications 2 1 2
  • 2. 8/31/2020 2 Prerequisites: Polar angle In the plane, the polar angle θ is the counterclockwise angle from x-axis at which a point in the x-y plane lies. 3 Orientation • Calculating Orientation • Three kinds of orientation for three points (a, b, c) • Clockwise (CW): right turn • Counterclockwise (CCW): left turn • Collinear (COLL): no turn • The orientation can be characterized by the sign of the determinant △(a,b,c) • If △(a,b,c)<0 ⇒ clockwise • If △(a,b,c)=0 ⇒ collinear • If △(a,b,c)>0 ⇒ counterclockwise CW a b c CCW a b c COLL a b c 4 3 4
  • 3. 8/31/2020 3 Convexity A shape or set is convex : If for any two points that are part of the shape, the whole connecting line segment is also part of the shape. convex non-convex 5 • Convex hull is defined as a set of given object • Convex hull of a set Q of points, denoted by CH(Q)is the smallest convex polygon P for which each points in Q is either on the boundary of P or in its interior. Convex hull 6 5 6
  • 4. 8/31/2020 4 Convex hull problem Give an algorithm that computes the convex hull of any given set of n points in the plane efficiently. • Inputs: location of n points • Outputs: a convex polygon ⇒ a sorted sequence of the points, clockwise (CW) or counterclockwise (CCW) along the boundary inputs= a set of point p1,p2,p3,p4,p5,p6,p7,p8,p9 outputs= representation of the convex hull p4,p5,p8,p2,p9 7 Convexhull Applications 1. computer visualization, ray tracing (e.g. video games, replacement of bounding boxes) ● Path finding (e.g. embedded AI of Mars mission rovers) ● Geographical Information Systems (GIS) (e.g. computing accessibility maps) ● visual pattern matching (e.g. detecting car license plates) ● verification methods (e.g. bounding of Number Decision Diagrams) ● geometry (e.g. diameter computation) 8 7 8
  • 5. 8/31/2020 5 Quickhull Algorithm 9 Quickhull Algorithms • Quickhull is a method of computing the convex hull of a finite set of points in the plane. • It uses a divide and conquer approach similar to that of quicksort, from which its name derives. • Quick Hull was published by C. Barber and D. Dobkin in 1995. 10 9 10
  • 6. 8/31/2020 6 11 QuickHull Algorithm Inspired by Quicksort compute Convex Hull: 1. Assume points are sorted by x-coordinate values Identify extreme points P1 and P2 (part of hull) 2. The set S of points is divided in two subsets S1 and S2 Compute Convex Hull for S1 Compute Convex Hull for S2 in a similar manner 12 P1 P2 Pmax 11 12
  • 7. 8/31/2020 7 QuickHull Algorithm (2) 3. How to compute the Convex (upper) Hull for S1 find point Pmax that is farthest away from line P1P2 compute the hull of the points to the left of line P1Pmax compute the hull of the points to the left of line PmaxP2 4. How to find the Pmax point Pmax maximizes the area of the triangle PmaxP1P2 if tie, select the Pmax that maximizes the angle PmaxP1P2 The points inside triangle PmaxP1P2 can be excluded from further consideration How to find geometrically the point Pmax and the points to the left of line P1Pmax 13 QuickHull Algorithm (3) How to find geometrically the point Pmax and the points to the left of line P1P2 Given points P1(x1,y1), P2(x2,y2), Pmax(xmax,ymax) The area of the triangle is half of the magnitude of the determinant =x1y2+xmaxy1+x2ymax–xmaxy2–x2y1-x1ymax The sign of the expression is positive if and only if the point Pmax is to the left of the line P1P2 x1 y1 1 x2 y2 1 xmax ymax 1 14 13 14
  • 8. 8/31/2020 8 Example Step 1 Step 2-5 Step 6 15 Quickhull Algorithm Pseudocode Input = a set S of n points Assume that there are at least 2 points in the input set S of points QuickHull (S) { // Find convex hull from the set S of n points Convex_Hull := {} Find left and right most points, say P1 & P2 Add P1 & P2 to convex hull Segment P1 P2 divides the remaining (n-2) points into 2 groups S1 and S2 where S1 are points in S that are on the right side of the oriented line from P1 to P2 , and S2 are points in S that are on the right side of the oriented line from P2 to P1 FindHull (S1, P1, P2) FindHull (S2, P2, P1) } FindHull (Sk, P, Q) { // Find points on convex hull from the set Sk of points // that are on the right side of the oriented line from P to Q If Sk has no point, then return. From the given set of points in Sk, find farthest point, say C, from segment PQ Add point C to convex hull at the location between P and Q . Three points P, Q and C partition the remaining points of Sk into 3 subsets: S0, S1, and S2 , where S0 are points inside triangle PCQ, S1 are points on the right side of the oriented line from P to C S2 are points on the right side of the oriented line from C to Q FindHull(S1, P, C) FindHull(S2, C, Q) } Output = Convex Hull 16 15 16
  • 9. 8/31/2020 9 Efficiency of QuickHull algorithm Finding point farthest away from line P1P2 can be done in linear time This gives same efficiency as quicksort: If points are not initially sorted by x-coordinate value, this can be accomplished in Θ(n log n) no increase in asymptotic efficiency class 17 Graham's Scan Algorithm 18 17 18
  • 10. 8/31/2020 10 Graham's Scan Algorithm Graham's Scan Algorithm is an efficient algorithm for finding the convex hull of a finite set of points in the plane with time complexity O(N log N). The algorithm finds all vertices of the convex hull ordered along its boundary. It uses a stack to detect and remove concavities in the boundary efficiently. Step 1. Find the lowest point p (guaranteed to be in) Step 2. Sort points around p (in polar angle) in increasing angle Step 3. Walk around to remove concave angle. (keep points with left turns, & drop those with right turns) 19 • Graham scan is a method of computing the convex hull of a finite set of points in the plane with time complexity O(n logn). • The algorithm finds all vertices of the convex hull ordered along its boundary • Graham's scan solves the convex-hull problem by maintaining a stack S of candidate points. Each point of the input set Q is pushed once onto the stack. Graham's Scan Algorithm 20 19 20
  • 11. 8/31/2020 11 • And the points that are not vertices of CH(Q) are eventually popped from the stack. When the algorithm terminates, stack S contains exactly the vertices of CH(Q), in counterclockwise order of their appearance on the boundary. • When we traverse the convex hull counter clockwise, we should make a left turn at each vertex. • Each time the while loop finds a vertex at which we make a non left turn ,the vertex is popped from the vertex. Graham's Scan Algorithm 21 22 21 22
  • 12. 8/31/2020 12 Example Graham's Scan 23 Step1: Select a start point Start point: with minimum y-coordinate and leftmost • Select a extreme point as a start point with the minimum y- coordinate. • If there are more than one points have minimum y-coordinate, select the leftmost one. 24 23 24
  • 13. 8/31/2020 13 Y X0 1 2 3 4 5 6 Step 2: Sort points by polar angle • Sort the points by polar angle in counterclockwise order. • If more than one point has the same angle, remove all but the one that is farthest from the start point. • This algorithm will work in this sorting order. • Angle can be calculated by vector cosine law 7 25 0 1 2 3 4 5 Step3: Push first 3 points • Add first 3 points into the result set. Let S be the result set. • Push(p0, S) • Push(p1, S) • Push(p2, S) 6 7 Stack S: <p0, p1, p2> 26 25 26
  • 14. 8/31/2020 14 0 1 2 4 5 Step4: Work around all points by sorting order • 1->2->3 is left turn. • Push (p3, S) 3 6 7 Next -to -top[S] top[S] Stack S: <p0, p1, p2> 27 Y 0 1 2 4 5 Step4: Work around all points by sorting order 3 6• 2->3->4 is left turn. • Push (p4, S) 7 Stack S: <p0, p1, p2, p3> 28 27 28
  • 15. 8/31/2020 15 Y 0 1 2 4 5 Step4: Work around all points by sorting order 3 6• 3->4->5 is left turn. • Push (p5, S) 7 Stack S: <p0, p1, p2, p3, p4> 29 0 1 2 4 5 Step4: Work around all points by sorting order 3 6• 4->5->6 is non-left turn. • Pop (p5, S) 7 Stack S: <p0, p1, p2, p3, p4, p5> 30 29 30
  • 16. 8/31/2020 16 0 1 2 4 5 Step4: Work around all points by sorting order 3 6• 3->4->6 is non-left turn. • Pop (p4, S) 7 Stack S: <p0, p1, p2, p3, p4> 31 Step4: Work around all points by sorting order 0 1 2 4 5 3 6• 2->3->6 is non-left turn. • Pop (p3, S) 7 Collinear Stack S: <p0, p1, p2, p3> 32 31 32
  • 17. 8/31/2020 17 Step4: Work around all points by sorting order 0 1 2 4 5 3 6• 2->6->7 is left turn. • Push (p6, S) 7 Stack S: <p0, p1, p2> 33 Step4: Work around all points by sorting order 0 1 2 4 5 3 6• 6->7->0 is left turn. • Push (p7, S) 7 Set S: <p0, p1, p2, p6, p7> 34 33 34
  • 18. 8/31/2020 18 0 1 2 4 5 3 6• Finally, we get a Convex hull. 7 35 p0 p1 p3 p2 p4 p5 p6 p7 p8 p11 p12 p10 p9 36 35 36
  • 25. 8/31/2020 25 49 Complexity of Graham’s Scan Phase 2: Sorting O(n*log n) Phase 3: O(n) ● Each point is inserted into the sequence exactly once, and ● Each point is removed from the sequence at most once O(n*logn) Phase 1: select a start point O(n) 50 49 50
  • 26. 8/31/2020 26 The correctness of Graham’s scan lies in two situations. ● Part 1 deals with nonleft turns; ● Part 2 deals with left turns. Correctness of Graham’s Scan Proof: The claim holds immediately after line 5. Points p0, p1, and p2 form a triangle. Claim 1: The points on stack always form the vertices of a convex polygon. p2 p1 p0 51 Correctness of Graham’s Scan Proof (cont’d): Note stack changes in two ways: Case i: When points are popped from the stack S. Case ii: When points are pushed onto the stack S. Case i. Here we rely on the geometric property: If a vertex is removed from a convex polygon, then the resulting polygon is convex. p0 p1 p2 pk pj pi pj pk ... p1 p0 pk ... p1 p0 52 51 52
  • 27. 8/31/2020 27 Correctness of Graham’s Scan Proof (cont’d): Note stack changes in two ways: Case i: When points are popped from the stack S. Case ii: When points are pushed onto the stack S. Case ii. If pi is pushed onto the stack: ● By the choice of p0 and p1, pi lies above the extension of the line p0p1. ● By the fact, a left turn occurs at pj, pi lies below the extension of the line joining pk and pj. ● By the order of polar angle, pi lies left of the line joining p0 and pj. pj pk ... p1 p0 pi pj pk ... p1 p0 53 Correctness of Graham’s Scan Claim 2: Each point popped from the stack is not a vertex of CH(P) and lies inside new convex hull. Proof: Suppose that point pj is popped from the stack because angle pkpjpi makes a nonleft turn as shown in the figure. Since points are ordered in increasing polar angle around anchor p0, there exists a triangle Δp0pipk with pj either in the interior of the triangle or on the line segment joining pi and pk. In either case, point pj cannot be a vertex of CH(P). pj pk ... p1 p0 pk ... p1 p0 pi pk ... p1 p0 54 53 54
  • 28. 8/31/2020 28 Correctness of Graham’s Scan Conclusion Since each point popped from the stack is not a vertex of CH(P) (by Claim 2). And the points on stack always form the vertices of a convex polygon (by Claim 1). Therefore, Graham’s Scan is correct. 55 Divide and Conquer method 56 55 56
  • 29. 8/31/2020 29 57 Convex Hull: Divide & Conquer  Preprocessing: sort the points by x-coordinate  Divide the set of points into two sets A and B:  A contains the left n/2 points,  B contains the right n/2 points Recursively compute the convex hull of A Recursively compute the convex hull of B  Merge the two convex hulls A B Divide & Conquer pseudo code for Convex hull hull(S) = smallest convex set that contains S (points are stored in ccw order) 1. Sort all points of S with increasing x-coordinates 2. Algorithm conv(S, n) if n < 4, then trivially solved else DIVIDE: A & B RECUR: conv(A, n/2), conv(B, n/2) MERGE: combine hull(A) and hull(B) 58 57 58
  • 30. 8/31/2020 30 Why n<4 ? Algorithm conv(S, n) if n < 4, then trivially solved else DIVIDE: A & B RECUR: conv(A, n/2), conv(B, n/2) MERGE: combine hull(A) and hull(B) 59 60 59 60
  • 32. 8/31/2020 32 Lower Tangent Example •Initially, T=(4, 7) is only a lower tangent for A. The A loop does not execute, but the B loop increments b to 11. •But now T=(4, 11) is no longer a lower tangent for A, so the A loop decrements a to 0. •T=(0, 11) is not a lower tangent for B, so b is incremented to 12. • T=(0, 12) is a lower tangent for both A and B, and T is returned. 63 Convex Hull: Complexity  Preprocessing: sort the points by x-coordinate  Divide the set of points into two sets A and B:  A contains the left n/2 points,  B contains the right n/2 points Recursively compute the convex hull of A Recursively compute the convex hull of B  Merge the two convex hulls O(n log n) just once O(1) T(n/2) T(n/2) O(n) 64 63 64
  • 33. 8/31/2020 33 Divide & Conquer T(n) = 2T(n/2) + cn Using Masters theorem a=2, b=2 f(n) = cn ∈ ⊝(n) d=1 T(n)=⊝(n log n) (a=bd =2) Best, Average, Worst case complexity O(n logn) Space Complexity O(n) 65 Stable and Unstable Algorithms Prepared by Dr. N. Subhash Chandra Source: Web resources 66 65 66
  • 34. 8/31/2020 34 Stable sorting A sorting algorithm is said to be stable if the order of the same values in the output remains the same as in input array. Stable Sorting algorithms: Insertion sort, Merge Sort, Bubble Sort This is an example of stable sorting, element 12 appears twice at index 5 and at index 7. In the output array after sorting is done the order is maintained 12 which was at index 5 appears first and 12 which was at index 7 appears later. Application of stability becomes more important when we have key value pairs where keys can be duplicated. 67 Unstable sorting In an unstable sorting algorithm the ordering of the same values is not necessarily preserved after sorting. Unstable sorting: Selection sort, Shell sort, Quicksort, Heapsort Here you can see that the ordering for the same element 12 is not maintained after sorting. 68 67 68
  • 35. 8/31/2020 35 Example: Stable vs Unstable Algorithm Suppose you need to sort following key-value pairs in the increasing order of keys: INPUT: (4,5), (3, 2) (4, 3) (5,4) (6,4) Now, there is two possible solution for the two pairs where the key is the same i.e. (4,5) and (4,3) as shown below: OUTPUT1: (3, 2), (4, 5), (4,3), (5,4), (6,4) OUTPUT2: (3, 2), (4, 3), (4,5), (5,4), (6,4) Stable sorting: Ex OUTPUT 1 because the original order of equal keys are maintained, you can see that (4, 5) comes before (4,3) in the sorted order, which was the original order i.e. in the given input, (4, 5) comes before (4,3) . Unstable sorting: Ex OUTPUT 2: because the order of objects with the same key is not maintained in the sorted order. You can see that in the second output, the (4,3) comes before (4,5) which was not the case in the original input. 69 Example 70 69 70
  • 36. 8/31/2020 36 When stability required? Stable Sorting Is Important, Sometimes: We don’t always need stable sorting. Stability is not a concern if: •equal elements are indistinguishable, or •all the elements in the collection are distinct When equal elements are distinguishable, stability is imperative. For instance, if the collection already has some order, then sorting on another key must preserve that order. For example, let’s say we are computing the word count of each distinct word in a text file. Now, we need to report the results in decreasing order of count, and further sorted alphabetically in case two words have the same count. Distinguishing Between Equal Elements: All sorting algorithms use a key to determine the ordering of the elements in the collection, called the sort key. If the sort key is the (entire) element itself, equal elements are indistinguishable, such as integers or strings. On the other hand, equal elements are distinguishable if the sort key is made up of one or more, but not all attributes of the element, such as age in an Employee class. 71 Example: Compute the word count: Input: how much wood would woodchuck chuck if woodchuck could chuck wood Output: how 1 much 1 wood 2 would 1 woodchuck 2 chuck 2 if 1 could 1 Second step – sort the whole list lexicographically, then by word count: First pass, sorted lexicographically: (chuck, 2) (could, 1) (how, 1) (if, 1) (much, 1) (wood, 2) (woodchuck, 2) (would, 1) Second pass, sorted by count using an unstable sort: (wood, 2) (chuck, 2) (woodchuck, 2) (could, 1) (how, 1) (if, 1) (would, 1) (much, 1) Second pass, sorted by count using a stable sort: (chuck, 2) (wood, 2) (woodchuck, 2) (could, 1) (how, 1) (if, 1) (much, 1) (would, 1) As we have used an unstable sort, the second pass does not maintain the lexicographical order. That’s where the stable sort comes into the picture. Since we already had sorted the list lexicographically, using a stable sort to by word count does not change the order of equal elements anymore. As a result words with the same word count remain sorted lexicographically. 72 71 72