Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.
-
date post
15-Jan-2016 -
Category
Documents
-
view
214 -
download
0
Transcript of Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.
![Page 1: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/1.jpg)
Runtime with Functions
CSC 172
SPRING 2002
LECTURE 9
![Page 2: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/2.jpg)
Programs with Method Calls
Establish a size measure n (for each function) Let Tf(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 runtimeWe must keep track of how many calls
![Page 3: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/3.jpg)
Non-recursive Functions
As long as there is no recursion we can use a structure tree
![Page 4: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/4.jpg)
Recursion
Define running time Tf(n) recursively In terms of itself with argument smaller than n
Solve the recurrence relationRepeated expansionGuess and check – based on common forms
Tf(n) subsumes O(1)
In general, big-Oh and T(n) cannot be combinedWe need to make the combination explicit in the proof
![Page 5: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/5.jpg)
Example: printing a list
public void printList(Node l) {
if (l == null) return;
else {
System.out.println(l.getData());
printList(l.getNext());
}
}
![Page 6: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/6.jpg)
A linked List
6 1 3 2 5 9
![Page 7: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/7.jpg)
A linked List in Printlist
6 1 3 2 5 9
PrintList l
![Page 8: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/8.jpg)
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:6
![Page 9: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/9.jpg)
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:61
![Page 10: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/10.jpg)
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:613
![Page 11: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/11.jpg)
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:6132
![Page 12: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/12.jpg)
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:61325
![Page 13: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/13.jpg)
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:613259
![Page 14: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/14.jpg)
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)
//O(1)
//T(n-1)
// n == l.length// TprintList(n) == ?
// TprintList(n) == O(1) + O(1) + TprintList(n-1)
![Page 15: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/15.jpg)
Recurrence Relation for printList
Basis
n == 0
TprintList(0) = O(1)
Induction
TprintList(n) = O(1) + TprintList(n-1)
![Page 16: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/16.jpg)
Replace O(1)s by concrete constants
TprintList(0) = a
TprintList(n) = b + TprintList(n-1)
Repeated expansion:
TprintList(n) = b + (b + TprintList(n-2) )
TprintList(n) = b + (b + (b + TprintList(n-3) )
TprintList(n) = b + (b + (b + (b + TprintList(n-4) )
![Page 17: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/17.jpg)
Repeated ExpansionTprintList(n) = b + TprintList(n-1)
TprintList(n) = b + (b + TprintList(n-2) )
TprintList(n) = b + (b + (b + TprintList(n-3) )
TprintList(n) = b + (b + (b + (b + TprintList(n-4) )
= 2b + TprintList(n-2)
= 3b + TprintList(n-3)
= 4b + TprintList(n-4)
= (n-1)b + (b + TprintList(0))
. . .
![Page 18: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/18.jpg)
Repeated Expansion
TprintList(n) = (n-1)b + (b + TprintList(0))
TprintList(n) = nb + a
Now, we have eliminated the recursive callSo, we can replace the unknown constants by Big-Oh
TprintList(n) = nb + a
TprintList(n) = nO(b) + O(a)
TprintList(n) = nO(1) + O(1)
TprintList(n) = O(n)
![Page 19: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/19.jpg)
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)//O(1)
//O(1)//O(1)
//O(1)
![Page 20: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/20.jpg)
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)//O(1)
//O(1)//O(1)
//O(1)
//O(1) + T(n-1)
![Page 21: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/21.jpg)
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;
}}
![Page 22: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/22.jpg)
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(1)//O(1)
//O(n)
![Page 23: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/23.jpg)
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(1)//O(1)
//O(n)
What is the length of list? secondList?
length(list) == length(secondList) = n/2
![Page 24: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/24.jpg)
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(1)//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)
![Page 25: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/25.jpg)
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(1)//O(1)
//O(n)
If the cost of mergeSort is TmergeSort(n):
What is the cost of mergeSort is TmergeSort(list)?
TmergeSort(secondlist)?//O(n)
![Page 26: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/26.jpg)
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(1)//O(1)
//O(n)
//O(n)
Tmergesort(n) = O(1)+O(n)+2Tmergesort(n/2)
![Page 27: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/27.jpg)
What is the RR for MergeSort?
![Page 28: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/28.jpg)
Recurrence Relation for MergeSort
Basis
n == 0
TmergeSort(0) = O(1)
Induction
TmergeSort(n) = O(n) + 2TmergeSort(n/2)
![Page 29: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/29.jpg)
Replace O(1)s by concrete constantsTmergeSort(1) = a
TmergeSort(n) = bn + 2TmergeSort(n/2)
TmergeSort(n)/n = b + TmergeSort(n/2)/(n/2)
TmergeSort(n/2)/(n/2) = b + TmergeSort(n/4)/(n/4)
TmergeSort(n/4)/(n/4) = b + TmergeSort(n/8)/(n/8)
TmergeSort(n/8)/(n/8) = b + TmergeSort(n/16)/(n/16)…
TmergeSort(2)/(2) = b + TmergeSort(1)/(1) How many calls?
![Page 30: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/30.jpg)
How many calls?
log2n calls
TmergeSort(n)/n = blog2n + TmergeSort(1)/(1)
TmergeSort(n) = nblog2n + na
TmergeSort(n) = O(nlog2n)
![Page 31: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/31.jpg)
Guess and Check Approach
Use domain knowledge to “guess” a bound
Try to prove the bound inductively
Tms(n) <= c nlog2n + dn
assume n a power of 2
discover c and d
![Page 32: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/32.jpg)
Basis
If n=1, Tms(n) = a
If a = Tms(1) <= c(1)log21+d(1),
Then d >= a since log21=0
![Page 33: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/33.jpg)
InductionAssume: Tms(n/2) <= (cn/2)log2(n/2) + dn/2
Tms(n) = bn + 2Tms(n/2) <= bn + cn(log2n –1) + dn
We want to show
Tms(n) <= cnlog2n+dn
By showing
bn + cnlog2n – cn + dn <= cnlog2n + dn
i.e. bn<=cn
So, the proof holds if (c >= b) && (d >= a)
Let d == a and c == b
![Page 34: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/34.jpg)
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 endT(n) = 2T(n-1)
![Page 35: Runtime with Functions CSC 172 SPRING 2002 LECTURE 9.](https://reader035.fdocuments.us/reader035/viewer/2022062804/56649d5e5503460f94a3ddb5/html5/thumbnails/35.jpg)
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) = 2n-1 T(1) = 3 * 2n-1