47
Introduction to Algorithms Rabie A. Ramadan [email protected] http://www. rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Introduction to Algorithms Rabie A. Ramadan [email protected] . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Embed Size (px)

Citation preview

Page 1: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Introduction to Algorithms

Rabie A. [email protected]

http://www. rabieramadan.org

6

Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Page 2: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Chapter 4Chapter 4

Divide-and-ConquerDivide-and-Conquer

Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

Acknowledgment :Some of the slides are based on a tutorial made by Kruse and Ryba

Page 3: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Convex Hull

3

Given a set of pins on a pinboard

And a rubber band around them

How does the rubber band look when it snaps tight?

We represent the convex hull as the sequence of points on the convex hull polygon, in counter-clockwise order.

0

2

1

3

4

6

5

Page 4: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Brute force Solution

4

Based on the following observation:

•A line segment connecting two points Pi and Pj of a set on n points is a part of the convex hull’s boundary if and only if all the other points of the set lie on the same side of the straight line through these two points.

•We need to try every pair of points O(n3 )

Page 5: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Quickhull Algorithm

Convex hull: smallest convex set that includes given points.

Assume points are sorted by x-coordinate values Identify extreme points P1 and P2 (leftmost and rightmost) Compute upper hull recursively:

• find point Pmax that is farthest away from line P1P2

• compute the upper hull of the points to the left of line P1Pmax

• compute the upper hull of the points to the left of line PmaxP2

Compute lower hull in a similar manner

P1

P2

Pmax

Page 6: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

QuickHull Algorithm

6

How to find the How to find the PPmaxmax point 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

Worst case (almost like quick sort) : O(n2)

Page 7: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Convex Hull: Divide & Conquer

7

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

Page 8: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Convex Hull: Runtime

8

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)

Page 9: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Convex Hull: Runtime

9

Runtime Recurrence:

T(n) = 2 T(n/2) + n

Solves to T(n) = (n log n)

Page 10: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Merging in O(n) time

10

Find upper and lower tangents in O(n) time

Compute the convex hull of AB:

walk clockwise around the convex hull of A, starting with left endpoint of lower tangent

when hitting the left endpoint of the upper tangent, cross over to the convex hull of B

walk counterclockwise around the convex hull of B

when hitting right endpoint of the lower tangent we’re done

This takes O(n) time

A B

1

2

3

45

6

7

Page 11: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Finding the lower tangent in O(n) time

11

can be checked in constant time

right turn or left turn?

a = rightmost point of A

b = leftmost point of B

while T=ab not lower tangent to both convex hulls of A and B do{

while T not lower tangent to convex hull of A do{ a=a-1 } while T not lower tangent to convex hull of B do{ b=b+1 } }

A B

0

a=2

1

5

3

4

0

1

2

3

4=b

5

6

7

T is lower tangent if all the points are above the line

Page 12: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Convex Hull – Divide & Conquer

12

Split set into two, compute convex hull of both, combine.

Page 13: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Convex Hull – Divide & Conquer

13

Split set into two, compute convex hull of both, combine.

Page 14: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

14

Split set into two, compute convex hull of both, combine.

Page 15: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

15

Split set into two, compute convex hull of both, combine.

Page 16: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

16

Split set into two, compute convex hull of both, combine.

Page 17: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

17

Split set into two, compute convex hull of both, combine.

Page 18: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

18

Split set into two, compute convex hull of both, combine.

Page 19: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

19

Split set into two, compute convex hull of both, combine.

Page 20: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

20

Split set into two, compute convex hull of both, combine.

Page 21: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

21

Split set into two, compute convex hull of both, combine.

Page 22: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

22

Split set into two, compute convex hull of both, combine.

Page 23: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

23

Merging two convex hulls.

Page 24: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

24

Merging two convex hulls: (i) Find the lower tangent.

Page 25: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

25

Merging two convex hulls: (i) Find the lower tangent.

Page 26: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

26

Merging two convex hulls: (i) Find the lower tangent.

Page 27: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

27

Merging two convex hulls: (i) Find the lower tangent.

Page 28: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

28

Merging two convex hulls: (i) Find the lower tangent.

Page 29: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

29

Merging two convex hulls: (i) Find the lower tangent.

Page 30: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

30

Merging two convex hulls: (i) Find the lower tangent.

Page 31: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

31

Merging two convex hulls: (i) Find the lower tangent.

Page 32: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

32

Merging two convex hulls: (i) Find the lower tangent.

Page 33: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

33

Merging two convex hulls: (ii) Find the upper tangent.

Page 34: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

34

Merging two convex hulls: (ii) Find the upper tangent.

Page 35: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

35

Merging two convex hulls: (ii) Find the upper tangent.

Page 36: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

36

Merging two convex hulls: (ii) Find the upper tangent.

Page 37: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

37

Merging two convex hulls: (ii) Find the upper tangent.

Page 38: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

38

Merging two convex hulls: (ii) Find the upper tangent.

Page 39: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

39

Merging two convex hulls: (ii) Find the upper tangent.

Page 40: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

40

Merging two convex hulls: (iii) Eliminate non-hull edges.

Page 41: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Chapter 5 Chapter 5

Decrease-and-ConquerDecrease-and-Conquer

Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

Page 42: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Decrease-and-Conquer

1. Reduce problem instance to smaller instance of the same problem

2. Solve smaller instance

3. Extend solution of smaller instance to obtain solution to original instance

Also referred to as inductive or incremental approach

Page 43: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

3 Types of Decrease and Conquer

Decrease by a constant Decrease by a constant (usually by 1):(usually by 1):• insertion sort• graph traversal algorithms (DFS and BFS)• topological sorting• algorithms for generating permutations, subsets

Decrease by a constant factor (usually by half)• binary search and bisection method• exponentiation by squaring• multiplication à la russe

Variable-size decrease• Euclid’s algorithm• selection by partition• Nim-like games

This usually results in a recursive algorithm.

Page 44: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

What is the difference?

Consider the problem of exponentiation: Compute xn

Brute Force:

Divide and conquer:

Decrease by one:

Decrease by constant factor:

n-1 multiplications

T(n) = 2*T(n/2) + 1 = n-1

T(n) = T(n-1) + 1 = n-1

T(n) = T(n/a) + a-1

= (a-1) n

= when a = 2

alogn2log

Page 45: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Insertion SortTo sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1]

in its proper place among the sorted A[0..n-2]

Usually implemented bottom up (nonrecursively) (Video)

Example: Sort 6, 4, 1, 8, 5

6 | 4 1 8 5 4 6 | 1 8 5 1 4 6 | 8 5 1 4 6 8 | 5 1 4 5 6 8

Page 46: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Write a Pseudocode for Insertion Sort

Page 47: Introduction to Algorithms Rabie A. Ramadan rabie@rabieramadan.org . rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials

Analysis of Insertion Sort Time efficiency

Cworst(n) = n(n-1)/2 Θ(n2)

Cbest(n) = n - 1 Θ(n) (also fast on almost sorted arrays)

Space efficiency: in-place

Best elementary sorting algorithm overall