Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(

Slides:



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

Lecture 4 Divide and Conquer for Nearest Neighbor Problem
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.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
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.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Analysis of Recursive Algorithms
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Project 2 due … Project 2 due … Project 2 Project 2.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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],
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
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 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences.
Foundations II: Data Structures and Algorithms
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Searching – Linear and Binary Searches
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Divide and Conquer.
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Divide and Conquer (Merge Sort)
Algorithms and Data Structures Lecture III
Divide and Conquer (Merge Sort)
Divide & Conquer Algorithms
Solving recurrence equations
Recurrences.
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-Conquer Divide: the problem into subproblems
Presentation transcript:

Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert( L, x ) O(logn) + O( n ) O( n ) + O( 1 ) O( 1 ) Delete( L, x ) O(logn) + O( n ) O( n ) + O( 1 ) O( n ) + O( n ) O( n ) + O( 1 ) 1CISC 235 Topic 2

Design and Complexity Analysis of Recursive Algorithms

CISC 235 Topic 23 Outline Design of Recursive Algorithms –Recursive algorithms for Lists Analysis of Recursive Algorithms –Modeling with recurrence relations –Solving recurrence relations

CISC 235 Topic 24 Thinking Recursively 1.What is the measure of the size of input? 2.What is the base case? 3.What is the recursive case? 4.In what ways could the input be reduced in size (and how easy is it to do so)? 5.If we assume that we have the solution to the same problem for a smaller size input, how can we solve the whole problem?

CISC 235 Topic 25 Example: Find Largest in List 1.Measure of input size: length of list 2.Base Case: list of length 1 3.Recursive Case: length > 1 4.Ways to reduce in size a.All except first item in list b.All except last item in list c.Divide list into two halves 5.Assume we have solution to smaller size list(s). a.Take max of first item and max of rest of list b.Take max of last item and max of rest of list c.Take max of the max of the two halves

CISC 235 Topic 26 Example: Find Largest in Array // Assumes list is not empty static int largest ( int[ ] A, int first, int last ) { int n = last - first + 1; if ( n == 1) return ( A[first] ); else return ( Math.max( A[first], largest( A, first + 1, last ) ) ); }

Version of largest method that divides list in two halves static int largest ( int[ ] A, int first, int last ) { int n = last - first + 1; if ( n == 1) return ( A[first] ); else { int mid = ( first + last ) / 2; return ( Math.max(largest( A, first, mid ), largest( A, mid + 1, last ) ) ); } Is there any advantage to dividing the list this way?` CISC 235 Topic 27

8 Design Rules for Recursive Functions 1.Make sure there’s a base case 2.Make progress towards the base case Reduce size of input on each recursive call 3.Assume the recursive calls are correct Write the method so that it’s correct if the recursive calls are correct 4.Compound Interest Rule Don’t duplicate work by solving the same problem instance in different calls

CISC 235 Topic 29 Incorrect Recursive Functions Which design rules do these violate? static int factorial ( int n ) { if ( n == 0 ) return (1); else return ( factorial( n ) * n-1 ); } static int factorial ( int n ) { return ( n * factorial( n-1 ) ); }

CISC 235 Topic 210 Inefficient Recursive Functions Which design rule does this violate? static int fibonacci ( int n ) { if ( n <= 1) return (n); else return ( fibonacci ( n – 1 ) + fibonacci ( n – 2 ) ); }

CISC 235 Topic 211 Divide and Conquer Algorithms Divide the problem into a number of subproblems of size ~n/2 or n/3 or … Conquer the subproblems by solving them recursively. If the subproblem sizes are small enough, however, just solve the subproblems in a straightforward manner. Combine the solutions to the subproblems into the solution for the original problem.

CISC 235 Topic 212 Example: Merge Sort Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted subsequences to produce the sorted answer.

CISC 235 Topic 213 Merge Sort Algorithm // if p  r, subarray A[ p..r ] has at most one element, // so is already in sorted order MERGE-SORT ( A, p, r ) if p < r then q   (p+r) / 2  MERGE-SORT( A, p, q ) MERGE-SORT( A, q+1, r ) MERGE( A, p, q, r )

CISC 235 Topic 214 Merge Algorithm // preconditions: p  q  r // A[p..q] and A[q+1..r] are in sorted order MERGE( A, p, q, r ) n 1  q - p + 1 n 2  r - q create arrays L[1.. n 1 + 1] and R[1.. n 2 +1] for i  1 to n 1 do L[ i ]  A[ p + i - 1 ] for j  1 to n 2 do R[ j ]  A[ q + j ] L[n ]   R[n ]  

CISC 235 Topic 215 Merge Algorithm, con. // Merge arrays L and R and place back in array A i  1 j  1 for k  p to r do if L[ i ]  R[ j ] then A[ k ]  L[ i ] i  i + 1 else A[ k ] = R[ j ] j  j + 1

CISC 235 Topic 216 Recursive Algorithms for Linked Lists 1.Measure of input size: length of list 2.Base Case: list of length 0 or 1 3.Recursive Case: length > 1 4.Ways to reduce in size on each recursive call?

Start of a Linked List Class public class List private class Node { private Node next; private int data; Node( int data ) { this.data = data; this.next = null; } } // end Node class private Node head; public List() { head = null; } … } // end List class CISC 235 Topic 217

Add Methods to Class public void append( int x ) public void insert( int x ) CISC 235 Topic 218

CISC 235 Topic 219 Functions: Complexity Analysis static int bar ( int x, int n ) { for ( int i=1; i<=n; i++ ) x += i; return x; } // end bar static int foo (int x, int n ) { for ( int i=1; i<=n; i++ ) x = x + bar( i, n ); return x; } // end foo static int m( int y ) { int a = 0; System.out.print(foo( a, y )); System.out.print(bar( a, y )); } // end m

What is the measure of the size of input of these methods? // Calculates x i static double pow ( double x, int i ) // Counts the number of occurrences of each // different character in a file (256 possible different chars) static int countChars ( String inFile ) // Determines whether vertex v is adjacent to // vertex w in graph g static boolean isAdjacent( Graph g, Vertex v, Vertex w ) CISC 235 Topic 220

CISC 235 Topic 221 Analysis: Recursive Methods static int factorial ( int n ) { if ( n <= 1 ) return (1); else return ( n * factorial( n – 1 ) ); } Recurrence Relation: T(n) = O(1), if n = 0 or 1 T(n) = T(n – 1) + O(1), if n > 1 Or: T(n) = c, if n = 0 or 1 T(n) = T(n – 1) + c, if n > 1

CISC 235 Topic 222 Recurrence Relations What if there was an O( n ) loop in the base case of the factorial function? What would its recurrence relation be? What if there was an O( n) loop in the recursive case of the factorial function? What would its recurrence relation be?

Recurrence Relations What is the recurrence relation for the first version of the largest method? What is the recurrence relation for the version of largest that divides the list into two halves? What is the recurrence relation for the fibonacci method? CISC 235 Topic 223

CISC 235 Topic 224 Binary Search Function // Search for x in A[low] through A[high] inclusive // Return index of x if found; return -1 if not found int binarySearch( int[ ] A, int x, int low, int high ) {if( low > high ) return -1; int mid = ( low + high ) / 2; if( A[mid] < x ) return binarySearch( A, x, mid+1, high); else if ( x < A[mid] ) return binarySearch( A, x, low, mid-1 ); else return mid; }

CISC 235 Topic 225 Analysis: Binary Search Measure of Size of Input: Recurrence Relation:

CISC 235 Topic 226 Analysis: Merge Sort Measure of Size of Input: Recurrence Relation:

CISC 235 Topic 227 Solving Recurrences Substitution Method: 1.Guess Solution 2.Prove it’s correct with proof by induction How to guess solution? Several ways: –Calculate first few values of recurrence –Substitute recurrence into itself –Construct a Recursion Tree for the recurrence

CISC 235 Topic 228 Calculate First Few Values T(0) = c T(1) = c T(2) = T(1) + c = 2c T(3) = T(2) + c = 3c T(4) = T(3) + c = 4c... Guess solution: T(n) = nc, for all n  1

CISC 235 Topic 229 Substitute recurrence into itself T(n) = T(n-1) + c T(n) = (T(n-2) + c) + c = T(n-2) + 2c T(n) = (T(n-3) + c) + 2c = T(n-3) + 3c T(n) = (T(n-4) + c) + 3c = T(n-4) + 4c... Guess Solution: T(n) = T(n-(n-1)) + (n-1)c = T(1) + (n-1)c = c + (n-1)c = nc

CISC 235 Topic 230 Prove Solution is Correct: T(n) = nc, for all n  1 Base Case: n = 1, formula gives T(1) = c? T(1) = 1c = c Inductive Assumption: T(k) = kc Show Theorem is true for T(k+1), i.e., T(k+1) = (k+1)c: By the recurrence relation, we have: T(k+1) = T(k) + c = kc + c by inductive assump. = (k+1)c