big o and algorithms (part 2)
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 PresentationTRANSCRIPT
Big O and Algorithms (Part 2)
Discrete Structures (CS 173)Madhsuudan Parthasarathy, University of Illinois 1
by Samuel Monnier http://algorithmic-worlds.net/
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
This class• Midterm review: induction form and
strategies
• Analysis of algorithm complexity– For loops– While loops– Recursion
3
Algorithms• Practice analyzing runtime based on
pseudocode
4
5
Key concept: instructions in the loops dominate
6
Key concept: runtime can be in terms of multiple input parameters
7
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
8
9
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
Binary search
11
12http://introcs.cs.princeton.edu/java/23recursion/hanoi/
13
Multiplying large numbersNaïve divide and conquer:
14
Multiplying large numbersMore clever divide and conquer (Karatsuba)
15
Thursday
• We won’t do NP next lecture.
• We will do advanced recursion/induction instead
• No reading for Thu.
16