Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Exceptions Exceptions oops, mistake correction oops, mistake correction Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort.

Similar presentations


Presentation on theme: "1 Exceptions Exceptions oops, mistake correction oops, mistake correction Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort."— Presentation transcript:

1 1 Exceptions Exceptions oops, mistake correction oops, mistake correction Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort Determining Algorithm Efficiency Determining Algorithm Efficiency Substitution Method Cost Tree Method Master Method CSE 30331 Lecture 7 – Exceptions, Algorithms III

2 2 Quick Aside Group Project Guidelines … are posted on web page Due: Tuesday, September 26 th (correction) Initial Group membership Brief description of project you plan to complete Initial references you have found …

3 3 Reading Reminder More Algorithms Cormen: 4.1 – 4.3 QuickSort Cormen: Ch 7 (all) Ford & Topp: 15.1

4 4 Try & Multiple Catches – oops this is WRONG while (...) { try {... x = myOtherFunct(num); // might throw several exceptions... } catch (out_of_range& e) { // exception was a simple out of range error cout << e.what(); // inform user and... continue; // try again -- get a better input }... } catch (exception& e) { // if exception was not a range error, abort cout << e.what(); exit(2); }

5 5 Try & Multiple Catches – CORRECTED try { while (...) { try {... x = myOtherFunct(num); // might throw several exceptions... } catch (out_of_range& e) { // exception was a simple out of range error cout << e.what(); // inform user and... continue; // try again -- get a better input }... } catch (exception& e) { // if exception was not a range error, abort cout << e.what(); exit(2); }

6 6 Try & Multiple Catches - catch and throw again (except.cpp) try { while (...) { try { x = myOtherFunct(num); // might throw several exceptions } catch (out_of_range& e) { cout << e.what(); // inform user and... continue; // try again -- get a better input } catch (runtime_error& e) { // note: if catch (exception& e) here and throw, message lost throw e; // <--- don’t want to handle it here, so throw again } catch (exception& e) { // if exception was not a range error, abort cout << e.what(); exit(2); }

7 7 Issues with Vector & Matrix Your Vector “is a” vector So it does not need a private vector data member Can call existing vector functions like this vector ::at(i) vector ::operator[](i) vector (r,c,val) Your Matrix class... must use your Vector class NOT the STL vector

8 8 Quicksort Algorithm Select pivot value from middle of vector Partition vector into two sub-vectors One contains values smaller that pivot The other contains values larger than pivot Poor choice of pivot can lead to unbalanced pairs of sub-vectors – leading to deeper recursion Recursively sort each subvector

9 9 QuickSort() – sorts v[first,last) template void quicksort(vector & v, int first, int last) { int pivot Loc; T temp; if (last-first <= 1) return; //done else if (last–first == 2) { // only 2 values if (v[last] < v[first]) { temp = v[first]; v[first] = v[last]; v[last] = temp; } return; } else { // still sorting to be done pivotLoc = pivotIndex(v, first, last); quicksort(v, first, pivotLoc); quicksort(v, pivotLoc+1, last); }

10 10 Pivot Index Algorithm If only one element return its index Else Select middle value as pivot and swap it with first value Scan up from left and down from right until we find leftmost value larger than pivot and rightmost value less than pivot Swap these two values and repeat scan When scanUp and scanDown pass each other Copy value from scanDown position to first position Copy pivot into scanDown position Return scanDown position

11 11 pivotIndex() template int pivotIndex(vector & v, int first, int last) { int mid, scanUp, scanDown; T pivot, temp; if (first == last) return last; // empty partition else if (first == last-1) return first; // one element partition else { mid = (last + first) / 2; pivot = v[mid]; v[mid] = v[first]; v[first] = pivot; scanUp = first+1; scanDown = last-1; // continued...

12 12 pivotIndex() continued for (;;) { while (scanUp <= scanDown && v[scanUp] < pivot) scanUp++; // find leftmost large value while (pivot < v[scanDown]) scanDown--; // find rightmost small value if (scanUp >= scanDown) break; temp = v[scanUp]; // swap two values v[scanUp] = v[scanDown]; v[scanDown] = temp; scanUp++; scanDown--; // reset for next scan } v[first] = v[scanDown]; // move rightmost small value v[scanDown] = pivot; // put pivot there return scanDown; // return position of pivot }

13 13 Quicksort Example Quicksort recursive calls to partition a list into smaller and smaller sublists about a value called the pivot. Example: Let v be a vector containing 10 integer values: v = {800,150,300,650,550,500,400,350,450,900}

14 14 Quicksort Example (Cont…)

15 15 Quicksort Example (Cont…)

16 16 Quicksort Example (Cont…)

17 17 Quicksort Example (Cont…)

18 18 Quicksort Example (Cont…)

19 19 Quicksort Example (Cont…)

20 20 Quicksort Example (Cont…)

21 21 Quicksort Efficiency (Average) Levels in call tree (log n) Elements at each level (average case) Level 0 (1 vector of n elements) Level 1 (2 vectors of approx. n/2 elements) Level 2 (4 vectors of approx. n/4 elements) …. Level k (2 k vectors of approx. n/2 k elements) Each level has n elements & Θ(n) effort required to find all pivotIndexes & partitions at each level There are approx. k = log n levels Quicksort is Θ(n log n) in best and average cases

22 22 Quicksort (Worst Case) If pivot is largest or smallest value One sub-vector will be empty The other will contain n-1 values If this happens at every level of recursion The call tree has n-1 rather than log n levels The effort to find the pivot Index is then (n-1) + (n-2) + … + 2 + 1 = n(n-1)/2 So quicksort is Θ(n 2 ) in the worst case It is easy to see how this could happen if the first value is chosen as the pivot and the array is already sorted.

23 23 Determining Algorithm Efficiency First we must determine a recurrence relation for the cost of the recursive algorithm Then there are three primary methods we can use to determine the efficiency Θ(?) The Substitution Method The Cost Tree Method The Master Method

24 24 Substitution Step 1 – guess form of solution Step 2 – use induction to find the constants that show solution works Example: Cormen shows (on pages 63-64) that T(n) = 2T(n/2) + n = O(n lg n), for c ≥ 1 Can we prove T(n) = 2T(n/2)+n = Ω(n lg n)

25 25 Substitution Prove T(n) = 2T(n/2)+n = Ω(n lg n) Assume T(n/2) = 2T(n/4) + n/2 ≥ cn/2 lg n/2 Show T(n) ≥ cn lg n T(n) = 2T(n/2) + n ≥ 2(cn/2 lg n/2) + n = cn lg n – cn lg 2 + n = cn lg n – cn + n = cn lg n + (1-c)n = cn lg n, for c = 1

26 26 Substitution No good strategy to guessing correct solution Math may not work out unless … Subtract low order term rather than add Change form of variables, work induction, and substitute original form at end May need to prove base case for value of n 0 other than base case value for recurrence relation Remember to solve induction for exact form of hypothesis, else you didn’t really get a proof

27 27 Cost Tree Analysis Expand recurrence relation, level by level Number of sub-problems and size of each determines number and nature of children of each node Eventually, each node has a cost based on dividing and reassembling partial results Leaves have costs based on base case of the recurrence relation

28 28 Example – Mergesort Cost Rewriting the recurrence as …... and assuming n = 2 k We can create a cost tree by expanding the recurrence at each level T(n/2) T(n)cn T(n/4) cn/2 T(n/2) cn/2 T(n/4)

29 29 Mergesort Cost Total cost at each level of the tree is … 2c(n/2), 4c(n/4), 8c(n/8), … or in general … 2 i c(n/2 i ) = cn At the bottom level there are n subarrays of 1 element each and the cost there is … n*T(1) = cn Depth of tree for sort of n = 2 k elements is … k = lg n, so the tree has … 1 + k = 1 + lg n levels, but only n lg n merges Therefore, the total cost of the algorithm is … cn lg n + cn, which is logarithmic Θ(n lg n)

30 30 Master Method This is the “cookbook” approach to solving recurrences with the form Where a ≥ 1, b > 1, and f(n) is an assymptotically positive a is the number of sub-problems, n/b is the size of each sub-problem, f(n) is the cost of dividing and reassembling There are three distinct classes of solutions

31 31 Master Method Case 1: Case 2: Case 3:

32 32 Master Method (Case 1) Example:

33 33 Master Method (Case 2) Example:

34 34 Master Method (Case 3) Example:

35 35 Master Method Examples Thanks to … wikipedia.org For excellent and clear examples of the Master Method and other cool stuff

36 36 Summary Quicksort algorithm uses a partitioning strategy that finds the final location of a pivot element within an interval [first,last). The pivot splits the interval into two parts, [first, pivotIndex), [pivotIndex, last). All elements in the lower interval have values  pivot and all elements in the upper interval have values  pivot. running time: Θ(n lg n) worst case: of Θ(n 2 ), unlikely to occur!??

37 37 Summary Determining Recursive Algorithm Efficiency First find a recurrence relation for the cost of the recursive algorithm Then use one of the three primary methods to solve the recurrence for its Θ(?) The Substitution Method The Cost Tree Method The Master Method


Download ppt "1 Exceptions Exceptions oops, mistake correction oops, mistake correction Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort."

Similar presentations


Ads by Google