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

Slides:



Advertisements
Similar presentations
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
A simple example finding the maximum of a set S of n numbers.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide and Conquer Strategy
1 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CS4413 Divide-and-Conquer
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Theory of Algorithms: Divide and Conquer
21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 1 Class 15 - Recursive sorting methods r Processing arrays by recursion r Divide-and-conquer.
Spring 2015 Lecture 5: QuickSort & Selection
The Substitution method T(n) = 2T(n/2) + cn Guess:T(n) = O(n log n) Proof by Mathematical Induction: Prove that T(n)  d n log n for d>0 T(n)  2(d  n/2.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Sorting Algorithms and Average Case Time Complexity
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.
1 Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort Determining Algorithm Efficiency Determining Algorithm Efficiency Substitution.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Updates HW#1 has been delayed until next MONDAY. There were two errors in the assignment Merge sort runs in Θ(n log n). Insertion sort runs in Θ(n2).
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Project 2 due … Project 2 due … Project 2 Project 2.
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Divide and Conquer Strategy
Divide and Conquer. Recall Divide the problem into a number of sub-problems that are smaller instances of the same problem. Conquer the sub-problems by.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Algorithm Design Methods
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4: Divide and Conquer
Unit-2 Divide and Conquer
Algorithms and Data Structures Lecture III
Divide-and-Conquer The most-well known algorithm design strategy:
CS 3343: Analysis of Algorithms
CS 332: Algorithms Quicksort David Luebke /9/2019.
CSE 373 Data Structures and Algorithms
Recursive Algorithms 1 Building a Ruler: drawRuler()
Design and Analysis of Algorithms
Divide and Conquer Merge sort and quick sort Binary search
Algorithms and Data Structures Lecture III
Presentation transcript:

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 Lecture 7 – Exceptions, Algorithms III

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 Reading Reminder More Algorithms Cormen: 4.1 – 4.3 QuickSort Cormen: Ch 7 (all) Ford & Topp: 15.1

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 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 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 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 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 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 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 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 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 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 Quicksort Example (Cont…)

15 Quicksort Example (Cont…)

16 Quicksort Example (Cont…)

17 Quicksort Example (Cont…)

18 Quicksort Example (Cont…)

19 Quicksort Example (Cont…)

20 Quicksort Example (Cont…)

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 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) + … = 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 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 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 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 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 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 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 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 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 Master Method Case 1: Case 2: Case 3:

32 Master Method (Case 1) Example:

33 Master Method (Case 2) Example:

34 Master Method (Case 3) Example:

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

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 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