@ zhigang zhu, 2002-2010 1 csc212 data structure - section rs lecture 18a trees, logs and time...
TRANSCRIPT
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 11
CSC212 Data Structure - Section RS
CSC212 Data Structure - Section RS
Lecture 18aLecture 18a
Trees, Logs and Time AnalysisTrees, Logs and Time Analysis
Instructor: Zhigang ZhuInstructor: Zhigang Zhu
Department of Computer Science Department of Computer Science
City College of New YorkCity College of New York
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 22
TopicsTopics
Big-O NotationBig-O Notation Worse Case Times for Tree OperationsWorse Case Times for Tree Operations Time Analysis for BSTsTime Analysis for BSTs Time Analysis for HeapsTime Analysis for Heaps Logarithms and Logarithmic AlgorithmsLogarithms and Logarithmic Algorithms
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 33
Big-O NotationBig-O Notation
The order of an algorithm generally is more The order of an algorithm generally is more important than the speed of the processorimportant than the speed of the processor
Input size: nInput size: n O(log n)O(log n) O (n)O (n) O (nO (n22))
# of stairs: n# of stairs: n [log[log1010n]+1n]+1 3n3n nn22+2n+2n
1010 22 3030 120120
100100 33 300300 10,20010,200
10001000 44 30003000 1,000,20001,000,2000
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 44
Worst-Case Times for Tree OperationsWorst-Case Times for Tree Operations
The worst-case time complexity for the The worst-case time complexity for the following are all O(d), where d = the depth of the following are all O(d), where d = the depth of the tree:tree: Adding an entry in a BST, a heap or a B-tree;Adding an entry in a BST, a heap or a B-tree; Deleting an entry from a BST, a heap or a B-tree;Deleting an entry from a BST, a heap or a B-tree; Searching for a specified entry in a BST or a B-tree.Searching for a specified entry in a BST or a B-tree.
This seems to be the end of our Big-O story...butThis seems to be the end of our Big-O story...but
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 55
What’s d, then?What’s d, then?
Time Analyses for these operations are Time Analyses for these operations are more useful if they are given in term of the more useful if they are given in term of the number of entries (n) instead of the tree’s number of entries (n) instead of the tree’s depth (d)depth (d)
Question: Question: What is the maximum depth for a tree with n What is the maximum depth for a tree with n
entries?entries?
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 66
Time Analysis for BSTsTime Analysis for BSTs
Maximum depth of a BST with n entires: n-1Maximum depth of a BST with n entires: n-1
An Example:An Example:Insert 1, 2, 3,4,5 in Insert 1, 2, 3,4,5 in that order into a bag that order into a bag using a BSTusing a BST
1
2
3
4
5
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 77
Worst-Case Times for BSTsWorst-Case Times for BSTs
Adding, deleting or searching for an entry Adding, deleting or searching for an entry in a BST with n entries is O(d), where d is in a BST with n entries is O(d), where d is the depth of the BSTthe depth of the BST
Since d is no more than n-1, the operations Since d is no more than n-1, the operations in the worst case is (n-1).in the worst case is (n-1).
Conclusion: the worst case time for the add, Conclusion: the worst case time for the add, delete or search operation of a BST is O(n)delete or search operation of a BST is O(n)
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 88
Time Analysis for HeapsTime Analysis for Heaps
A heap is a complete treeA heap is a complete tree The minimum number of nodes needed for The minimum number of nodes needed for
a heap to reach depth d is 2a heap to reach depth d is 2d d :: = (1 + 2 + 4 + ... + 2= (1 + 2 + 4 + ... + 2d-1d-1) + 1) + 1 The extra one at the end is required since there The extra one at the end is required since there
must be at least one entry in level nmust be at least one entry in level n Question: how to add up the formula?Question: how to add up the formula?
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 99
Time Analysis for HeapsTime Analysis for Heaps
A heap is a complete treeA heap is a complete tree The minimum number of nodes needed for The minimum number of nodes needed for
a heap to reach depth d is 2a heap to reach depth d is 2d d :: The number of nodes n >= 2The number of nodes n >= 2dd
Use base 2 logarithms on both sideUse base 2 logarithms on both side loglog22 n >= log n >= log22 2 2d d = d= d
Conclusion: d <= logConclusion: d <= log22 n n
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 1010
Worst-Case Times for Heap OperationsWorst-Case Times for Heap Operations
Adding or deleting an entry in a heap with n Adding or deleting an entry in a heap with n entries is O(d), where d is the depth of the entries is O(d), where d is the depth of the treetree
Because d is no more than logBecause d is no more than log22n, we n, we
conclude that the operations are O(log n)conclude that the operations are O(log n)
Why we can omit the subscript 2 ?Why we can omit the subscript 2 ?
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 1111
Logarithms (log)Logarithms (log)
Base 10: the number of digits in n is [logBase 10: the number of digits in n is [log1010n ]+1n ]+1 101000 = 1, so that log = 1, so that log1010 1 = 0 1 = 0 101011 = 10, so that log = 10, so that log1010 10 = 1 10 = 1 10101.51.5 = 32+, so that log = 32+, so that log1010 32 = 1.5 32 = 1.5 101033 = 1000, so that log = 1000, so that log1010 1000 = 3 1000 = 3
Base 2:Base 2: 2200 = 1, so that log = 1, so that log22 1 = 0 1 = 0 2211 = 2, so that log = 2, so that log22 2 = 1 2 = 1 2233 = 8, so that log = 8, so that log22 8 = 3 8 = 3 2255 = 32, so that log = 32, so that log22 32 = 5 32 = 5 221010 =1024, so that log =1024, so that log22 1024 = 10 1024 = 10
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 1212
Logarithms (log)Logarithms (log)
Base 10: the number of digits in n is [logBase 10: the number of digits in n is [log1010n ]+1n ]+1 10101.51.5 = 32+, so that log = 32+, so that log1010 32 = 1.5 32 = 1.5 101033 = 1000, so that log = 1000, so that log1010 1000 = 3 1000 = 3
Base 2:Base 2: 2233 = 8, so that log = 8, so that log22 8 = 3 8 = 3 2255 = 32, so that log = 32, so that log22 32 = 5 32 = 5
Relation: For any two bases, a and b, and a positive Relation: For any two bases, a and b, and a positive number n, we havenumber n, we have loglogbb n = (log n = (logbb a) log a) logaa n n = log = logbb a a(log(logaa n) n)
loglog22 n n = (log = (log22 10) log 10) log1010 n = (5/1.5) log n = (5/1.5) log1010 n = n = 3.3 log3.3 log1010 n n
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 1313
Logarithmic AlgorithmsLogarithmic Algorithms
Logarithmic algorithms are those with worst-case time Logarithmic algorithms are those with worst-case time O(log n), such as adding to and deleting from a heapO(log n), such as adding to and deleting from a heap
For a logarithm algorithm, doubling the input size (n) For a logarithm algorithm, doubling the input size (n) will make the time increase by a fixed number of new will make the time increase by a fixed number of new operationsoperations
Comparison of linear and logarithmic algorithmsComparison of linear and logarithmic algorithms n= m = 1 hour -> logn= m = 1 hour -> log22m m 6 minutes 6 minutes n=2m = 2 hour -> logn=2m = 2 hour -> log22m + 1 m + 1 7 minutes 7 minutes n=8m = 1 work day -> logn=8m = 1 work day -> log22m + 3 m + 3 9 minutes 9 minutes n=24m = 1 day&night -> logn=24m = 1 day&night -> log22m + 4.5 m + 4.5 10.5 minutes 10.5 minutes
@ Zhigang Zhu, 2002-2010@ Zhigang Zhu, 2002-2010 1414
SummarySummary
Big-O Notation :Big-O Notation : Order of an algorithm versus input size (n)Order of an algorithm versus input size (n)
Worse Case Times for Tree OperationsWorse Case Times for Tree Operations O(d), d = depth of the treeO(d), d = depth of the tree
Time Analysis for BSTsTime Analysis for BSTs worst case: O(n)worst case: O(n)
Time Analysis for HeapsTime Analysis for Heaps worst case O(log n) worst case O(log n)
Logarithms and Logarithmic AlgorithmsLogarithms and Logarithmic Algorithms doubling the input only makes time increase a fixed numberdoubling the input only makes time increase a fixed number