Introduction to Algorithms Introduction to Algorithms For insertion - - PowerPoint PPT Presentation

introduction to algorithms introduction to algorithms
SMART_READER_LITE
LIVE PREVIEW

Introduction to Algorithms Introduction to Algorithms For insertion - - PowerPoint PPT Presentation

Motivation Introduction to Algorithms Introduction to Algorithms For insertion sort (and other problems) as n For insertion sort (and other problems) as n doubles in size, the quadratic quadruples! Recursion & Merge Sort Recursion


slide-1
SLIDE 1

Introduction to Algorithms Introduction to Algorithms

Recursion & Merge Sort Recursion & Merge Sort CSE 680

  • Prof. Roger Crawfis

Motivation

For insertion sort (and other problems) as n For insertion sort (and other problems) as n

doubles in size, the quadratic quadruples!

Can we decrease n? Can we decrease n? What if we Divide the sort into smaller

pieces? pieces?

We can then solve those (Conquer them).

W d t b bl t bi th i

We need to be able to combine the pieces

in a manner simpler than quadratic.

Divide and Conquer q

Divide (into two equal parts) Divide (into two equal parts) Conquer (solve for each part separately)

C bi t l ti

Combine separate solutions Merge sort

Divide into two equal parts Sort each part using merge-sort

p g g (recursion!!!)

Merge two sorted subsequences

Merge Sort g

MergeSort(A, left, right) { if (left < right) { mid = floor((left + right) / 2); MergeSort(A, left, mid); MergeSort(A, left, mid); MergeSort(A, mid+1, right); Merge(A, left, mid, right); } } // Merge() takes two sorted subarrays of A and // merges them into a single sorted subarray of A // (how long should this take?) // (how long should this take?)

slide-2
SLIDE 2

Merge Sort: Example g p

Show MergeSort() running on the array Show MergeSort() running on the array

A = {10, 5, 7, 6, 1, 4, 8, 3, 2, 9}; { , , , , , , , , , }

Analysis of Merge Sort y g

Statement Effort Statement Effort

MergeSort(A, left, right) {

T(n)

if (left < right) { Θ(1) / Θ mid = floor((left + right) / 2); Θ(1) MergeSort(A, left, mid); T(n/2) MergeSort(A, mid+1, right); T(n/2) Merge(A, left, mid, right); Θ(n)

So T(n) = Θ(1) when n = 1 and

g ( , , , g ); ( ) } }

So T(n) Θ(1) when n 1, and

2T(n/2) + Θ(n) when n > 1

So what (more succinctly) is T(n)? So what (more succinctly) is T(n)?

Recurrences

The expression: The expression:

⎪ ⎪ ⎧ =1 n c ⎪ ⎪ ⎩ ⎪ ⎪ ⎨ > + ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ = 1 2 2 ) ( n cn n T n T

  • is a recurrence.

⎪ ⎩ ⎠ ⎝ 2

Recurrence: an equation that describes a function in terms of its value on smaller functions

Recursion Tree

1 2 3 4 5 6 7 8 1 3 5 8 2 4 6 7 log n 1 5 3 8 5 1 8 3 4 7 2 6 7 4 6 2

  • n comparisons per level
  • log n levels

t t l ti l

  • total runtime = n log n
slide-3
SLIDE 3

Recurrence Examples p

⎧ = 0 n ⎩ ⎨ ⎧ > − + = ) 1 ( ) ( n n n T c n T

Recurrence Examples p

⎧ = 0 n ⎩ ⎨ ⎧ > − + = ) 1 ( ) ( n n T n n n T

Recurrence Examples p

⎧ =1 n c ⎪ ⎪ ⎪ ⎨ ⎧ ⎞ ⎛ = 1 ) ( n n c n T ⎪ ⎪ ⎩ > + ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ 1 2 2 n c n T

Recurrence Examples p

⎧ ⎪ ⎪ ⎨ ⎧ = = 1 ) ( n c n T ⎪ ⎪ ⎩ ⎨ > + ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ = 1 ) ( n cn b n aT n T ⎩ ⎠ ⎝ b

slide-4
SLIDE 4

Solving Recurrences g

Chapter 4 will look at several methods to Chapter 4 will look at several methods to

solve these recursions:

Substitution method Substitution method Recursion-tree method Master method Master method