1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE 30331 Lecture 4 – Algorithms II.

Slides:



Advertisements
Similar presentations
Back to Sorting – More efficient sorting algorithms.
Advertisements

CSE Lecture 3 – Algorithms I
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
CS4413 Divide-and-Conquer
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.
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
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
1 Exceptions Exceptions oops, mistake correction oops, mistake correction Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort.
1 Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort Determining Algorithm Efficiency Determining Algorithm Efficiency Substitution.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Analysis of Recursive Algorithms
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
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.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Project 2 due … Project 2 due … Project 2 Project 2.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
Introduction to Analysis of Algorithms CS342 S2004.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
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.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
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.
Spring 2015 Lecture 2: Analysis of Algorithms
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
CSC317 1 Recap: Oh, Omega, Theta Oh (like ≤) Omega (like ≥) Theta (like =) O(n) is asymptotic upper bound 0 ≤ f(n) ≤ cg(n) Ω(n) is asymptotic lower bound.
Lecture 2 Algorithm Analysis
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
UNIT- I Problem solving and Algorithmic Analysis
CS 3343: Analysis of Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer.
CS 3343: Analysis of Algorithms
Algorithm Design Methods
Divide-and-Conquer The most-well known algorithm design strategy:
Sorting Algorithms Written by J.J. Shepherd.
CS 3343: Analysis of Algorithms
O-notation (upper bound)
Chapter 4: Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
CS200: Algorithms Analysis
CSE 2010: Algorithms and Data Structures
Divide and Conquer Algorithms Part I
David Kauchak cs161 Summer 2009
Recursive Algorithms 1 Building a Ruler: drawRuler()
Algorithms and Data Structures Lecture II
Presentation transcript:

1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II

2 Divide & Conquer Basic approach is … break problem in parts separately solve each part rejoin resulting partial solutions into whole

3 Binary Search Algorithm Case 1: A match occurs. The search is complete and mid is the index that locates the target. if (midValue == target) // found match return mid;

4 Binary Search Algorithm Case 2: The value of target is less than midvalue and the search must continue in the lower sublist. if (target < midvalue) // search lower sublist <search sublist arr[first]…arr[mid-1]

5 Binary Search Algorithm Case 3: The value of target is greater than midvalue and the search must continue in the upper sublist. if (target > midvalue)// search upper sublist

6 Successful Binary Search Search for target = 23 Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4. Since target = 23 > midvalue = 12, step 2 searches the upper sublist with first = 5 and last = 9.

7 Successful Binary Search Step 2: Indices first = 5, last = 9, mid = (5+9)/2 = 7. Since target = 23 < midvalue = 33, step 3 searches the lower sublist with first = 5 and last = 7.

8 Successful Binary Search Step 3: Indices first = 5, last = 7, mid = (5+7)/2 = 6. Since target = midvalue = 23, a match is found at index mid = 6.

9 Unsuccessful Binary Search Search for target = 4. Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4. Since target = 4 < midvalue = 12, step 2 searches the lower sublist with first = 0 and last = 4.

10 Unsuccessful Binary Search Step 2: Indices first = 0, last = 4, mid = (0+4)/2 = 2. Since target = 4 < midvalue = 5, step 3 searches the lower sublist with first = 0 and last 2.

11 Unsuccessful Binary Search Step 3: Indices first = 0, last = 2, mid = (0+2)/2 = 1. Since target = 4 > midvalue = 3, step 4 should search the upper sublist with first = 2 and last =2. However, since first >= last, the target is not in the list and we return index last = 9.

12 Binary Search Implementation int binSearch (const int arr[], int first, int last, int target) { int origLast = last; // original value of last while (first < last){ int mid = (first+last)/2; if (target == arr[mid]) return mid; // a match so return mid else if (target < arr[mid]) last = mid; // search lower sublist else first = mid+1; // search upper sublist } return origLast; // target not found }

13 Binary Search Analysis Each comparison divides the problem size in half Assume for simplicity that n = 2 k So, worst case … How many times do we divide n in half before there are no more values to check? T(n) = 1+ k = 1 + lg n Logarithmic Θ(lg n)

14 Merge Sort Recursively divide vector in half … … until sub-vectors reach size 1 Merge results into larger sorted sub-vectors while backing out of recursion

15 Mergesort() // sort v in range [first,last) template void mergeSort(vector & v, int first, int last) { if (first+1 < last) // more than 1 element { int mid = (first + last) / 2; mergesort(v, first, mid); // sort 1st half mergesort(v, mid, last); // sort 2nd half merge(v, first, mid, last); // merge halves }

16 Partitioning and Merging of Sublists in mergeSort()

17 Merge Algorithm Use a temporary vector Scan both sub-vectors left to right If value in vector A is smaller Append value to temp vector and advance in vector A Else value in vector B is smaller Append value to temp vector and advance in vector B Now one of vectors (A or B) is empty, so copy remaining values from non-empty vector to temp Now copy all values back from temp to area originally occupied by vectors A and B

18 Merge() template void merge(vector & v, int first, int mid, int last) { vector temp; // temporary vector needed int iA = first, iB = mid; while (iA < mid && iB < last) // values in both halves if (v[iA] < v[iB]) // smallest in 1 st half temp.push_back(v[iA++]); else // smallest in 2 nd half temp.push_back(v[iB++]); while (iA < mid) // still values in 1 st half temp.push_back(v[iA++]); while (iB < last) // still values in 2 nd half temp.push_back(v[iB++]); iA = first; for (int iV=0; iV<temp.size();iV++) // copy from temp v[iA++] = temp[iV]; }

19 Merge Algorithm Example The merge algorithm takes a sequence of elements in a vector v having index range [first, last). The sequence consists of two ordered sublists separated by an intermediate index, called mid.

20 Merge Algorithm Example …

21 Merge Algorithm Example …

22 Merge Algorithm Example …

23 Function calls in mergeSort()

24 Mergesort Efficiency (Informal) Assume n = 2 k Calls continue until subarrays have 1 element Levels in call tree (1 + k = 1 + lg n) Elements at each level Level 0 (1 vector of n elements) Level 1 (2 vectors of n/2 elements) Level 2 (4 vectors of n/4 elements) …. Level k (2 k vectors of n/2 k elements) Each level has n elements & merges across entire level requires Θ(n) effort Mergesort is always logarithmic Θ (n lg n)

25 Mergesort Efficiency (Formal) For any divide and conquer algorithm … Cost is expressed by the recurrence a is the number of subproblems n/b is the size of each subproblem D(n) is the cost to divide into subproblems C(n) is the cost to combine the results of the subproblems

26 Mergesort Efficiency … Divide: compute middle index – Θ(1) Conquer: solve 2 subproblems of size n/2 Combine: merge results – Θ(n) Putting it together … … and noting that Θ(n) + Θ(1) is still only Θ(n) we get the specific recurrence …

27 Mergesort Efficiency 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)

28 Mergesort Efficiency 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)

29 Asymptotic Growth Notation Big Theta A function f(n) is Θ(g(n)) if c 1 g(n) ≤ f(n) ≤ c 2 g(n), for some c 1, c 2 and all n ≥ n 0 Example: 3n 2 + 2n is Θ(n 2 ), Let c 1 =3 and c 2 =5. 3n 2 ≤ 3n 2 + 2n ≤ 5n 2 for all n ≥ 1 Essentially f(n) is bounded by scalar multiples of g(n) for sufficiently large values of n So, g(n) is an asymptotically tight bound for f(n)

30 Asymptotic Growth Notation Big O A function f(n) is O(g(n)) if f(n) ≤ cg(n), for some c and all n ≥ n 0 Example: 3n 2 + 2n is O(n 2 ), Let c=5. 3n 2 + 2n ≤ 5n 2 for all n ≥ 1 Essentially f(n) is upper bounded by a scalar multiple of g(n) for sufficiently large values of n So, g(n) is an asymptotic upper bound for f(n)

31 Asymptotic Growth Notation Big Omega A function f(n) is Ω(g(n)) if cg(n) ≤ f(n), for some c and all n ≥ n 0 Example: 3n 2 + 2n is Ω(n 2 ), Let c=3. 3n 2 ≤ 3n 2 + 2n for all n ≥ 1 Essentially f(n) is lower bounded by a scalar multiple of g(n) for sufficiently large values of n So, g(n) is an asymptotic lower bound for f(n)