CS473 - Algorithms I



Similar documents
CS473 - Algorithms I

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Matrix-Chain Multiplication

Full and Complete Binary Trees

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

FUZZY CLUSTERING ANALYSIS OF DATA MINING: APPLICATION TO AN ACCIDENT MINING SYSTEM

Mathematical Induction. Lecture 10-11

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Closest Pair Problem

Analysis of Algorithms I: Optimal Binary Search Trees

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Sample Induction Proofs

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

1/1 7/4 2/2 12/7 10/30 12/25

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Zabin Visram Room CS115 CS126 Searching. Binary Search

Divide-and-conquer algorithms

Unit 4: Dynamic Programming

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Approximation Algorithms

Dynamic Programming. Lecture Overview Introduction

Analysis of Algorithms I: Binary Search Trees

Solutions to Homework 6

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Binary Heap Algorithms

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Math 115A HW4 Solutions University of California, Los Angeles. 5 2i 6 + 4i. (5 2i)7i (6 + 4i)( 3 + i) = 35i + 14 ( 22 6i) = i.

csci 210: Data Structures Recursion

From Last Time: Remove (Delete) Operation

5.1 Bipartite Matching

Research Tools & Techniques

Converting a Number from Decimal to Binary

Lecture 1: Course overview, circuits, and formulas

Scheduling a sequence of tasks with general completion costs

Partitioning and Divide and Conquer Strategies

Matrix Algebra. Some Basic Matrix Laws. Before reading the text or the following notes glance at the following list of basic matrix algebra laws.

Randomized algorithms

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

Data Structures and Algorithms Written Examination

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Search Trees CMPSC 122

Binary Multiplication

Unit 18 Determinants

Algorithms Chapter 12 Binary Search Trees

Warshall s Algorithm: Transitive Closure

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

Analysis of Algorithms, I

CS 575 Parallel Processing

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Computing exponents modulo a number: Repeated squaring

A Branch and Bound Algorithm for Solving the Binary Bi-level Linear Programming Problem

Data Structures. Algorithm Performance and Big O Analysis

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Fast Fourier Transform: Theory and Algorithms

Algorithm Design and Analysis

The Running Time of Programs

Lecture 13: The Knapsack Problem

Binary search algorithm

Lecture 4 Online and streaming algorithms for clustering

Algorithms and Data Structures

Faster deterministic integer factorisation

Algorithms. Margaret M. Fleck. 18 October 2010

Analysis of Binary Search algorithm and Selection Sort algorithm

Continued Fractions and the Euclidean Algorithm

Appendix: Solving Recurrences [Fa 10] Wil Wheaton: Embrace the dark side! Sheldon: That s not even from your franchise!

How To Create A Tree From A Tree In Runtime (For A Tree)

Section IV.1: Recursive Algorithms and Recursion Trees

8 Square matrices continued: Determinants

Binary Heaps. CSE 373 Data Structures

Similarity and Diagonalization. Similar Matrices

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

5.4 Closest Pair of Points

Algorithms and data structures

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

Notes on Determinant

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

Lecture 6 Online and streaming algorithms for clustering

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

CS/COE

Sample Questions Csci 1112 A. Bellaachia

A note on companion matrices

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Classification of Cartan matrices

CMSC 451 Design and Analysis of Computer Algorithms 1

Introduction to data structures

Optimal Binary Search Trees Meet Object Oriented Programming

Chapter 7. Matrices. Definition. An m n matrix is an array of numbers set out in m rows and n columns. Examples. (

Near Optimal Solutions

Optimal Algorithms for 2 n AB Games A Graph-Partition Approach *

Lecture 2 Matrix Operations

AN ALGORITHM FOR DETERMINING WHETHER A GIVEN BINARY MATROID IS GRAPHIC

Transcription:

CS473 - Algorithms I Lecture 4 The Divide-and-Conquer Design Paradigm View in slide-show mode 1

Reminder: Merge Sort Input array A sort this half sort this half Divide Conquer merge two sorted halves Combine 2

The Divide-and-Conquer Design Paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving them recursively. 3. Combine subproblem solutions. CS473 Lecture 4 3

Example: Merge Sort 1. Divide: Trivial. 2. Conquer: Recursively sort 2 subarrays. 3. Combine: Linear- time merge. T(n) = 2 T(n/2) + Θ(n) # subproblems subproblem size work dividing and combining CS473 Lecture 4 4

Master Theorem: Reminder T(n) = at(n/b) + f(n) Case 1: T(n) = Case 2: T(n) = Case 3: and a f (n/b) c f (n) for c < 1 T(n) = 5

Merge Sort: Solving the Recurrence T(n) = 2 T(n/2) + Θ(n) a = 2, b = 2, f(n) = Θ(n), n log ba = n Case 2: T(n) = holds for k = 0 T(n) = Θ (nlgn) 6

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 7

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 8

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 9

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 10

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 11

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 CS473 Lecture 4 12

Recurrence for Binary Search T(n) = 1 T(n/2) + Ө(1) # subproblems subproblem size work dividing and combining CS473 Lecture 4 13

Binary Search: Solving the Recurrence T(n) = T(n/2) + Θ(1) a = 1, b = 2, f(n) = Θ(1), n log ba = n0 = 1 Case 2: T(n) = holds for k = 0 T(n) = Θ (lgn) 14

Powering a Number Problem: Compute a n, where n is a natural number Naive-Power (a, n) powerval 1 for i 1 to n powerval powerval. a return powerval What is the complexity? T(n) = Θ (n) 15

Powering a Number: Divide & Conquer Basic idea: a n = a n/2. a n/2 even a (n-1)/2. a (n-1)/2. a if n is if n is odd 16

Powering a Number: Divide & Conquer POWER (a, n) if n = 0 then return 1 else if n is even then val POWER (a, n/2) return val * val else if n is odd then val POWER (a, (n-1)/2) return val * val * a 17

Powering a Number: Solving the Recurrence T(n) = T(n/2) + Θ(1) a = 1, b = 2, f(n) = Θ(1), n log ba = n0 = 1 Case 2: T(n) = holds for k = 0 T(n) = Θ (lgn) 18

Matrix Multiplication Input : A = [a ij ], B = [b ij ]. Output: C = [c ij ] = A. B. i, j = 1, 2,..., n. c 11 c 12... c 1n a 11 a 12... a 1n b 11 b 12... b 1n c 21 c 22... c 2n = a 21 a 22... a 2n. b 21 b 22... b 2n............... c n1 c n2... c nn a n1 a n2... a nn b n1 b n2... b nn............ c ij = 1 k n a ik.b kj CS473 Lecture 4 19

Standard Algorithm for i 1 to n do for j 1 to n do c ij 0 for k 1 to n do c ij c ij + a ik. b kj Running time = Θ(n 3 ) CS473 Lecture 4 20

Matrix Multiplication: Divide & Conquer IDEA: Divide the n x n matrix into 2x2 matrix of (n/2)x(n/2) submatrices C A B c 11 =. c 12 a 11 a 12 b 11 b 12 c 21 c 22 a 21 a 22 b 21 b 22 c 11 = a 11 b 11 + a 12 b 21 21

Matrix Multiplication: Divide & Conquer IDEA: Divide the n x n matrix into 2x2 matrix of (n/2)x(n/2) submatrices C A B c 11 b 11 =. c 12 a 11 a 12 b 12 c 21 c 22 a 21 a 22 b 21 b 22 c 12 = a 11 b 12 + a 12 b 22 22

Matrix Multiplication: Divide & Conquer IDEA: Divide the n x n matrix into 2x2 matrix of (n/2)x(n/2) submatrices C A B c 11 a 11 a 12 =. c 12 b 11 b 12 c 21 c 22 a 21 a 22 b 21 b 22 c 21 = a 21 b 11 + a 22 b 21 23

Matrix Multiplication: Divide & Conquer IDEA: Divide the n x n matrix into 2x2 matrix of (n/2)x(n/2) submatrices C A B c 11 c 12 a 11 b 11 =. a 12 b 12 c 21 c 22 a 21 a 22 b 21 b 22 c 22 = a 21 b 12 + a 22 b 22 24

Matrix Multiplication: Divide & Conquer C A B c 11 c 12 a 11 a 12 b =. 11 b 12 c 21 c 22 a 21 a 22 b 21 b 22 c 11 = a 11 b 11 + a 12 b 21 c 12 = a 11 b 12 + a 12 b 22 c 21 = a 21 b 11 + a 22 b 21 8 mults of (n/2)x(n/2) submatrices 4 adds of (n/2)x(n/2) submatrices c 22 = a 21 b 12 + a 22 b 22 25

Matrix Multiplication: Divide & Conquer MATRIX-MULTIPLY (A, B) // Assuming that both A and B are nxn matrices if n = 1 then return A * B else partition A, B, and C as shown before c 11 = MATRIX-MULTIPLY (a 11, b 11 ) + MATRIX-MULTIPLY (a 12, b 21 ) c 12 = MATRIX-MULTIPLY (a 11, b 12 ) + MATRIX-MULTIPLY (a 12, b 22 ) c 21 = MATRIX-MULTIPLY (a 21, b 11 ) + MATRIX-MULTIPLY (a 22, b 21 ) c 22 = MATRIX-MULTIPLY (a 21, b 12 ) + MATRIX-MULTIPLY (a 22, b 22 ) return C 26

Matrix Multiplication: Divide & Conquer Analysis T(n) = 8 T(n/2) + Θ(n 2 ) 8 recursive calls each subproblem has size n/2 submatrix addition 27

Matrix Multiplication: Solving the Recurrence T(n) = 8 T(n/2) + Θ(n 2 ) a = 8, b = 2, f(n) = Θ(n 2 ), n log ba = n 3 Case 1: T(n) = T(n) = Θ (n 3 ) No better than the ordinary algorithm! 28

Matrix Multiplication: Strassen s Idea C A B c 11 c 12 a 11 a 12 b =. 11 b 12 c 21 c 22 a 21 a 22 b 21 b 22 Compute c 11, c 12, c 21, and c 22 using 7 recursive multiplications 29

Matrix Multiplication: Strassen s Idea P 1 = a 11 x (b 12 - b 22 ) P 2 = (a 11 + a 12 ) x b 22 P 3 = (a 21 + a 22 ) x b 11 P 4 = a 22 x (b 21 - b 11 ) P 5 = (a 11 + a 22 ) x (b 11 + b 22 ) P 6 = (a 11 - a 22 ) x (b 21 + b 22 ) P 7 = ( a 11 - a 21 ) x (b 11 + b 12 ) Reminder: Each submatrix is of size (n/2)x(n/2) Each add/sub operation takes Θ(n 2 ) time Compute P 1..P 7 using 7 recursive calls to matrix-multiply How to compute c ij using P 1.. P 7? 30

Matrix Multiplication: Strassen s Idea P 1 = a 11 x (b 12 - b 22 ) P 2 = (a 11 + a 12 ) x b 22 P 3 = (a 21 + a 22 ) x b 11 P 4 = a 22 x (b 21 - b 11 ) P 5 = (a 11 + a 22 ) x (b 11 + b 22 ) P 6 = (a 11 - a 22 ) x (b 21 + b 22 ) P 7 = ( a 11 - a 21 ) x (b 11 + b 12 ) c 11 = P 5 + P 4 P 2 + P 6 c 12 = P 1 + P 2 c 21 = P 3 + P 4 c 22 = P 5 + P 1 P 3 P 7 7 recursive multiply calls 18 add/sub operations Does not rely on commutativity of multiplication 31

Matrix Multiplication: Strassen s Idea P 1 = a 11 x (b 12 - b 22 ) P 2 = (a 11 + a 12 ) x b 22 P 3 = (a 21 + a 22 ) x b 11 P 4 = a 22 x (b 21 - b 11 ) P 5 = (a 11 + a 22 ) x (b 11 + b 22 ) P 6 = (a 11 - a 22 ) x (b 21 + b 22 ) P 7 = ( a 11 - a 21 ) x (b 11 + b 12 ) e.g. Show that c 12 = P 1 +P 2 c 12 = P 1 + P 2 = a 11 (b 12 b 22 )+(a 11 +a 12 )b 22 = a 11 b 12 -a 11 b 22 +a 11 b 22 +a 12 b 22 = a 11 b 12 +a 12 b 22 32

Strassen s Algorithm 1. Divide: Partition A and B into (n/2) x (n/2) submatrices. Form terms to be multiplied using + and. 2. Conquer: Perform 7 multiplications of (n/2) x (n/2) submatrices recursively. 3. Combine: Form C using + and on (n/2) x (n/2) submatrices. Recurrence: T(n) = 7 T(n/2) + Ө(n 2 ) 33

Strassen s Algorithm: Solving the Recurrence T(n) = 7 T(n/2) + Θ(n 2 ) a = 7, b = 2, f(n) = Θ(n 2 ), n log ba = n lg7 Case 1: T(n) = T(n) = Θ (n lg7 ) Note: lg7 2.81 34

Strassen s Algorithm The number 2.81 may not seem much smaller than 3 But, it is significant because the difference is in the exponent. Strassen s algorithm beats the ordinary algorithm on today s machines for n 30 or so. Best to date: Θ(n 2.376... ) (of theoretical interest only) 35

VLSI Layout: Binary Tree Embedding Problem: Embed a complete binary tree with n leaves into a 2D grid with minimum area. Example: 36

Binary Tree Embedding Use divide and conquer root LEFT SUBTREE RIGHT SUBTREE 1. Embed the root node 2. Embed the left subtree 3. Embed the right subtree What is the min-area required for n leaves? 37

Binary Tree Embedding root H(n/2) EMBED LEFT SUBTREE HERE EMBED RIGHT SUBTREE HERE H(n) = H(n/2) + 1 W(n/2) W(n/2) W(n) = 2W(n/2) + 1 38

Binary Tree Embedding Solve the recurrences: W(n) = 2W(n/2) + 1 H(n) = H(n/2) + 1 W(n) = Ө(n) H(n) = Ө(lgn) Area(n) = Ө(nlgn) 39

Binary Tree Embedding Example: W(n) H(n) 40

Binary Tree Embedding: H-Tree Use a different divide and conquer method root left subtree 1 subtree 2 subtree 3 right subtree 4 1. Embed root, left, right nodes 2. Embed subtree 1 3. Embed subtree 2 4. Embed subtree 3 5. Embed subtree 4 What is the min-area required for n leaves? 41

Binary Tree Embedding: H-Tree H(n/4) H(n/4) SUBTREE 1 SUBTREE 2 SUBTREE 3 SUBTREE 4 W(n) = 2W(n/4) + 1 H(n) = 2H(n/4) + 1 W(n/4) W(n/4) 42

Binary Tree Embedding: H-Tree Solve the recurrences: W(n) = 2W(n/4) + 1 H(n) = 2H(n/4) + 1 W(n) = Ө( ) H(n) = Ө( ) Area(n) = Ө(n) 43

Binary Tree Embedding: H-Tree Example: W(n) H(n) 44

Correctness Proofs Proof by induction commonly used for D&C algorithms Base case: Show that the algorithm is correct when the recursion bottoms out (i.e., for sufficiently small n) Inductive hypothesis: Assume the alg. is correct for any recursive call on any smaller subproblem of size k (k < n) General case: Based on the inductive hypothesis, prove that the alg. is correct for any input of size n 45

Example Correctness Proof: Powering a Number POWER (a, n) if n = 0 then return 1 else if n is even then val POWER (a, n/2) return val * val else if n is odd then val POWER (a, (n-1)/2) return val * val * a 46

Example Correctness Proof: Powering a Number Base case: POWER (a, 0) is correct, because it returns 1 Ind. hyp: Assume POWER (a, k) is correct for any k < n General case: In POWER (a, n) function: If n is even: val = a n/2 (due to ind. hyp.) it returns val. val = a n If n is odd: val = a (n-1)/2 (due to ind. hyp.) it returns val. val. a = a n The correctness proof is complete 47

Maximum Subarray Problem Input: An array of values Output: The contiguous subarray that has the largest sum of elements Input array: 13-3 -25 20-3 -16-23 18 20-7 12-22 -4 7 the maximum contiguous subarray 48

Maximum Subarray Problem: Divide & Conquer Basic idea: Divide the input array into 2 from the middle Pick the best solution among the following: 1. The max subarray of the left half 2. The max subarray of the right half 3. The max subarray crossing the mid-point Crosses the mid-point A Entirely in the left half Entirely in the right half 49

Maximum Subarray Problem: Divide & Conquer Divide: Trivial (divide the array from the middle) Conquer: Recursively compute the max subarrays of the left and right halves Combine: Compute the max-subarray crossing the midpoint (can be done in Θ(n) time). Return the max among the following: 1. the max subarray of the left subarray 2. the max subarray of the right subarray 3. the max subarray crossing the mid-point See textbook for the detailed solution. 50

Conclusion Divide and conquer is just one of several powerful techniques for algorithm design. Divide-and-conquer algorithms can be analyzed using recurrences and the master method (so practice this math). Can lead to more efficient algorithms CS473 Lecture 4 51