big o and algorithms (part 2)

16
Big O and Algorithms (Part 2) Discrete Structures (CS 173) Madhsuudan Parthasarathy, University of Illinois 1 by Samuel Monnier http ://algorithmic-worlds.ne

Upload: saki

Post on 24-Feb-2016

27 views

Category:

Documents


0 download

DESCRIPTION

Big O and Algorithms (Part 2). by Samuel Monnier http ://algorithmic-worlds.net/. Discrete Structures (CS 173) Madhsuudan Parthasarathy, University of Illinois. Last class. Big-O Model algorithm efficiency as a function of the parameter size - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Big O and Algorithms (Part 2)

Big O and Algorithms (Part 2)

Discrete Structures (CS 173)Madhsuudan Parthasarathy, University of Illinois 1

by Samuel Monnier http://algorithmic-worlds.net/

Page 2: Big O and Algorithms (Part 2)

Last class• Big-O

– Model algorithm efficiency as a function of the parameter size

– Asymptotic analysis: how does it perform for large inputs?

– Keep only dominant terms, ignoring coefficients

– Applies to runtimes or memory usage or any other resources

2

Page 3: Big O and Algorithms (Part 2)

This class• Midterm review: induction form and

strategies

• Analysis of algorithm complexity– For loops– While loops– Recursion

3

Page 4: Big O and Algorithms (Part 2)

Algorithms• Practice analyzing runtime based on

pseudocode

4

Page 5: Big O and Algorithms (Part 2)

5

Key concept: instructions in the loops dominate

Page 6: Big O and Algorithms (Part 2)

6

Key concept: runtime can be in terms of multiple input parameters

Page 7: Big O and Algorithms (Part 2)

7

Key concept: n/2 recursion

http://www.youtube.com/watch?v=XaqR3G_NVoo

Page 8: Big O and Algorithms (Part 2)

Reachability algorithm reachable(G: graph; s,t: nodes in G) if (s=t) return true; Unmark all nodes in G. M=emptylist Mark node s and add it to M while (M is not empty) { p=pop(M) for every node q in neighbor(p) in G if q=t return true; else if q is not marked, mark q and add it to M } return false

8

Page 9: Big O and Algorithms (Part 2)

9

Page 10: Big O and Algorithms (Part 2)

Binary search

Searching a sorted array A[1..n]

binary_search(int A[], int key, int imin, int imax) { if (imax < imin) return -1; else { int imid = midpoint(imin, imax); if (A[imid] > key) return binsearch(A, key, imin, mid-1); else if (A[imid] < key) binsearch(A, key, imid+1, imax); else return imid; }}

10

Page 11: Big O and Algorithms (Part 2)

Binary search

11

Page 13: Big O and Algorithms (Part 2)

13

Page 14: Big O and Algorithms (Part 2)

Multiplying large numbersNaïve divide and conquer:

14

Page 15: Big O and Algorithms (Part 2)

Multiplying large numbersMore clever divide and conquer (Karatsuba)

15

Page 16: Big O and Algorithms (Part 2)

Thursday

• We won’t do NP next lecture.

• We will do advanced recursion/induction instead

• No reading for Thu.

16