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

Slides:



Advertisements
Similar presentations
2/9/06CS 3343 Analysis of Algorithms1 Convex Hull  Given a set of pins on a pinboard  And a rubber band around them  How does the rubber band look when.
Advertisements

1/13/15CMPS 3130/6130: Computational Geometry1 CMPS 3130/6130: Computational Geometry Spring 2015 Convex Hulls Carola Wenk.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
Chapter 5: Decrease and Conquer
Chap 5: Decrease & conquer. Objectives To introduce the decrease-and-conquer mind set To show a variety of decrease-and-conquer solutions: Depth-First.
Divide-and-Conquer The most-well known algorithm design strategy:
Convex Hulls Computational Geometry, WS 2007/08 Lecture 2 – Supplementary Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Problem Reduction Dr. M. Sakalli Marmara Unv, Levitin ’ s notes.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CS4413 Divide-and-Conquer
Reminder: Closest-Pair Problem Given a collection P of n points in  × , obtain the two points p1 and p2 such that their distance is less or equal that.
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating.
Theory of Algorithms: Divide and Conquer
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer.
Convex Hulls May Shmuel Wimer Bar Ilan Univ., Eng. Faculty Technion, EE Faculty.
8/29/06CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to Algorithms
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Design and Analysis of Algorithms - Chapter 5
Convex Hull Computation ● Applications of Convex Hull Computation ● Definitions ● Basic Math Functions ● Algorithms Algorithm Speed Discovered By Brute.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Divide and Conquer.
Convex Hull. What is the Convex Hull? Imagine a set of points on a board with a nail hammered into each point. Now stretch a rubber band over all the.
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull Strassen's Algorithm: Matrix Multiplication.
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 5 Decrease-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CMPS 3120: Computational Geometry Spring 2013
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 7.
Convex Hull. University of Manchester biologists used lasers to measure the minimum amount of skin required to wrap around the skeletons of modern-day.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
Convex Hull 2012/10/23. Convex vs. Concave A polygon P is convex if for every pair of points x and y in P, the line xy is also in P; otherwise, it is.
Now, Chapter 5: Decrease and Conquer Reduce problem instance to smaller instance of the same problem and extend solution Solve smaller instance Extend.
9/8/10CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Fall 2010 Triangulations and Guarding Art Galleries Carola Wenk.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
Chapter 4 Divide-and-Conquer
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Convex Hull.
CSCE350 Algorithms and Data Structure
Algorithm design techniques Dr. M. Gavrilova
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 5.
Decrease-and-Conquer
Algorithm Design Strategy Divide and Conquer Revisit
Decrease-and-Conquer
Decrease-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
CMPS 3130/6130: Computational Geometry Spring 2017
Divide-and-Conquer The most-well known algorithm design strategy:
CMPS 3120: Computational Geometry Spring 2013
Introduction to Algorithms
CSC 380: Design and Analysis of Algorithms
Presentation transcript:

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

Chapter 4 Divide-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

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

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(n 3 )

Quickhull Algorithm Convex hull: smallest convex set that includes given points. Assume points are sorted by x-coordinate values Identify extreme points P 1 and P 2 (leftmost and rightmost) Compute upper hull recursively: find point P max that is farthest away from line P 1 P 2 compute the upper hull of the points to the left of line P 1 P max compute the upper hull of the points to the left of line P max P 2 Compute lower hull in a similar manner P1P1 P2P2 P max

QuickHull Algorithm 6 How to find the P max point How to find the P max point P max maximizes the area of the triangle P max P 1 P 2 if tie, select the P max that maximizes the angle P max P 1 P 2 The points inside triangle P max P 1 P 2 can be excluded from further consideration Worst case (almost like quick sort) : O(n 2 )

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

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) O(n)

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

Merging in O(n) time 10  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 A B

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= =b T is lower tangent if all the points are above the line

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

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

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.

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

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

3 Types of Decrease and Conquer Decrease by a constant (usually by 1): 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.

What is the difference? Consider the problem of exponentiation: Compute x n 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

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)Video Example: Sort 6, 4, 1, 8, 5 6 | | | |

Write a Pseudocode for Insertion Sort

Analysis of Insertion Sort Time efficiency C worst (n) = n(n-1)/2  Θ(n 2 ) C best (n) = n - 1  Θ(n) (also fast on almost sorted arrays) Space efficiency: in-place Best elementary sorting algorithm overall