CSC212 Data Structure - Section KL - PowerPoint PPT Presentation

About This Presentation
Title:

CSC212 Data Structure - Section KL

Description:

... for the following are all O(d), where d = the depth of ... This seems to be the end of our Big-O story...but _at_ Zhigang Zhu, 2002-2005. 5. What's d, then? ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 15
Provided by: Zhiga6
Category:
Tags: csc212 | data | of | section | story | structure

less

Transcript and Presenter's Notes

Title: CSC212 Data Structure - Section KL


1
CSC212 Data Structure - Section KL
  • Lecture 18a
  • Trees, Logs and Time Analysis
  • Instructor Zhigang Zhu
  • Department of Computer Science
  • City College of New York

2
Topics
  • Big-O Notation
  • Worse Case Times for Tree Operations
  • Time Analysis for BSTs
  • Time Analysis for Heaps
  • Logarithms and Logarithmic Algorithms

3
Big-O Notation
  • The order of an algorithm generally is more
    important than the speed of the processor

Input size n O(log n) O (n) O (n2)
of stairs n log10n1 3n n22n
10 2 30 120
100 3 300 10,200
1000 4 3000 1,002,000
4
Worst-Case Times for Tree Operations
  • The worst-case time complexity for the following
    are all O(d), where d the depth of the 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
  • Searching for a specified entry in a BST or a
    B-tree.
  • This seems to be the end of our Big-O story...but

5
Whats d, then?
  • Time Analyses for these operations are more
    useful if they are given in term of the number of
    entries (n) instead of the trees depth (d)
  • Question
  • What is the maximum depth for a tree with n
    entries?

6
Time Analysis for BSTs
  • Maximum depth of a BST with n entires n-1
  • An Example
  • Insert 1, 2, 3,4,5 in that order into a bag using
    a BST

7
Worst-Case Times for BSTs
  • Adding, deleting or searching for an entry in a
    BST with n entries is O(d), where d is the depth
    of the BST
  • Since d is no more than n-1, the operations in
    the worst case is (n-1).
  • Conclusion the worst case time for the add,
    delete or search operation of a BST is O(n)

8
Time Analysis for Heaps
  • A heap is a complete tree
  • The minimum number of nodes needed for a heap to
    reach depth d is 2d
  • (1 2 4 ... 2d-1) 1
  • The extra one at the end is required since there
    must be at least one entry in level n
  • Question how to add up the formula?

9
Time Analysis for Heaps
  • A heap is a complete tree
  • The minimum number of nodes needed for a heap to
    reach depth d is 2d
  • The number of nodes n gt 2d
  • Use base 2 logarithms on both side
  • log2 n gt log2 2d d
  • Conclusion d lt log2 n

10
Worst-Case Times for Heap Operations
  • Adding or deleting an entry in a heap with n
    entries is O(d), where d is the depth of the tree
  • Because d is no more than log2n, we conclude that
    the operations are O(log n)
  • Why we can omit the subscript 2 ?

11
Logarithms (log)
  • Base 10 the number of digits in n is log10n 1
  • 100 1, so that log10 1 0
  • 101 10, so that log10 10 1
  • 101.5 32, so that log10 32 1.5
  • 103 1000, so that log10 1000 3
  • Base 2
  • 20 1, so that log2 1 0
  • 21 2, so that log2 2 1
  • 23 8, so that log2 8 3
  • 25 32, so that log2 32 5
  • 210 1024, so that log2 1024 10

12
Logarithms (log)
  • Base 10 the number of digits in n is log10n 1
  • 101.5 32, so that log10 32 1.5
  • 103 1000, so that log10 1000 3
  • Base 2
  • 23 8, so that log2 8 3
  • 25 32, so that log2 32 5
  • Relation For any two bases, a and b, and a
    positive number n, we have
  • logb n (logb a) loga n logb a(loga n)
  • log2 n (log2 10) log10 n (5/1.5) log10 n
    3.3 log10 n

13
Logarithmic Algorithms
  • Logarithmic algorithms are those with worst-case
    time O(log n), such as adding to and deleting
    from a heap
  • For a logarithm algorithm, doubling the input
    size (n) will make the time increase by a fixed
    number of new operations
  • Comparison of linear and logarithmic algorithms
  • n m 1 hour -gt log2m ? 8
    minutes
  • n2m 2 hour -gt log2m 1 ? 9 minutes
  • n8m 1 work day -gt log2m 3 ? 11 minutes
  • n24m 1 daynight -gt log2m 4.5 ? 12.5 minutes

14
Summary
  • Big-O Notation
  • Order of an algorithm versus input size (n)
  • Worse Case Times for Tree Operations
  • O(d), d depth of the tree
  • Time Analysis for BSTs
  • worst case O(n)
  • Time Analysis for Heaps
  • worst case O(log n)
  • Logarithms and Logarithmic Algorithms
  • doubling the input only makes time increase a
    fixed number
Write a Comment
User Comments (0)
About PowerShow.com