CS 3343: Analysis of Algorithms

Slides:



Advertisements
Similar presentations
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Advertisements

5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
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.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
CS421 - Course Information Website Syllabus Schedule The Book:
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
Project 2 due … Project 2 due … Project 2 Project 2.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences.
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.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Lecture 2 Algorithm Analysis
CMPT 438 Algorithms.
Analysis of Algorithms CS 477/677
Introduction to Algorithms: Divide-n-Conquer Algorithms
Introduction to Algorithms
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
分治法.
Analysis of Algorithms
UNIT- I Problem solving and Algorithmic Analysis
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
Algorithms and Data Structures Lecture III
Data Structures Review Session
Topic: Divide and Conquer
Ch 4: Recurrences Ming-Te Chi
CS 3343: Analysis of Algorithms
CSE 373 Data Structures and Algorithms
CS200: Algorithms Analysis
Divide and Conquer (Merge Sort)
CS 3343: Analysis of Algorithms
Trevor Brown CS 341: Algorithms Trevor Brown
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Divide & Conquer Algorithms
Analysis of Algorithms
David Kauchak cs161 Summer 2009
Algorithms Recurrences.
Recurrences.
The Selection Problem.
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
Quicksort Quick sort Correctness of partition - loop invariant
Divide-and-conquer approach
Divide and Conquer Merge sort and quick sort Binary search
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

CS 3343: Analysis of Algorithms Analyzing recursive algorithms 11/15/2018

Problem of the day How do you find a coffee shop if you don’t know on which direction it might be? 11/15/2018

Analyzing recursive algorithms 11/15/2018

Recursive algorithms General idea: Divide and Conquer Divide a large problem into smaller ones By a constant ratio By a constant or some variable Solve each smaller one recursively or explicitly Combine the solutions of smaller ones to form a solution for the original problem Divide and Conquer 11/15/2018

Merge sort MERGE-SORT A[1 . . n] If n = 1, done. Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . “Merge” the 2 sorted lists. Key subroutine: MERGE 11/15/2018

Merging two sorted arrays Subarray 1 Subarray 2 20 13 7 2 12 11 9 1 11/15/2018

Merging two sorted arrays Subarray 1 Subarray 2 20 13 7 2 12 11 9 1 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 1 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 1 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 1 2 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 1 2 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 1 2 7 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 1 2 7 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 1 2 7 9 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 1 2 7 9 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 1 2 7 9 11 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 20 13 12 1 2 7 9 11 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 11/15/2018

How to show the correctness of a recursive algorithm? By induction: Base case: prove it works for small examples Inductive hypothesis: assume the solution is correct for all sub-problems Step: show that, if the inductive hypothesis is correct, then the algorithm is correct for the original problem. 11/15/2018

Correctness of merge sort MERGE-SORT A[1 . . n] If n = 1, done. Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . “Merge” the 2 sorted lists. Proof: Base case: if n = 1, the algorithm will return the correct answer because A[1..1] is already sorted. Inductive hypothesis: assume that the algorithm correctly sorts smaller suarrays, i.e., A[1.. n/2 ] and A[n/2+1..n]. Step: if A[1.. n/2 ] and A[n/2+1..n] are both correctly sorted, the whole array A[1.. n] is sorted after merging. Therefore, the algorithm is correct. 11/15/2018

How to analyze the time-efficiency of a recursive algorithm? Express the running time on input of size n as a function of the running time on smaller problems 11/15/2018

Analyzing merge sort T(n) MERGE-SORT A[1 . . n] Θ(1) 2T(n/2) f(n) MERGE-SORT A[1 . . n] If n = 1, done. Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . “Merge” the 2 sorted lists Sloppiness: Should be T( n/2 ) + T( n/2 ) , but it turns out not to matter asymptotically. 11/15/2018

Analyzing merge sort T(n) = 2 T(n/2) + f(n) +Θ(1) Divide: Trivial. Conquer: Recursively sort 2 subarrays. Combine: Merge two sorted subarrays T(n) = 2 T(n/2) + f(n) +Θ(1) # subproblems Dividing and Combining subproblem size What is the time for the base case? What is f(n)? What is the growth order of T(n)? Constant 11/15/2018

Merging two sorted arrays 20 13 7 2 12 11 9 1 Θ(n) time to merge a total of n elements (linear time). 11/15/2018

Recurrence for merge sort T(n) = Θ(1) if n = 1; 2T(n/2) + Θ(n) if n > 1. Later we shall often omit stating the base case when T(n) = Q(1) for sufficiently small n, but only when it has no effect on the asymptotic solution to the recurrence. But what does T(n) solve to? I.e., is it O(n) or O(n2) or O(n3) or …? 11/15/2018

Binary Search To find an element in a sorted array, we Check the middle element If ==, we’ve found it else if less than wanted, search right half else search left half Example: Find 9 3 5 7 8 9 12 15 11/15/2018

Binary Search To find an element in a sorted array, we Check the middle element If ==, we’ve found it else if less than wanted, search right half else search left half Example: Find 9 3 5 7 8 9 12 15 11/15/2018

Binary Search To find an element in a sorted array, we Check the middle element If ==, we’ve found it else if less than wanted, search right half else search left half Example: Find 9 3 5 7 8 9 12 15 11/15/2018

Binary Search To find an element in a sorted array, we Check the middle element If ==, we’ve found it else if less than wanted, search right half else search left half Example: Find 9 3 5 7 8 9 12 15 11/15/2018

Binary Search To find an element in a sorted array, we Check the middle element If ==, we’ve found it else if less than wanted, search right half else search left half Example: Find 9 3 5 7 8 9 12 15 11/15/2018

Binary Search To find an element in a sorted array, we Check the middle element If ==, we’ve found it else if less than wanted, search right half else search left half Example: Find 9 3 5 7 8 9 12 15 11/15/2018

What’s the recurrence for the running time of BinarySearch? BinarySearch (A[1..N], value) { if (N == 0) return -1; // not found mid = (1+N)/2; if (A[mid] == value) return mid; // found else if (A[mid] < value) return BinarySearch (A[mid+1, N], value) else return BinarySearch (A[1..mid-1], value); } What’s the recurrence for the running time of BinarySearch? 11/15/2018

Recurrence for binary search 11/15/2018

Recursive Insertion Sort RecursiveInsertionSort(A[1..n]) 1. if (n == 1) do nothing; 2. RecursiveInsertionSort(A[1..n-1]); 3. Find index i in A such that A[i] <= A[n] < A[i+1]; 4. Insert A[n] after A[i]; 11/15/2018

Recurrence for insertion sort 11/15/2018

Compute factorial Factorial (n) if (n == 1) return 1; return n * Factorial (n-1); Note: here we use n as the size of the input. However, usually for such algorithms we would use log(n), i.e., the bits needed to represent n, as the input size. 11/15/2018

Recurrence for computing factorial Note: here we use n as the size of the input. However, usually for such algorithms we would use log(n), i.e., the bits needed to represent n, as the input size. 11/15/2018

What do these mean? Challenge: how to solve the recurrence to get a closed form, e.g. T(n) = Θ (n2) or T(n) = Θ(nlgn), or at least some bound such as T(n) = O(n2)? 11/15/2018

Solving recurrence Running time of many algorithms can be expressed in one of the following two recursive forms or Both can be very hard to solve. We focus on relatively easy ones, which you will encounter frequently in many real algorithms (and exams…) 11/15/2018

Solving recurrence Recursion tree / iteration method Substitution method Master method 11/15/2018