160 likes | 237 Views
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
E N D
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 • 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
This class • Midterm review: induction form and strategies • Analysis of algorithm complexity • For loops • While loops • Recursion
Algorithms • Practice analyzing runtime based on pseudocode
Key concept: runtime can be in terms of multiple input parameters
Key concept: n/2 recursion http://www.youtube.com/watch?v=XaqR3G_NVoo
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
Binary search Searching a sorted array A[1..n] binary_search(int A[],int key,intimin,intimax){ if(imax<imin)return -1; else{ intimid= midpoint(imin,imax); if(A[imid]> key)returnbinsearch(A, key,imin,mid-1); elseif(A[imid]< key) binsearch(A, key, imid+1,imax); elsereturnimid; } }
Multiplying large numbers Naïve divide and conquer:
Multiplying large numbers More clever divide and conquer (Karatsuba)
Thursday • We won’t do NP next lecture. • We will do advanced recursion/induction instead • No reading for Thu.