CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.

Slides:



Advertisements
Similar presentations
Divide-and-Conquer CIS 606 Spring 2010.
Advertisements

Lecture 3: Parallel Algorithm Design
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
A simple example finding the maximum of a set S of n numbers.
Anany Levitin ACM SIGCSE 1999SIG. Outline Introduction Four General Design Techniques A Test of Generality Further Refinements Conclusion.
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
DIVIDE AND CONQUER. 2 Algorithmic Paradigms Greedy. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer.
The Divide-and-Conquer Strategy
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.
Convex Hulls in Two Dimensions Definitions Basic algorithms Gift Wrapping (algorithm of Jarvis ) Graham scan Divide and conquer Convex Hull for line intersections.
CS4413 Divide-and-Conquer
Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Theory of Algorithms: Divide and Conquer
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
By Groysman Maxim. Let S be a set of sites in the plane. Each point in the plane is influenced by each point of S. We would like to decompose the plane.
1 Dynamic Programming Jose Rolim University of Geneva.
Advanced Topics in Algorithms and Data Structures Lecture 7.1, page 1 An overview of lecture 7 An optimal parallel algorithm for the 2D convex hull problem,
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
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.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
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.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
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.
5 -1 Chapter 5 The Divide-and-Conquer Strategy A simple example finding the maximum of a set S of n numbers.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
CS 8833 Algorithms Algorithms Dynamic Programming.
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 Andreas Klappenecker [based on slides by Prof. Welch]
Divide and Conquer Strategy
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 16.
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
Lecture 3: Parallel Algorithm Design
Chapter 4 Divide-and-Conquer
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull
Chapter 4 Divide-and-Conquer
CSCE350 Algorithms and Data Structure
Objective of This Course
Unit-2 Divide and Conquer
Topic: Divide and Conquer
Chapter 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
CSC 380: Design and Analysis of Algorithms
Richard Anderson Lecture 14 Divide and Conquer
Introduction to Algorithms
CSC 380: Design and Analysis of Algorithms
Major Design Strategies
Richard Anderson Lecture 14, Winter 2019 Divide and Conquer
Major Design Strategies
Complexity Theory: Foundations
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms Khawaja Mohiuddin Assistant Professor, Department of Computer Sciences Bahria University, Karachi Campus, Contact: Lecture # 11 – Divide And Conquer

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 2  What is Divide And Conquer Strategy?  The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original (larger) instance by combining these solutions

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 3  What is Divide And Conquer Strategy?

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 4  Example : Multiplication of Large Integers  Consider the problem of multiplying two (large) N-digit integers represented by arrays of their digits such as: A = B = The grade-school algorithm: a 1 a 2 … a n b 1 b 2 … b n a 1 b n a 2 b n … a n b n a 1 b n-1 a 2 b n-1 … a n b n-1 … … … … … … … … + a 1 b 1 a 2 b 1 … a n b n1 Runtime: O(n 2 ) single-digit multiplications

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 5  Example : Multiplication of Large Integers – First Algorithm  In general, if A = A 1 A 2 and B = B 1 B 2 (where A and B are n-digit, A 1, A 2, B 1, B 2 are n/2-digit numbers), A  B = A 1  B 1 ·10 n + (A 1  B 2 + A 2  B 1 ) ·10 n/2 + A 2  B 2  Example: A  B where A = 2135 and B = 4014 A = (21· ), B = (40 · ) So, A  B = (21 · )  (40 · ) = 21  40 · (21   40) ·  14  Recurrence for the number of one-digit multiplications M(N): M(N) = 4M(N/2), M(1) = 1 Solution: M(N) = 4 log 2 N = N log 2 4 = N 2 Runtime = O(N 2 )

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 6  Example : Multiplication of Large Integers- Analysis  This procedure can be used recursively to reduce multiplication of two integers of any length to a sequence of single-digit multiplication  This certainly makes the algorithm easier, but it does not reduce the computational complexity  At each stage in the recursion, the number of multiplications increases by a factor of four and the size of the numbers being multiplied decreases by a factor of two – so the number of single-digit multiplications in each of the multiplications decreases by a factor of four  These effects cancel and there is no reduction in the number of single-digit multiplications. It is still of order O(N 2 )

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 7  Example : Multiplication of Large Integers- Second Algorithm A  B = A 1  B 1 ·10 n + (A 1  B 2 + A 2  B 1 ) ·10 n/2 + A 2  B 2 The idea is to decrease the number of multiplications from 4 to 3: (A 1 + A 2 )  (B 1 + B 2 ) = A 1  B 1 + (A 1  B 2 + A 2  B 1 ) + A 2  B 2, i.e., (A 1  B 2 + A 2  B 1 ) = (A 1 + A 2 )  (B 1 + B 2 ) - A 1  B 1 - A 2  B 2, which requires only 3 multiplications at the expense of (4-1) extra add/sub. Recurrence for the number of multiplications M(n): M(N) = 3M(N/2), M(1) = 1 Solution: M(n) = 3 log 2 N = N log 2 3 ≈ N 1.585

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 8  Example : Multiplication of Large Integers- Second Algorithm Example  To compute the product of and 6789, choose B = 10 and m = 3. Then we decompose the input operands using the resulting base (B m = 1000), as:  = 12 · = 6 · Only three multiplications, which operate on smaller integers, are used to compute three partial results:  z 2 = 12 × 6 = 72 z 0 = 345 × 789 = z 1 = ( ) × ( ) − z 2 − z 0 = 357 × 795 − 72 − = − 72 − = We get the result by just adding these three partial results, shifted accordingly (and then taking carries into account by decomposing these three inputs in base 1000 like for the input operands):  result = z 2 · B 2m + z 1 · B m + z 0, i.e. result = 72 · · =

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 9  Example : Conventional Matrix Multiplication  Brute Force Algorithm  Total : 8 Multiplications  Runtime: O(N 3 )

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 10  Example : Strassen’s Matrix Multiplication  Divide each of the matrices into four sub-matrices, each of dimensions N/2 x N/2  Strassen derived the following result  7 Multiplications

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 11  Example : Strassen’s Matrix Multiplication  M 1 = (a 00 + a 11 ) * (b 00 + b 11 )  M 2 = (a 10 + a 11 ) * b 00  M 3 = a 00 * (b 01 - b 11 )  M 4 = a 11 * (b 10 - b 00 )  M 5 = (a 00 + a 01 ) * b 11  M 6 = (a 10 - a 00 ) * (b 00 + b 01 )  M 7 = (a 01 - a 11 ) * (b 10 + b 11 )

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 12  Example : Strassen’s Matrix Multiplication - Analysis  If N is not a power of 2, matrices can be padded with zeros. Number of multiplications: M(N) = 7M(N/2), M(1) = 1 Solution: M(N) = 7 log 2 N = N log 2 7 ≈ N vs. N 3 of brute-force algorithm Algorithms with better asymptotic efficiency are known but they are even more complex and not used in practice

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 13  Triangulation  Triangulation is a process that takes a region of space and divides it into sub-regions  The space may be of any dimensions (e.g. 2D), and the sub-regions typically are shaped as triangles (2D), tetrahedrons (3D), and so on.  The goal of triangulation is to produce a sub-division that obeys certain “nice” properties  For example: one in which the output triangles are nearly equilateral

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 14  Triangulation  Triangulation is used in many real-world applications  One such application is the finite element simulation, in which a partial differential equation (PDE) is simulated over a continuous surface or space by first using a triangulation to break the space into small, discrete element, then simulating the PDE discretely over the elements  Finite element simulations are common in fluid flow applications and similar problems  Other important applications of triangulation include:

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 15  Triangulation  Surface Approximation: A continuous surface is approximated by a mesh of triangles. This is used in graphic applications (example, face representation), compression of surface representations, interpolation of surfaces, and construction applications (example, optimizing dirt movement to contour a region for new construction)  Nearest Neighbour Structure Identification: The triangulation establishes data identifying the nearest neighbours of each point in the triangulated data set. This is potentially useful for clustering applications

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 16  Convex Hulls  Given a set of points S, the convex hull problem is to find the smallest convex region that contains all points in S  The boundary of the region is called the convex hull of S, and is usually specified by a set of points and their neighbour relation  Convex hulls are well-defined in any dimension, a 2-dimensional example is given in Figure 11.1  The following is a summary of the algorithms for finding convex hulls based on D&C strategy  Mergehull: Time Complexity: T(sort) + O(N)  Quickhull: Time Complexity: O(N) best, O(N 2 ) Worst, O(NLogN) Average  Kirkpatrick-Seidel: Time Complexity: O(Nlog H)

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 17  Example – Convex Hulls Convex Hulls Figure 11.1: Example of 2-D Convex Hull Extreme Point

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 18  Quickhull Algorithm  The Quickhull algorithm is one of the algorithms based on the recursive divide-and- conquer method for finding convex hulls  It is similar in structure to the Quicksort algorithm  Quickhull works in (2-D) by locating three points A, B, and C on the convex hull of its input S (with C above both A and B), then recursively refine the hull between AC and CB  When the algorithm completes, the upper half-hull between A and B will be complete

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 19  Quickhull Algorithm 1. Find the farthest point C from the line joining A and B. To do this is the line-side test can be used, as it returns the area of the triangle ABC, and the triangle with largest area is the one that has the farthest point C. Note that, when building the upper half-hull, C should be above AB, whereas C should be above the inverse line BA when building the lower half-hull 2. Discard all points in triangle ABC, as they will not be on the boundary of the final hull (since ABC is contained within the hull) 3. Recurse on (A,C) and (C,B) to fully refine the hull between A and C, and between C and B

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 20  Quickhull Algorithm 4. Connect the two resulting hulls at C 5. Repeat for the lower half-hull, and connect it at A and B  The worst case running time of the Quickhull algorithm is O(N 2 ), for if the splits (choices of C) are bad, the algorithm may only eliminate one point on each recursion, resulting in a recursion depth of N and a per-recursion cost of N.  Despite this poor worst-case bound, Quickhull runs in O(NlogN) time for most “realistic” point sets  In practice, it can run as fast as O(N), since the algorithm discards points in Step 2, reducing the size of the input set for further recursions

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 21  Where Divide & Conquer Fails?  All divide & conquer (D & C) problems do not exhibit optimal problem division  Consider the problem of visiting all the vertices of a graph in a simple cycle, starting from a chosen vertex  The problem is to find the cycle that yields the smallest total edge-cost  We divide the problem into two parts – the set of vertices to be visited before the last vertex, and the last vertex  The optimal tour is A→B→C→D→E→A, but when the last vertex E is removed, the optimal tour for sub-problems (ABCD) is A→B→C→D→A, which will not lead to complete optimal solution.

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Divide And Conquer 22  Where Divide & Conquer Fails?  The type of problems for which divide-and-conquer is not suitable are: 1. Where the solution of size n depends upon n sub-solutions, each of size (n – 1) 2. Overlapping sub-problems

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Summary 23  What is Divide and Conquer Strategy?  A Multiplication Algorithm and Its Analysis  Triangulation  Convex Hulls  Where Divide And Conquer Fails?