runtime with functions csc 172 spring 2002 lecture 9
Post on 15-Jan-2016
214 views
TRANSCRIPT
Runtime with Functions
CSC 172
SPRING 2002
LECTURE 9
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
Non-recursive Functions
As long as there is no recursion we can use a structure tree
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
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
6 1 3 2 5 9
A linked List in Printlist
6 1 3 2 5 9
PrintList l
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:6
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:61
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:613
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:6132
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:61325
A linked List in Printlist
6 1 3 2 5 9
PrintList l
Output:613259
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)
Recurrence Relation for printList
Basis
n == 0
TprintList(0) = O(1)
Induction
TprintList(n) = O(1) + TprintList(n-1)
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) )
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))
. . .
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)
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)
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)
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(1)//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(1)//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(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)
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)
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)
What is the RR for MergeSort?
Recurrence Relation for MergeSort
Basis
n == 0
TmergeSort(0) = O(1)
Induction
TmergeSort(n) = O(n) + 2TmergeSort(n/2)
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?
How many calls?
log2n calls
TmergeSort(n)/n = blog2n + TmergeSort(1)/(1)
TmergeSort(n) = nblog2n + na
TmergeSort(n) = O(nlog2n)
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
Basis
If n=1, Tms(n) = a
If a = Tms(1) <= c(1)log21+d(1),
Then d >= a since log21=0
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
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)
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