1 / 147

CS 3343: Analysis of Algorithms

CS 3343: Analysis of Algorithms. Review for Exam 2. Exam 2. Closed book exam One cheat sheet allowed (limit to a single page of letter-size paper, double-sided) Tuesday, Nov 17, class time + 5 minutes Basic calculator (no graphing) is allowed but not necessary. Final grade calculation.

aorlando
Download Presentation

CS 3343: Analysis of Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 3343: Analysis of Algorithms Review for Exam 2

  2. Exam 2 • Closed book exam • One cheat sheet allowed (limit to a single page of letter-size paper, double-sided) • Tuesday, Nov 17, class time + 5 minutes • Basic calculator (no graphing) is allowed but not necessary

  3. Final grade calculation • Three components: HQP, Midterm, Final • HQ = HW + Quiz + Participation : (90% HW + 10% Quiz&Participation) : One lowest HW dropped • Midterm = 40% midterm1+ 60% midterm2 • Max for each component is 100. • Sort three components (highest, middle, lowest) • Final grade: • Case 1: HQP is not the highest 55% highest + 30% middle + 15% lowest • Case 2: HQP is highest 55% middle + 30% HQP + 15% lowest

  4. Materials covered • Quick Sort • Heap sort, priority queue • Linear time sorting algorithms • Order statistics • Dynamic programming • Greedy algorithm • Graph basics • Questions will be similar to homework / quizzes • Familiar with the algorithm procedure • Some analysis of time/space complexity • One or two problems on algorithm design

  5. QuickSort • Quicksort algorithm • Randimized Quicksort algorithm • Partition algorithm • Quicksort/Randomized Quicksort time complexity analysis Back

  6. £x x ≥x Quick sort • Quicksort an n-element array: • Divide:Partition the array into two subarrays around a pivotx such that elements in lower subarray £x£ elements in upper subarray. • Conquer: Recursively sort the two subarrays. • Combine: Trivial. Key:Linear-time partitioning subroutine.

  7. Pseudocode for quicksort • QUICKSORT(A, p, r) • ifp < r • thenq PARTITION(A, p, r)QUICKSORT(A, p, q–1) • QUICKSORT(A, q+1, r) Initial call:QUICKSORT(A, 1, n)

  8. Partition Code Partition(A, p, r) x = A[p]; // pivot is the first element i = p; j = r + 1; while (TRUE) { repeat i++; until A[i] > x | i >= j; repeat j--; until A[j] < x | j <= i; if (i < j) Swap (A[i], A[j]); else break; } swap (A[p], A[j]); return j;

  9. p r 6 10 5 8 13 3 2 11 x = 6 i j 6 10 5 8 13 3 2 11 scan i j 6 2 5 8 13 3 10 11 swap i j Partition example 6 2 5 8 13 3 10 11 scan i j 6 2 5 3 13 8 10 11 swap i j 6 2 5 3 13 8 10 11 scan j i p q r 3 2 5 6 13 8 10 11 final swap

  10. 3 2 5 6 11 8 10 13 2 3 5 6 10 8 11 13 2 3 5 6 8 10 11 13 2 3 5 6 8 10 11 13 6 10 5 8 11 3 2 13 Quick sort example

  11. Quicksort Runtimes • Best case runtime Tbest(n)  O(n log n) • Worst case runtime Tworst(n)  O(n2) • Average case runtime Tavg(n)  O(n log n) • Expected runtime of randomized quicksort is O(n log n)

  12. Randomized Partition • Randomly choose an element as pivot • Every time need to do a partition, throw a die to decide which element to use as the pivot • Each element has 1/n probability to be selected Rand-Partition(A, p, r){ d = random(); // draw a random number between 0 and 1 index = p + floor((r-p+1) * d); // p<=index<=q swap(A[p], A[index]); Partition(A, p, r); // now use A[p] as pivot }

  13. Running time of randomized quicksort • The expected running time is an average of all cases T(0) + T(n–1) + dn if 0:n–1 split, T(1) + T(n–2) + dn if 1:n–2 split, M T(n–1) + T(0) + dn if n–1:0 split, T(n) = Expectation

  14. Fact: • Need to Prove:T(n) ≤ c n log (n) • Assumption:T(k) ≤ ck log (k) for 0 ≤ k ≤ n-1 • Prove by induction If c ≥ 4

  15. Heaps • Heap definition • Heapify • Buildheap • Procedure and running time (!) • Heapsort • Comparison with other sorting algorithms in terms of running time, stability, and in-place. • Priority Queue operations Back

  16. 16 14 10 8 7 9 3 2 4 1 Heaps • A heap can be seen as a complete binary tree: Perfect binary tree 16 14 10 8 7 9 3 2 4 1

  17. Referencing Heap Elements • So… Parent(i) {return i/2;} Left(i) {return 2*i;} right(i) {return 2*i + 1;}

  18. Heap Operations: Heapify() Heapify(A, i) { //precondition: subtrees rooted at l and r are heaps l = Left(i); r = Right(i); if (l <= heap_size(A) && A[l] > A[i]) largest = l; else largest = i; if (r <= heap_size(A) && A[r] > A[largest]) largest = r; if (largest != i) { Swap(A, i, largest); Heapify(A, largest); } } //postcondition: subtree rooted at i is a heap Among A[l], A[i], A[r], which one is largest? If violation, fix it.

  19. Heapify() Example 16 4 10 14 7 9 3 2 8 1 A = 16 4 10 14 7 9 3 2 8 1

  20. Heapify() Example 16 4 10 14 7 9 3 2 8 1 A = 16 4 10 14 7 9 3 2 8 1

  21. Heapify() Example 16 4 10 14 7 9 3 2 8 1 A = 16 4 10 14 7 9 3 2 8 1

  22. Heapify() Example 16 14 10 4 7 9 3 2 8 1 A = 16 14 10 4 7 9 3 2 8 1

  23. Heapify() Example 16 14 10 4 7 9 3 2 8 1 A = 16 14 10 4 7 9 3 2 8 1

  24. Heapify() Example 16 14 10 8 7 9 3 2 4 1 A = 16 14 10 8 7 9 3 2 4 1

  25. Heapify() Example 16 14 10 8 7 9 3 2 4 1 A = 16 14 10 8 7 9 3 2 4 1

  26. BuildHeap() // given an unsorted array A, make A a heap BuildHeap(A) { heap_size(A) = length(A); for (i = length[A]/2 downto 1) Heapify(A, i); }

  27. BuildHeap() Example • Work through exampleA = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7} 4 1 3 2 16 9 10 14 8 7 A = 4 1 3 2 16 9 10 14 8 7

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

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

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

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

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

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

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

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

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

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

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

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

  40. Analyzing BuildHeap(): Tight • To Heapify() a subtree takes O(h) time where h is the height of the subtree • h = O(lg m), m = # nodes in subtree • The height of most subtrees is small • Fact: an n-element heap has at most n/2h+1 nodes of height h • CLR 6.3 uses this fact to prove that BuildHeap() takes O(n) time

  41. Heapsort Heapsort(A) { BuildHeap(A); for (i = length(A) downto 2) { Swap(A[1], A[i]); heap_size(A) -= 1; Heapify(A, 1); } }

  42. Heapsort Example • Work through exampleA = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7} 4 1 3 2 16 9 10 14 8 7 A = 4 1 3 2 16 9 10 14 8 7

  43. Heapsort Example • First: build a heap 16 14 10 8 7 9 3 2 4 1 A = 16 14 10 8 7 9 3 2 4 1

  44. Heapsort Example • Swap last and first 1 14 10 8 7 9 3 2 4 16 A = 1 14 10 8 7 9 3 2 4 16

  45. Heapsort Example • Last element sorted 1 14 10 8 7 9 3 2 4 16 A = 1 14 10 8 7 9 3 2 4 16

  46. Heapsort Example • Restore heap on remaining unsorted elements 14 8 10 4 7 9 3 2 1 16 Heapify A = 14 8 10 4 7 9 3 2 1 16

  47. Heapsort Example • Repeat: swap new last and first 1 8 10 4 7 9 3 2 14 16 A = 1 8 10 4 7 9 3 2 14 16

  48. Heapsort Example • Restore heap 10 8 9 4 7 1 3 2 14 16 A = 10 8 9 4 7 1 3 2 14 16

  49. Heapsort Example • Repeat 9 8 3 4 7 1 2 10 14 16 A = 9 8 3 4 7 1 2 10 14 16

  50. Heapsort Example • Repeat 8 7 3 4 2 1 9 10 14 16 A = 8 7 3 4 2 1 9 10 14 16

More Related