Presentation is loading. Please wait.

Presentation is loading. Please wait.

Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.

Similar presentations


Presentation on theme: "Runtime with Functions CSC 172 SPRING 2002 LECTURE 9."— Presentation transcript:

1

2 Runtime with Functions CSC 172 SPRING 2002 LECTURE 9

3 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

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

5 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

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

7 A linked List 6 1 3 2 5 9 

8 A linked List in Printlist 6 1 3 2 5 9  PrintList l

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

10 A linked List in Printlist 6 1 3 2 5 9  PrintList l Output: 6 1

11 A linked List in Printlist 6 1 3 2 5 9  PrintList l Output: 6 1 3

12 A linked List in Printlist 6 1 3 2 5 9  PrintList l Output: 6 1 3 2

13 A linked List in Printlist 6 1 3 2 5 9  PrintList l Output: 6 1 3 2 5

14 A linked List in Printlist 6 1 3 2 5 9  PrintList l Output: 6 1 3 2 5 9

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

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

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

18 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))...

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

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

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

22 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; }

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

24 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

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

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

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

28 What is the RR for MergeSort?

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

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

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

32 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

33 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

34 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

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

36 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


Download ppt "Runtime with Functions CSC 172 SPRING 2002 LECTURE 9."

Similar presentations


Ads by Google