Convex Hulls - Carleton University

59
Convex Hulls Convex Hulls: An Application of Stacks and Deques Pat Morin COMP2402/2002 Carleton University Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Transcript of Convex Hulls - Carleton University

Page 1: Convex Hulls - Carleton University

Convex Hulls

Convex Hulls: An Application of Stacks andDeques

Pat MorinCOMP2402/2002

Carleton University

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 2: Convex Hulls - Carleton University

Convex Hulls

Convex Hulls

I Let P = {p0, . . . , pn−1} be a set of points in the planeI The convex hull of S

I the smallest convex set that contains S .

I stretch a rubber band around S

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 3: Convex Hulls - Carleton University

Convex Hulls

Convex Hulls

I Let P = {p0, . . . , pn−1} be a set of points in the planeI The convex hull of S

I the smallest convex set that contains S .I stretch a rubber band around S

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 4: Convex Hulls - Carleton University

Convex Hulls

Convex Hulls

I Let P = {p0, . . . , pn−1} be a set of points in the planeI The convex hull of S

I the smallest convex set that contains S .I stretch a rubber band around S and let it go

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 5: Convex Hulls - Carleton University

Convex Hulls

Upper and Lower Hulls

I The upper hull of S is a bit simpler

I get a rope with weights on both ends

I For the lower hull, use helium-baloons

I Convex hull = upper hull + lower hull

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 6: Convex Hulls - Carleton University

Convex Hulls

Upper and Lower Hulls

I The upper hull of S is a bit simplerI get a rope with weights on both ends

I For the lower hull, use helium-baloonsI Convex hull = upper hull + lower hull

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 7: Convex Hulls - Carleton University

Convex Hulls

Upper and Lower Hulls

I The upper hull of S is a bit simplerI get a rope with weights on both ends and throw it over S

I For the lower hull, use helium-baloonsI Convex hull = upper hull + lower hull

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 8: Convex Hulls - Carleton University

Convex Hulls

Upper and Lower Hulls

I The upper hull of S is a bit simplerI get a rope with weights on both ends and throw it over S

I For the lower hull, use helium-baloons

I Convex hull = upper hull + lower hull

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 9: Convex Hulls - Carleton University

Convex Hulls

Upper and Lower Hulls

I The upper hull of S is a bit simplerI get a rope with weights on both ends and throw it over S

I For the lower hull, use helium-baloonsI Convex hull = upper hull + lower hull

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 10: Convex Hulls - Carleton University

Convex Hulls

Summary so far

I Input: A set P of n points (unordered)

I Output: A list of the points of P on the convex hull — inclockwise order

I First compute the upper hull, then the lower hull

p1

p2

p3

p4p5

p6

p7

p8p9

p10

p11

p12

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 11: Convex Hulls - Carleton University

Convex Hulls

Summary so far

I Input: A set P of n points (unordered)I Output: A list of the points of P on the convex hull — in

clockwise order

I First compute the upper hull, then the lower hull

p1

p2

p3

p4p5

p6

p7

p8p9

p10

p11

p12

〈p5, p6, p7, p10, p11, p9, p1〉Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 12: Convex Hulls - Carleton University

Convex Hulls

Summary so far

I Input: A set P of n points (unordered)I Output: A list of the points of P on the convex hull — in

clockwise orderI First compute the upper hull, then the lower hull

p1

p2

p3

p4p5

p6

p7

p8p9

p10

p11

p12

〈p5, p6, p7, p10, p11, p9, p1〉Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 13: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

I Invented by Ron Graham in 1973

I Sorts the points by x-coordinate

I Constructs the upper hull incrementallyusing a stack

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 14: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

I Invented by Ron Graham in 1973

I Sorts the points by x-coordinate

I Constructs the upper hull incrementallyusing a stack

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 15: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

I Invented by Ron Graham in 1973

I Sorts the points by x-coordinate

I Constructs the upper hull incrementallyusing a stack

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 16: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p1

p2

p3

p4p5

p6

p7

p8p9

p10

p11

p12

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 17: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 18: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉

3. For i = 3 to n doI add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 19: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p2〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 20: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p2, p3〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 21: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p4〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 22: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p4, p5〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 23: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p6〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 24: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p6, p7〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 25: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p6, p8〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 26: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p6, p9〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 27: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p6, p9〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 28: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p6, p10〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 29: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan

1. Sort the points by x-coordinate

2. Create a stack s containing 〈p0, p1〉3. For i = 3 to n do

I add pi to the convex hull of p1, . . . , pi−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p6, p10, p11〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 30: Convex Hulls - Carleton University

Convex Hulls

Adding pi

I Suppose s contains the upper hull of p0, . . . , pi−1I We want to compute upper hull of p0, . . . , pi

1. while s.get(s.size()-2), s.get(s.size()-1), and pi form aleft turn

I s.remove(s.size()-1) (pop)

2. s.add(pi)

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p2, p3〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 31: Convex Hulls - Carleton University

Convex Hulls

Adding pi

I Suppose s contains the upper hull of p0, . . . , pi−1I We want to compute upper hull of p0, . . . , pi

1. while s.get(s.size()-2), s.get(s.size()-1), and pi form aleft turn

I s.remove(s.size()-1) (pop)

2. s.add(pi)

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p2, p3〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 32: Convex Hulls - Carleton University

Convex Hulls

Adding pi

I Suppose s contains the upper hull of p0, . . . , pi−1I We want to compute upper hull of p0, . . . , pi

1. while s.get(s.size()-2), s.get(s.size()-1), and pi form aleft turn

I s.remove(s.size()-1) (pop)

2. s.add(pi)

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p2〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 33: Convex Hulls - Carleton University

Convex Hulls

Adding pi

I Suppose s contains the upper hull of p0, . . . , pi−1I We want to compute upper hull of p0, . . . , pi

1. while s.get(s.size()-2), s.get(s.size()-1), and pi form aleft turn

I s.remove(s.size()-1) (pop)

2. s.add(pi)

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 34: Convex Hulls - Carleton University

Convex Hulls

Adding pi

I Suppose s contains the upper hull of p0, . . . , pi−1I We want to compute upper hull of p0, . . . , pi

1. while s.get(s.size()-2), s.get(s.size()-1), and pi form aleft turn

I s.remove(s.size()-1) (pop)

2. s.add(pi)

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p4〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 35: Convex Hulls - Carleton University

Convex Hulls

Adding pi

I Suppose s contains the upper hull of p0, . . . , pi−1I We want to compute upper hull of p0, . . . , pi

1. while s.get(s.size()-2), s.get(s.size()-1), and pi form aleft turn

I s.remove(s.size()-1) (pop)

2. s.add(pi)

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p0, p1, p4〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 36: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan – in Java

public static List <Point2D >

grahamScan(List <Point2D > p) {

Collections.sort(p, new XComparator ());

List <Point2D > s = new ArrayList <Point2D >();

s.add(p.get (0)); s.add(p.get (1));

for (int i = 2; i < p.size (); i++) {

Point2D pi = p.get(i);

while (s.size() >= 2

&& leftTurn(s.get(s.size()-2),

s.get(s.size()-1),

pi)) {

s.remove(s.size ()); // pop

}

s.add(pi);

}

return s;

}

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 37: Convex Hulls - Carleton University

Convex Hulls

Analysis of Graham’s Scan

I Graham’s Scan first sorts the dataI can be done O(n log n) time (see COMP3804)

I Creates a stack and pushes two valuesI takes O(1) time

I A for loop over n − 2 = O(n) values

I each iteration does some pop/remove operationsI how many?

I each iteration does 1 push/add operationI O(1) per iteration = O(n) overall

I Total: O(n log n) + O(n)

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 38: Convex Hulls - Carleton University

Convex Hulls

Analysis of Graham’s Scan

I Graham’s Scan first sorts the dataI can be done O(n log n) time (see COMP3804)

I Creates a stack and pushes two valuesI takes O(1) time

I A for loop over n − 2 = O(n) values

I each iteration does some pop/remove operationsI how many?

I each iteration does 1 push/add operationI O(1) per iteration = O(n) overall

I Total: O(n log n) + O(n)

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 39: Convex Hulls - Carleton University

Convex Hulls

Analysis of Graham’s Scan

I Graham’s Scan first sorts the dataI can be done O(n log n) time (see COMP3804)

I Creates a stack and pushes two valuesI takes O(1) time

I A for loop over n − 2 = O(n) values

I each iteration does some pop/remove operationsI how many?

I each iteration does 1 push/add operationI O(1) per iteration = O(n) overall

I Total: O(n log n) + O(n)

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 40: Convex Hulls - Carleton University

Convex Hulls

Analysis of Graham’s Scan

I Graham’s Scan first sorts the dataI can be done O(n log n) time (see COMP3804)

I Creates a stack and pushes two valuesI takes O(1) time

I A for loop over n − 2 = O(n) valuesI each iteration does some pop/remove operations

I how many?

I each iteration does 1 push/add operationI O(1) per iteration = O(n) overall

I Total: O(n log n) + O(n)

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 41: Convex Hulls - Carleton University

Convex Hulls

Analysis of Graham’s Scan

I Graham’s Scan first sorts the dataI can be done O(n log n) time (see COMP3804)

I Creates a stack and pushes two valuesI takes O(1) time

I A for loop over n − 2 = O(n) valuesI each iteration does some pop/remove operations

I how many?

I each iteration does 1 push/add operationI O(1) per iteration = O(n) overall

I Total: O(n log n) + O(n)

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 42: Convex Hulls - Carleton University

Convex Hulls

Analysis of Graham’s Scan

I Graham’s Scan first sorts the dataI can be done O(n log n) time (see COMP3804)

I Creates a stack and pushes two valuesI takes O(1) time

I A for loop over n − 2 = O(n) valuesI each iteration does some pop/remove operations

I how many?

I each iteration does 1 push/add operationI O(1) per iteration = O(n) overall

I Total: O(n log n) + O(n) + O(num. pop operations)

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 43: Convex Hulls - Carleton University

Convex Hulls

Analysis of Graham’s Scan

I Graham’s Scan first sorts the dataI can be done O(n log n) time (see COMP3804)

I Creates a stack and pushes two valuesI takes O(1) time

I A for loop over n − 2 = O(n) valuesI each iteration does some pop/remove operations

I how many?

I each iteration does 1 push/add operationI O(1) per iteration = O(n) overall

I Total: O(n log n) + O(n) + O(n)

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 44: Convex Hulls - Carleton University

Convex Hulls

Analysis of Graham’s Scan

I Graham’s Scan first sorts the dataI can be done O(n log n) time (see COMP3804)

I Creates a stack and pushes two valuesI takes O(1) time

I A for loop over n − 2 = O(n) valuesI each iteration does some pop/remove operations

I how many?

I each iteration does 1 push/add operationI O(1) per iteration = O(n) overall

I Total: O(n log n) + O(n) + O(n) = O(n log n)

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 45: Convex Hulls - Carleton University

Convex Hulls

Summary

I Theorem: Given a collection P of n points in the plane,Graham’s Scan can compute their upper hull in O(n log n)time.

I With two passes, we can compute the upper hull and lowerhull and attach them together to get the convex hull:

I Theorem: Given a collection P of n points in the plane, twoapplications of Graham’s Scan can compute their convex hullin O(n log n) time.

I By using a Deque, we only need one pass

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 46: Convex Hulls - Carleton University

Convex Hulls

Summary

I Theorem: Given a collection P of n points in the plane,Graham’s Scan can compute their upper hull in O(n log n)time.

I With two passes, we can compute the upper hull and lowerhull and attach them together to get the convex hull:

I Theorem: Given a collection P of n points in the plane, twoapplications of Graham’s Scan can compute their convex hullin O(n log n) time.

I By using a Deque, we only need one pass

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 47: Convex Hulls - Carleton University

Convex Hulls

Summary

I Theorem: Given a collection P of n points in the plane,Graham’s Scan can compute their upper hull in O(n log n)time.

I With two passes, we can compute the upper hull and lowerhull and attach them together to get the convex hull:

I Theorem: Given a collection P of n points in the plane, twoapplications of Graham’s Scan can compute their convex hullin O(n log n) time.

I By using a Deque, we only need one pass

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 48: Convex Hulls - Carleton University

Convex Hulls

Summary

I Theorem: Given a collection P of n points in the plane,Graham’s Scan can compute their upper hull in O(n log n)time.

I With two passes, we can compute the upper hull and lowerhull and attach them together to get the convex hull:

I Theorem: Given a collection P of n points in the plane, twoapplications of Graham’s Scan can compute their convex hullin O(n log n) time.

I By using a Deque, we only need one pass

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 49: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan with a deque

I Graham’s Scan can compute the convex hull in one-pass usinga deque

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p1, p0, p1〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 50: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan with a deque

I Graham’s Scan can compute the convex hull in one-pass usinga deque

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p2, p0, p1, p2〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 51: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan with a deque

I Graham’s Scan can compute the convex hull in one-pass usinga deque

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p3, p0, p1, p2, p3〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 52: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan with a deque

I Graham’s Scan can compute the convex hull in one-pass usinga deque

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p4, p3, p0, p1, p4〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 53: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan with a deque

I Graham’s Scan can compute the convex hull in one-pass usinga deque

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p5, p3, p0, p1, p4, p5〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 54: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan with a deque

I Graham’s Scan can compute the convex hull in one-pass usinga deque

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p6, p5, p3, p0, p1, p6〉

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 55: Convex Hulls - Carleton University

Convex Hulls

Graham’s Scan with a deque

I Graham’s Scan can compute the convex hull in one-pass usinga deque

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

〈p6, p5, p3, p0, p1, p6〉 . . .

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 56: Convex Hulls - Carleton University

Convex Hulls

Melkman’s Algorithm

I Graham’s Scan starts by sorting the thepoints by x-coordinate

I This means that p0, . . . , pn−1 becomes anon-self-intersecting path

I If points are already sorted thenGraham’s Scan takes O(n) time

I Melkman’s Algorithm:

I Works for any non-self-intersecting pathp0, . . . , pn−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 57: Convex Hulls - Carleton University

Convex Hulls

Melkman’s Algorithm

I Graham’s Scan starts by sorting the thepoints by x-coordinate

I This means that p0, . . . , pn−1 becomes anon-self-intersecting path

I If points are already sorted thenGraham’s Scan takes O(n) time

I Melkman’s Algorithm:

I Works for any non-self-intersecting pathp0, . . . , pn−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 58: Convex Hulls - Carleton University

Convex Hulls

Melkman’s Algorithm

I Graham’s Scan starts by sorting the thepoints by x-coordinate

I This means that p0, . . . , pn−1 becomes anon-self-intersecting path

I If points are already sorted thenGraham’s Scan takes O(n) time

I Melkman’s Algorithm:

I Works for any non-self-intersecting pathp0, . . . , pn−1

p3

p7

p9

p2p0

p1

p6

p5p8

p10

p11

p4

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques

Page 59: Convex Hulls - Carleton University

Convex Hulls

Melkman’s Algorithm

I Graham’s Scan starts by sorting the thepoints by x-coordinate

I This means that p0, . . . , pn−1 becomes anon-self-intersecting path

I If points are already sorted thenGraham’s Scan takes O(n) time

I Melkman’s Algorithm:I Works for any non-self-intersecting path

p0, . . . , pn−1

p1

p3

p11

p4p6

p7

p8

p0p2

p9

p10

p5

Pat Morin COMP2402/2002 Convex Hulls: An Application of Stacks and Deques