KEMBAR78
Unit ii divide and conquer -4 | PDF
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

Unit ii divide and conquer -4

  • 1.
    8/31/2020 1 UNIT-II Convex Hulls Material preparedby 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 Inthe 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 orset 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 Givean 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 byQuicksort 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 Step2-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 QuickHullalgorithm 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'sScan 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 thepoints 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: Sortpoints 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 aroundall 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 aroundall 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 aroundall 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 aroundall 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, weget a Convex hull. 7 35 p0 p1 p3 p2 p4 p5 p6 p7 p8 p11 p12 p10 p9 36 35 36
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
    8/31/2020 25 49 Complexity of Graham’sScan 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 ofGraham’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’sScan 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’sScan 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 ? Algorithmconv(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
  • 31.
  • 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 sortingalgorithm 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 vsUnstable 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? StableSorting 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