Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.

Slides:



Advertisements
Similar presentations
A simple example finding the maximum of a set S of n numbers.
Advertisements

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.
1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the.
Lecture 5COMPSCI.220.FS.T Worst-Case Performance Upper bounds : simple to obtain Lower bounds : a difficult matter... Worst case data may be unlikely.
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.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
MASTERMIND  Did anyone play the game over the weekend?  Any thoughts on strategy?
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Runtime Analysis CSC 172 SPRING 2002 LECTURE 9 RUNNING TIME A program or algorithm has running time T(n), where n is the measure of the size of the input.
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.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Analysis of Recursive Algorithms
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).
Recursion CSC 172 SPRING 2002 LECTURE 8 Visual Proof 123.n n.
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
Analysis of Recursive Algorithms October 29, 2014
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer 7 2  9 4   2   4   7
Analysis of Algorithms
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
Brian Mitchell - Drexel University MCS680-FCS 1 Running Time of Programs int MSTWeight(int graph[][], int size) { int i,j; int.
MCA 202: Discrete Mathematics Instructor Neelima Gupta
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Project 2 due … Project 2 due … Project 2 Project 2.
Solving Recurrence Lecture 19: Nov 25. Some Recursive Programming (Optional?)
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Recursion Algorithm : Design & Analysis [3]. In the last class… Asymptotic growth rate The Sets ,  and  Complexity Class An Example: Maximum Subsequence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
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.
Foundations II: Data Structures and Algorithms
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Solving Recurrences with the Substitution Method.
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.
1Computer Sciences Department. Objectives Recurrences.  Substitution Method,  Recursion-tree method,  Master method.
2/19/2016 Recurrences. 2/19/2016 Evaluating Run-time of Loops MergeSort(Array A) int n=A.length; int i=0; int t=1 for (t=1;t
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Recursion Ali.
Mathematical Foundations (Solving Recurrence)
Analysis of Algorithms CS 477/677
Divide-and-Conquer 6/30/2018 9:16 AM
Runtime of MergeSort CSC 172 SPRING 2009 LECTURE 17 b.
Chapter 4 Divide-and-Conquer
MergeSort CSC 172.
Runtime of MergeSort CSC 172 SPRING 2004 LECTURE 8.
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Divide and Conquer (Merge Sort)
Divide-and-Conquer 7 2  9 4   2   4   7
Divide & Conquer Algorithms
Analysis of Algorithms
Algorithms Recurrences.
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

Runtime with Functions CSC 172 SPRING 2002 LECTURE 9

Programs with Method Calls  Establish a size measure n (for each function)  Let T f (n) be the running time of function f  Include the running time of any function calls when evaluating a simple or a compound statement  i.e. int y = fibonacci(x);  Do not put big-Oh around the functions runtime  We must keep track of how many calls

Non-recursive Functions As long as there is no recursion we can use a structure tree

Recursion  Define running time T f (n) recursively  In terms of itself with argument smaller than n  Solve the recurrence relation  Repeated expansion  Guess and check – based on common forms  T f (n) subsumes O(1)  In general, big-Oh and T(n) cannot be combined  We need to make the combination explicit in the proof

Example: printing a list public void printList(Node l) { if (l == null) return; else { System.out.println(l.getData()); printList(l.getNext()); }

A linked List 

A linked List in Printlist  PrintList l

A linked List in Printlist  PrintList l Output: 6

A linked List in Printlist  PrintList l Output: 6 1

A linked List in Printlist  PrintList l Output: 6 1 3

A linked List in Printlist  PrintList l Output:

A linked List in Printlist  PrintList l Output:

A linked List in Printlist  PrintList l Output:

Example: printing a linked list public void printList(Node l) { if (l == null) return; else { System.out.println(l.getData()); printList(l.getNext()); } //O(1) //T(n-1) // n == l.length // T printList (n) == ? // T printList (n) == O(1) + O(1) + T printList (n-1)

Recurrence Relation for printList Basis n == 0 T printList (0) = O(1) Induction T printList (n) = O(1) + T printList (n-1)

Replace O(1)s by concrete constants T printList (0) = a T printList (n) = b + T printList (n-1) Repeated expansion: T printList (n) = b + (b + T printList (n-2) ) T printList (n) = b + (b + (b + T printList (n-3) ) T printList (n) = b + (b + (b + (b + T printList (n-4) )

Repeated Expansion T printList (n) = b + T printList (n-1) T printList (n) = b + (b + T printList (n-2) ) T printList (n) = b + (b + (b + T printList (n-3) ) T printList (n) = b + (b + (b + (b + T printList (n-4) ) = 2b + T printList (n-2) = 3b + T printList (n-3) = 4b + T printList (n-4) = (n-1)b + (b + T printList (0))...

Repeated Expansion T printList (n) = (n-1)b + (b + T printList (0)) T printList (n) = nb + a Now, we have eliminated the recursive call So, we can replace the unknown constants by Big-Oh T printList (n) = nb + a T printList (n) = nO(b) + O(a) T printList (n) = nO(1) + O(1) T printList (n) = O(n)

What is the RR for split? public static Node split(Node head){ Node secondNode; if (head == null) return null; else if (head.getNext() == null) return null; else { secondNode = head.getNext(); head.setNext(secondNode.getNext()); secondNode.setNext(split(secondNode.getNext()); return secondNode; } //O(1)

What is the RR for split? public static Node split(Node head){ Node secondNode; if (head == null) return null; else if (head.getNext() == null) return null; else { secondNode = head.getNext(); head.setNext(secondNode.getNext()); secondNode.setNext(split(secondNode.getNext()); return secondNode; } //O(1) //O(1) + T(n-1)

What is the RR for Merge? public static Node merge(Node list1, Node list2){ if (list1 == null) return list2; else if (list2 == null) return list1; else if (list1.getData.compareTo(list2.getData()) < 1) { list1.setNext(merge(list1.getNext(),list2); return list1; } else { list2.setNext(merge(list1,list2.getNext()); return list2; }

What is the RR for MergeSort? public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList)); } //O(1) //O(n)

What is the RR for MergeSort? public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList)); } //O(1) //O(n) What is the length of list? secondList? length(list) == length(secondList) = n/2

What is the RR for MergeSort? public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList)); } //O(1) //O(n) What is the length of list? secondList? length(list) == length(secondList) = n/2 So, what is the cost of the call to merge? //O(n)

What is the RR for MergeSort? public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList)); } //O(1) //O(n) If the cost of mergeSort is T mergeSort (n): What is the cost of mergeSort is T mergeSort (list)? T mergeSort (secondlist)? //O(n)

What is the RR for MergeSort? public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList) ); } //O(1) //O(n) T mergesort (n) = O(1)+O(n)+2T mergesort (n/2)

What is the RR for MergeSort?

Recurrence Relation for MergeSort Basis n == 0 T mergeSort (0) = O(1) Induction T mergeSort (n) = O(n) + 2T mergeSort (n/2)

Replace O(1)s by concrete constants T mergeSort (1) = a T mergeSort (n) = bn + 2T mergeSort (n/2) T mergeSort (n)/n = b + T mergeSort (n/2)/(n/2) T mergeSort (n/2)/(n/2) = b + T mergeSort (n/4)/(n/4) T mergeSort (n/4)/(n/4) = b + T mergeSort (n/8)/(n/8) T mergeSort (n/8)/(n/8) = b + T mergeSort (n/16)/(n/16) … T mergeSort (2)/(2) = b + T mergeSort (1)/(1) How many calls?

log 2 n calls T mergeSort (n)/n = blog 2 n + T mergeSort (1)/(1) T mergeSort (n) = nblog 2 n + na T mergeSort (n) = O(nlog 2 n)

Guess and Check Approach Use domain knowledge to “guess” a bound Try to prove the bound inductively T ms (n) <= c nlog 2 n + dn assume n a power of 2 discover c and d

Basis If n=1, T ms (n) = a If a = T ms (1) <= c(1)log 2 1+d(1), Then d >= a since log 2 1=0

Induction Assume: T ms (n/2) <= (cn/2)log 2 (n/2) + dn/2 T ms (n) = bn + 2T ms (n/2) <= bn + cn(log 2 n –1) + dn We want to show T ms (n) <= cnlog 2 n+dn By showing bn + cnlog 2 n – cn + dn <= cnlog 2 n + dn i.e. bn<=cn So, the proof holds if (c >= b) && (d >= a) Let d == a and c == b

An Exponential Recurrence How many strings of length n over symbols a,b,c have no identical consecutive symbols? Basis: T(1) = 3, they are “a”, “b”, and “c” Induction: Each string ends in a symbol, all strings of length n-1 are represented, so each string can give rise to two more of length n by adding one of two more symbols on the end T(n) = 2T(n-1)

Expand T(n) = 2T(n-1) T(n) = 4T(n-2) T(n) = 8T(n-3) T(n) = 16T(n-4) T(n) = 32T(n-5) … T(n) = 2 n-1 T(1) = 3 * 2 n-1