Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 380: Design and Analysis of Algorithms

Similar presentations


Presentation on theme: "CSC 380: Design and Analysis of Algorithms"— Presentation transcript:

1 CSC 380: Design and Analysis of Algorithms
Dr. Curry Guinn

2 Quick Info Dr. Curry Guinn CIS 2015 guinnc@uncw.edu
Office Hours: MWF: 10:00am-11:00m and by appointment

3 Today Divide-and-Conquer Transform-and-Conquer
Multiplication of large integers Closest pair problem Transform-and-Conquer

4 Divide-and-Conquer Examples
Sorting: mergesort and quicksort Binary tree traversals Multiplication of large integers Matrix multiplication: Strassen’s algorithm Closest-pair and convex-hull algorithms Binary search: decrease-by-half (or degenerate divide&conq.) 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 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: a1 a2 … an b1 b2 … bn (d10) d11d12 … d1n (d20) d21d22 … d2n … … … … … … … (dn0) dn1dn2 … dnn Efficiency: n2 one-digit multiplications

6 Multiplying Integers x = 61,438,521 y = 94,736,407
Typical algorithm is N2 Use divide and conquer!

7 Multiplying Integers x = 61,438,521 y = 94,736,407 X = XL104+XR
YL=9,473 YR=6,407 X = XL104+XR Y = YL104+YR XY = XLYL108 + (XLYR + XRYL)104 + XRYR T(N) = 4T(N/2) + O(N) = O(N2)

8 Multiplying Integers XY = XLYL108 + (XLYR + XRYL)104 + XRYR
(XL – XR)(YR-YL)+XLYL+XRYR Use 1 multiplication plus 2 results already computed T(N) = 3T(N/2) + O(N) T(N) = O(Nlog23) = O(N1.59)

9 Example of Large-Integer Multiplication
2135  4014 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

10 Strassen’s Matrix Multiplication
Strassen observed [1969] that the product of two matrices can be computed as follows: C00 C A00 A B00 B01 = * C10 C A10 A B10 B11 M1 + M4 - M5 + M M3 + M5 = M2 + M M1 + M3 - M2 + M6 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

11 Analysis of Strassen’s Algorithm
Number of multiplications: M(n) = 7M(n/2), M(1) = 1 Solution: M(n) = 7log 2n = nlog 27 ≈ n vs. n3 of brute- force alg. Algorithms with better asymptotic efficiency are known but they are even more complex. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

12 Closest-Points Problem
p1 = (x1, y1) p2 = (x2, y2) Euclidean distance between p1 and p2 is [(x1-x2)2 + (y1-y2)2]1/2 Goal: Given N points, find closest pair Brute force algorithm?

13 Closest-Points Example

14 Closest-Points Problem
Brute force algorithm? O(N2) Divide and Conquer similar to maximum subsequence sum sort by X coordinate find closest two in left half (dL) find closest two in right half (dR) find closest two where 1 is in left, 1 in right (dC) closest is minimum

15 Closest-Points Example
dC dR dL

16 Closest-Points Problem
find closest two where 1 is in left, 1 in right (dC) To ensure O(NlogN) solution, this step must be done in O(N) time d = min(dL, dR) Only need to look at points d from center Still cannot do brute force on these points – what would be running time? Observe: for each point, only need to consider points that are less than d away in y coordinate There are a maximum of 5 points to consider Points must be separated by at least d Don't need to look at points that have already been processed

17 Example: Worst Case

18 Efficiency of the Closest-Pair Algorithm
Running time of the algorithm is described by T(n) = 2T(n/2) + M(n), where M(n)  O(n) By the Master Theorem (with a = 2, b = 2, d = 1) T(n)  O(n log n) A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

19 Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance simplification) a different representation of the same instance (representation change) a different problem for which an algorithm is already available (problem reduction) A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

20 Instance simplification - Presorting
Solve a problem’s instance by transforming it into another simpler/easier instance of the same problem Presorting Many problems involving lists are easier when list is sorted, e.g. searching computing the median (selection problem) checking if all elements are distinct (element uniqueness) Also: Topological sorting helps solving some problems for dags. Presorting is used in many geometric algorithms. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

21 How fast can we sort ? Efficiency of algorithms involving sorting depends on efficiency of sorting. Theorem (see Sec. 11.2): log2 n!  n log2 n comparisons are necessary in the worst case to sort a list of size n by any comparison-based algorithm. Note: About nlog2 n comparisons are also sufficient to sort array of size n (by mergesort). A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

22 Searching with presorting
Problem: Search for a given K in A[0..n-1] Presorting-based algorithm: Stage 1 Sort the array by an efficient sorting algorithm Stage 2 Apply binary search Efficiency: Θ(nlog n) + O(log n) = Θ(nlog n) Good or bad? Why do we have our dictionaries, telephone directories, etc. sorted? A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

23 Element Uniqueness with presorting
Presorting-based algorithm Stage 1: sort by efficient sorting algorithm (e.g. mergesort) Stage 2: scan array to check pairs of adjacent elements Efficiency: Θ(nlog n) + O(n) = Θ(nlog n) Brute force algorithm Compare all pairs of elements Efficiency: O(n2) Another algorithm? Hashing A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

24 Taxonomy of Searching Algorithms
List searching sequential search binary search interpolation search Tree searching binary search tree binary balanced trees: AVL trees, red-black trees multiway balanced trees: 2-3 trees, trees, B trees Hashing open hashing (separate chaining) closed hashing (open addressing) A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

25 For Next Class, Wednesday
Homework 5 due Tuesday after Spring Break


Download ppt "CSC 380: Design and Analysis of Algorithms"

Similar presentations


Ads by Google