Merge Sort Algorithm: Analysis, Recurrences, and Solving Techniques, Slides of Computer Science

An in-depth look at the merge sort algorithm, including its analysis, recurrences, and solving techniques using the master theorem and the substitution method. It also includes examples and explanations of asymptotic notation and the iteration method.

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms
Merge Sort
Solving Recurrences
The Master Theorem
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Merge Sort Algorithm: Analysis, Recurrences, and Solving Techniques and more Slides Computer Science in PDF only on Docsity!

Algorithms

Merge Sort

Solving Recurrences

The Master Theorem

Administrative Question

  • Who here cannot make Monday-Wednesday

office hours at 10 AM?

  • If nobody, should we change class time?

Review: Asymptotic Notation

  • Upper Bound Notation:
    • f(n) is O(g(n)) if there exist positive constants c

and n 0 such that f(n) ≤ c ⋅ g(n) for all n ≥ n (^0)

  • Formally, O(g(n)) = { f(n): ∃ positive constants c

and n 0 such that f(n) ≤ c ⋅ g(n) ∀ n ≥ n 0

  • Big O fact:
    • A polynomial of degree k is O( nk )

Review: Asymptotic Notation

  • Asymptotic lower bound:
    • f(n) is Ω (g(n)) if ∃ positive constants c and n 0 such

that 0 ≤ c⋅g(n) ≤ f(n) ∀ n ≥ n 0

  • Asymptotic tight bound:
    • f(n) is Θ (g(n)) if ∃ positive constants c 1 , c 2 , and n 0

such that c 1 g(n) ≤ f(n) ≤ c 2 g(n) ∀ n ≥ n 0

  • f(n) = Θ(g(n)) if and only if

f(n) = O(g(n)) AND f(n) = Ω(g(n))

Merge Sort

MergeSort(A, left, right) {

if (left < right) { mid = floor((left + right) / 2); 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?)

Merge Sort: Example

  • Show MergeSort() running on the array

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

Recurrences

  • The expression:

is a recurrence.

  • Recurrence: an equation that describes a function

in terms of its value on smaller functions

cn n

n T

c n

T n

Recurrence Examples

n

n

c s n

s n

n s n n

n s n

c n

n T

c n

T n

cn n b

n aT

c n

T n

Solving Recurrences

  • The substitution method (CLR 4.1)
    • A.k.a. the “making a good guess method”
    • Guess the form of the answer, then use induction

to find the constants and show that solution works

  • Examples:
    • T(n) = 2T(n/2) + Θ(n)  T(n) = Θ(n lg n)
    • T(n) = 2T(n/2) + n  ???

Solving Recurrences

  • The substitution method (CLR 4.1)
    • A.k.a. the “making a good guess method”
    • Guess the form of the answer, then use induction

to find the constants and show that solution works

  • Examples:
    • T(n) = 2T(n/2) + Θ(n)  T(n) = Θ(n lg n)
    • T(n) = 2T(n/2) + n  T(n) = Θ(n lg n)
    • T(n) = 2T(n/2 )+ 17) + n  ???

Solving Recurrences

  • Another option is what the book calls the

“iteration method”

  • Expand the recurrence
  • Work some algebra to express as a summation
  • Evaluate the summation
  • We will show several examples

c s n n

n s n

  • s(n) =

c + s(n-1)

c + c + s(n-2)

2c + s(n-2)

2c + c + s(n-3)

3c + s(n-3)

kc + s(n-k) = ck + s(n-k)

c s n n

n s n

  • So far for n >= k we have
    • s(n) = ck + s(n-k)
  • What if k = n?
    • s(n) = cn + s(0) = cn
  • So
  • Thus in general
    • s(n) = cn

c s n n

n s n

n s n n

n s n

  • s(n)

= n + s(n-1)

= n + n-1 + s(n-2)

= n + n-1 + n-2 + s(n-3)

= n + n-1 + n-2 + n-3 + s(n-4)

= …

= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)