Divide and Conquer Neil Tang 4152010 CS 223

  • Slides: 18
Download presentation
Divide and Conquer Neil Tang 4/15/2010 CS 223 Advanced Data Structures and Algorithms 1

Divide and Conquer Neil Tang 4/15/2010 CS 223 Advanced Data Structures and Algorithms 1

Class Overview Ø Basic idea Ø Quick sort Ø Merge sort Ø Integer multiplication

Class Overview Ø Basic idea Ø Quick sort Ø Merge sort Ø Integer multiplication Ø Matrix multiplication CS 223 Advanced Data Structures and Algorithms 2

Basic Idea Ø Divide: divide the original problem to several sub-problems. Ø Conquer: Solve

Basic Idea Ø Divide: divide the original problem to several sub-problems. Ø Conquer: Solve the sub-problems recursively. Ø Combine: Combine the solutions to sub-problems to form a solution for the original problem. CS 223 Advanced Data Structures and Algorithms 3

Merge Sort Ø Divide: Divide the N-element sequence into 2 subsequences of N/2 each.

Merge Sort Ø Divide: Divide the N-element sequence into 2 subsequences of N/2 each. Ø Conquer: Sort each subsequence recursively using merge sort. Ø Combine: Merge two sorted subsequences to produce a single sorted sequence. CS 223 Advanced Data Structures and Algorithms 4

Merge Sort CS 223 Advanced Data Structures and Algorithms 5

Merge Sort CS 223 Advanced Data Structures and Algorithms 5

Merge Sort Ø T(N) = 2 T(N/2) + N Ø a=2, b=2, f(N)=N Ø

Merge Sort Ø T(N) = 2 T(N/2) + N Ø a=2, b=2, f(N)=N Ø log 22 = 1 Ø So we have case 2, T(N) = (Nlog. N). CS 223 Advanced Data Structures and Algorithms 6

Quick Sort Ø Divide: Divide the sequence into 2 subsequences, s. t. each element

Quick Sort Ø Divide: Divide the sequence into 2 subsequences, s. t. each element in the 1 st subsequence is less than or equal to each element in the 2 nd subsequence. Ø Conquer: Sort each subsequence recursively using quick sort. Ø Combine: no work is needed. CS 223 Advanced Data Structures and Algorithms 7

Quick Sort people age 25 people age < 23 age people 23 age 25

Quick Sort people age 25 people age < 23 age people 23 age 25 people age < 30 30 CS 223 Advanced Data Structures and Algorithms people age 30 8

Quick Sort Ø T(N) = T(N-1) + N Ø The Master method does not

Quick Sort Ø T(N) = T(N-1) + N Ø The Master method does not work. Ø T(N) = O(N 2) CS 223 Advanced Data Structures and Algorithms 9

Integer Multiplication Ø Compute XY, where X and Y are N-digit integers. Ø Divide:

Integer Multiplication Ø Compute XY, where X and Y are N-digit integers. Ø Divide: X=XL 104+XR , Y=YL 104+YR Ø Conquer and combine: XY = XLYL 108 + (XLYR+ XRYL)104+XRYR CS 223 Advanced Data Structures and Algorithms 10

Integer Multiplication Ø T(N) = 4 T(N/2) + (N) Ø a=4, b=2, f(N)= (N)

Integer Multiplication Ø T(N) = 4 T(N/2) + (N) Ø a=4, b=2, f(N)= (N) Ø log 24 = 2 Ø So we have case 1, T(N) = (N 2). CS 223 Advanced Data Structures and Algorithms 11

A Better Algorithm CS 223 Advanced Data Structures and Algorithms 12

A Better Algorithm CS 223 Advanced Data Structures and Algorithms 12

Integer Multiplication Ø T(N) = 3 T(N/2) + (N) Ø a=3, b=2, f(N)= (N)

Integer Multiplication Ø T(N) = 3 T(N/2) + (N) Ø a=3, b=2, f(N)= (N) Ø log 23 = 1. 59 Ø So we have case 1, T(N) = (N 1. 59). CS 223 Advanced Data Structures and Algorithms 13

Matrix Multiplication Ø Compute AB, where A and B are N N matrix. Ø

Matrix Multiplication Ø Compute AB, where A and B are N N matrix. Ø Divide: Divide each matrix to 4 quadrants. Ø Conquer and combine: CS 223 Advanced Data Structures and Algorithms 14

Matrix Multiplication CS 223 Advanced Data Structures and Algorithms 15

Matrix Multiplication CS 223 Advanced Data Structures and Algorithms 15

Matrix Multiplication Ø T(N) = 8 T(N/2) + (N 2) Ø a=8, b=2, f(N)=

Matrix Multiplication Ø T(N) = 8 T(N/2) + (N 2) Ø a=8, b=2, f(N)= (N 2) Ø log 28 = 3 Ø So we have case 1, T(N) = (N 3). CS 223 Advanced Data Structures and Algorithms 16

A Better Algorithm CS 223 Advanced Data Structures and Algorithms 17

A Better Algorithm CS 223 Advanced Data Structures and Algorithms 17

Matrix Multiplication Ø T(N) = 7 T(N/2) + (N 2) Ø a=7, b=2, f(N)=

Matrix Multiplication Ø T(N) = 7 T(N/2) + (N 2) Ø a=7, b=2, f(N)= (N 2) Ø log 27 = 2. 81 Ø So we have case 1, T(N) = (N 2. 81). CS 223 Advanced Data Structures and Algorithms 18