Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms

Similar presentations


Presentation on theme: "Introduction to Algorithms"— Presentation transcript:

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

2 Chapter 4 Divide-and-Conquer Acknowledgment :
Some of the slides are based on a tutorial made by Kruse and Ryba Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

3 Convex Hull 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. 2 1 3 4 6 5

4 Brute force Solution 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 )

5 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 Pmax P2 P1

6 QuickHull Algorithm 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 Worst case (almost like quick sort) : O(n2)

7 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

8 Convex Hull: Runtime 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)

9 Convex Hull: Runtime T(n) = 2 T(n/2) + n Solves to T(n) = (n log n)
Runtime Recurrence: T(n) = 2 T(n/2) + n Solves to T(n) = (n log n)

10 Merging in O(n) time Find upper and lower tangents in O(n) time
4 Find upper and lower tangents in O(n) time Compute the convex hull of AB: 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 5 3 6 2 1 7 A B

11 Finding the lower tangent in O(n) time
3 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 } while T not lower tangent to convex hull of B do{ b=b } } 4 4=b 2 3 5 5 a=2 6 1 7 1 can be checked in constant time right turn or left turn? Note that to determine if some line, T = AB, is a lower tangent to the left hull you only need to check the 2 points on either side of point A (A-1 and A+1). If both these points are above the line T then all the points in the left hull are above the line T. That result comes from the fact that the hulls are convex. Note that you have to be careful about accessing the A-1 and A+1 elements so you don’t go out of the array bounds. A B T is lower tangent if all the points are above the line

12 Convex Hull – Divide & Conquer
Split set into two, compute convex hull of both, combine. Convex Hull – Divide & Conquer

13 Convex Hull – Divide & Conquer
Split set into two, compute convex hull of both, combine. Convex Hull – Divide & Conquer

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

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

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

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

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

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

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

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

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

23 Merging two convex hulls.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

41 Chapter 5 Decrease-and-Conquer
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

42 Decrease-and-Conquer
Reduce problem instance to smaller instance of the same problem Solve smaller instance Extend solution of smaller instance to obtain solution to original instance Also referred to as inductive or incremental approach

43 3 Types of Decrease and Conquer
Decrease by a constant (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.

44 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 Try to get the students involved in coming up with these: Brute Force: an= a*a*a*a*...*a n Divide and conquer: an= an/2 * an/2 (more accurately, an= an/2 * a n/2│) Decrease by one: an= an-1* a (one hopes a student will ask what is the difference with brute force here: there is none in the resulting algorithm, of course, but you can arrive at it in two different ways) Decrease by constant factor: an= (an/2) (again, if no student asks about it, be sure to point out the difference with divide and conquer. Here there is a significant difference that leads to a much more efficient algorithm – in divide and conquer we recompute an/2 T(n) = T(n/a) + a-1 = (a-1) n = when a = 2

45 Insertion Sort To 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, | 4 6 | | 8 5 | 5

46 Write a Pseudocode for Insertion Sort

47 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


Download ppt "Introduction to Algorithms"

Similar presentations


Ads by Google