Introduction to Algorithms Part 2: Greedy Algorithms Dynamic ...
Big O and Algorithms (Part 2)
description
Transcript of Big O and Algorithms (Part 2)
![Page 1: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/1.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/2.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/3.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/4.jpg)
Algorithms• Practice analyzing runtime based on
pseudocode
4
![Page 5: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/5.jpg)
5
Key concept: instructions in the loops dominate
![Page 6: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/6.jpg)
6
Key concept: runtime can be in terms of multiple input parameters
![Page 7: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/7.jpg)
7
Key concept: n/2 recursion
http://www.youtube.com/watch?v=XaqR3G_NVoo
![Page 8: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/8.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/9.jpg)
9
![Page 10: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/10.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/11.jpg)
Binary search
11
![Page 12: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/12.jpg)
12http://introcs.cs.princeton.edu/java/23recursion/hanoi/
![Page 13: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/13.jpg)
13
![Page 14: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/14.jpg)
Multiplying large numbersNaïve divide and conquer:
14
![Page 15: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/15.jpg)
Multiplying large numbersMore clever divide and conquer (Karatsuba)
15
![Page 16: Big O and Algorithms (Part 2)](https://reader036.fdocuments.us/reader036/viewer/2022062310/5681657f550346895dd815ef/html5/thumbnails/16.jpg)
Thursday
• We won’t do NP next lecture.
• We will do advanced recursion/induction instead
• No reading for Thu.
16