Divide and Conquer Merge sort and quick sort Binary search

Slides:



Advertisements
Similar presentations
A simple example finding the maximum of a set S of n numbers.
Advertisements

1 Theory I Algorithm Design and Analysis (2 - Trees: traversal and analysis of standard search trees) Prof. Th. Ottmann.
Divide-and-Conquer The most-well known algorithm design strategy:
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.
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
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 7 2  9 4   2   4   7
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
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.
Divide and Conquer Strategy
Binary Search Trees (BST)
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Advanced Algorithms Analysis and Design
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Analysis of Algorithms
Data Structure and Algorithms
CSCE 210 Data Structures and Algorithms
Trees Chapter 11 (continued)
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Trees Chapter 11 (continued)
Chapter 4 Divide-and-Conquer
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull
Binary Search Tree (BST)
Introduction to the Design and Analysis of Algorithms
Chapter 6 Transform-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4: Divide and Conquer
Decrease-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Unit-2 Divide and Conquer
Data Structures Review Session
Decrease-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 11 Limitations of Algorithm Power
Design and Analysis of Algorithms
Chapter 4 Divide-and-Conquer
Divide and Conquer Merge sort and quick sort Binary search
Binary Trees, Binary Search Trees
Trees.
Transform and Conquer Transform and Conquer Transform and Conquer.
Divide-and-Conquer The most-well known algorithm design strategy:
Topic 5: Heap data structure heap sort Priority queue
Transform and Conquer Transform and Conquer Transform and Conquer.
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Divide and Conquer Merge sort and quick sort Binary search Multiplication of large integers Strassen’s Matrix Multiplication Closest pair problem

Expected Outcomes Students should be able to Explain the ideas of binary search, multiplication of large integers, and Strassen’s matrix multiplication algorithms Analyze the time complexity of the above algorithms

Binary Search an Iterative Algorithm Very efficient algorithm for searching in sorted array: K vs A[0] . . . A[m] . . . A[n-1] If K = A[m], stop (successful search); otherwise, continue searching by the same method in A[0..m-1] if K < A[m] and in A[m+1..n-1] if K > A[m] Binary search is clearly based on recursive idea, it can be easily implemented as a nonrecursive algorithm, too. Here is pesudocode for this nonrecursive version: ALGORITHM BinarySearch(A[0..n-1], K) l  0; r  n-1 while l  r do // l and r crosses over can’t find K m  (l+r)/2 if K = A[m] return m //the key is found else if K < A[m] r  m-1 //the key is on the left half of the array else l  m+1 // the key is on the right half of the array return -1

Binary Search – a Recursive Algorithm ALGORITHM BinarySearchRecur(A[0..n-1], l, r, K) if l > r return –1 else m  (l + r) / 2 if K = A[m] return m else if K < A[m] return BinarySearchRecur(A[0..n-1], l, m-1, K) return BinarySearchRecur(A[0..n-1], m+1, r, K)

Analysis of Binary Search Let us find the number of key comparisons in the Worst-Case (successful /fail) Cw (n). The worst case inputs include all arrays that do not contain a given search key. Since after one comparison the algorithm faces the same situation but for an array half the size, we get the following recurrence relation: Cw (n) = Cw( n/2 ) + 1 for n>1, Cw (1) = 1 ---------- (4.2) For simplicity n = 2k so, Cw(2k) = k + 1 = log2 n +1 -----------------(4.3) Equation (4.3) for n = 2k can be tweaked to get a solution valid for an positive integer n: Cw(n) =  log2 n +1 = log2(n+1) -----------------(4.4)

This is VERY fast: e.g., Cw(106) = 20 by applying equation 4.4 Best-case: successful Cb (n) = 1, fail Cb (n) =  log2 n +1 Average-case: The average number of key comparisons made by binary search is only smaller than that in the worst case: Cavg (n) ≈ log2 n More accurate, average comparison in a successful and unsuccessful search are Cavg (n) ≈ log2 n – 1 and Cavg (n) ≈ log2 (n+1)

Remarks on Binary Search Optimal for searching in a sorted array Limitations: must be a sorted array (not linked list) Bad (degenerate) example of divide-and-conquer Has a continuous counterpart called bisection method for solving equations in one unknown f(x) = 0 (see Sec. 12.4)

Binary Tree Traversals Definitions A binary tree T is defined as a finite set of nodes that is either empty or consists of a root and two disjoint binary trees TL and TR called, respectively, the left and right subtree of the root. The height of a tree is defined as the length of the longest path from the root to a leaf. Problem: find the height of a binary tree.

Find the Height of a Binary Tree ALGORITHM Height(T) //Computes recursively the height of a binary tree //Input: A binary tree T //Output: The height of T if T =  return –1 else return max{Height(TL), Height(TR)} + 1 Analysis: Number of comparisons of a tree T with : 2n + 1 Number of comparisons of height is the same as number of additions: A(n(T)) = A(n(TL)) + A(n(TR)) +1 for n>0, A(0) = 0 The solution is A(n) = n

Binary Tree Traversals– preorder, inorder, and postorder traversal Binary tee traversal: visit all nodes of a binary tree recursively. Write a recursive preorder binary tree traversal algorithm. Algorithm Preorder(T) //Implement the preorder traversal of a binary tree //Input: Binary tree T (with labeled vertices) //Output: Node labels listed in preorder if T ‡  write label of T’s root Preorder(TL) Preorder(TR)

Multiplication of Large Integers Consider the problem of multiplying two (large) n-digit integers represented by arrays of their digits such as: A = 12345678901357986429 B = 87654321284820912836 The grade-school algorithm: a1 a2 … an b1 b2 … bn (d10) d11d12 … d1n (d20) d21d22 … d2n … … … … … … … (dn0) dn1dn2 … dnn Efficiency: n2 one-digit multiplications

First Divide-and-Conquer Algorithm A small example: A  B where A = 2135 and B = 4014 A = (21·102 + 35), B = (40 ·102 + 14) So, A  B = (21 ·102 + 35)  (40 ·102 + 14) = 21  40 ·104 + (21  14 + 35  40) ·102 + 35  14 In general, if A = A1A2 and B = B1B2 (where A and B are n-digit, A1, A2, B1, B2 are n/2-digit numbers), A  B = A1  B1·10n + (A1  B2 + A2  B1) ·10n/2 + A2  B2 Recurrence for the number of one-digit multiplications M(n): M(n) = 4M(n/2), M(1) = 1 Solution: M(n) = n2

Second Divide-and-Conquer Algorithm A  B = A1  B1·10n + (A1  B2 + A2  B1) ·10n/2 + A2  B2 The idea is to decrease the number of multiplications from 4 to 3: (A1 + A2 )  (B1 + B2 ) = A1  B1 + (A1  B2 + A2  B1) + A2  B2, I.e., (A1  B2 + A2  B1) = (A1 + A2 )  (B1 + B2 ) - A1  B1 - A2  B2, 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) = 3log 2n = nlog 23 ≈ n1.585

Strassen’s Matrix Multiplication Strassen observed [1969] that the product of two matrices can be computed as follows: C00 C01 A00 A01 B00 B01 = * C10 C11 A10 A11 B10 B11 M1 + M4 - M5 + M7 M3 + M5 = M2 + M4 M1 + M3 - M2 + M6

Formulas for Strassen’s Algorithm M1 = (A00 + A11)  (B00 + B11) M2 = (A10 + A11)  B00 M3 = A00  (B01 - B11) M4 = A11  (B10 - B00) M5 = (A00 + A01)  B11 M6 = (A10 - A00)  (B00 + B01) M7 = (A01 - A11)  (B10 + B11)

Analysis of Strassen’s Algorithm 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) = 7log 2n = nlog 27 ≈ n2.807 vs. n3 of brute-force alg. Algorithms with better asymptotic efficiency are known but they are even more complex.

Standard vs Strassen: Practical Multiplications Additions Standard alg. 100 1,000,000 990,000 Strassen’s alg. 411,822 2,470,334 1000 1,000,000,000 999,000,000 264,280,285 1,579,681,709 10,000 1012 9.99*1011 0.169*1012

Happy National’s day! Assignment + Wish 4.1 (3, 6, 9) 4.2 (2, 5, 6, 8) 4.3 (6) 4.4 (2, 8) Happy National’s day!