Docsity

Data Structures & Algorithms Review: Asymptotic Analysis, Sorting, & Dynamic Sets, Lecture notes of Algorithms and Programming

Partial preview of the text

Download Data Structures & Algorithms Review: Asymptotic Analysis, Sorting, & Dynamic Sets and more Lecture notes Algorithms and Programming in PDF only on Docsity!

CS 332: Algorithms

Review for Exam 1

Administrative

● Reminder: homework 3 due today

● Reminder: Exam 1 Wednesday, Feb 13

■ (^) 1 8.5x11 crib sheet allowed ○ (^) Both sides, mechanical reproduction okay ○ (^) You will turn it in with the exam

Review: Induction

● Suppose

■ (^) S(k) is true for fixed constant k ○ (^) Often k = 0 ■ (^) S(n)  S(n+1) for all n >= k

● Then S(n) is true for all n >= k

Proof By Induction

● Claim:S(n) is true for all n >= k

● Basis:

■ (^) Show formula is true when n = k

● Inductive hypothesis:

■ (^) Assume formula is true for an arbitrary n

● Step:

■ (^) Show that formula is then true for n+

Induction Example: Geometric Closed Form

● Prove a^0 + a^1 + … + an^ = (an+1^ - 1)/(a - 1) for all

a  1

■ (^) Basis: show that a^0 = (a0+1^ - 1)/(a - 1) a^0 = 1 = (a^1 - 1)/(a - 1) ■ (^) Inductive hypothesis: ○ (^) Assume a^0 + a^1 + … + an^ = (an+1^ - 1)/(a - 1) ■ (^) Step (show true for n+1): a^0 + a^1 + … + an+1^ = a^0 + a^1 + … + an^ + an+ = (an+1^ - 1)/(a - 1) + an+1^ = (an+1+1^ - 1)/(a - 1)

Review: Asymptotic Performance

● Asymptotic performance : How does algorithm
behave as the problem size gets very large?

○ (^) Running time ○ (^) Memory/storage requirements ■ (^) Use the RAM model: ○ (^) All memory equally expensive to access ○ (^) No concurrent operations ○ (^) All reasonable instructions take unit time  (^) Except, of course, function calls ○ (^) Constant word size

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))

Review: 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. // Code for this is in the book. It requires O(n) // time, and does require allocating O(n) space

Review: Analysis of Merge Sort

Statement Effort

● (^) So T(n) = (1) when n = 1, and 2T(n/2)

  • (n) when n > 1 ● (^) This expression is a recurrence 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) } }

Review: Solving Recurrences

● The substitution method

■ (^) 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 ■ (^) Example: merge sort ○ (^) T(n) = 2T(n/2) + cn ○ (^) We guess that the answer is O(n lg n) ○ (^) Prove it by induction ■ (^) Can similarly show T(n) = Ω(n lg n), thus Θ(n lg n)

Review: Solving Recurrences ● (^) The “iteration method” ■ (^) Expand the recurrence ■ (^) Work some algebra to express as a summation ■ (^) Evaluate the summation ● (^) We showed several examples including complex ones:       ^         1 1 ( ) cn n b n aT c n T n

Review: The Master Theorem

● if T(n) = aT(n/b) + f(n) then

 

( / ) ( ) for large
( ) AND
( ) log

log log log log log

c
af n b cf n n
f n n
f n n
f n O n
f n
n n
n
T n

a a a a a b b b b b

 

Review: Heaps

● A heap is a “complete” binary tree, usually

represented as an array:

16 4 10 14 7 9 3 2 8 1 A = 16 14 10 8 7 9 3 2 4 1