@ Zhigang Zhu, 2002-2014 1 CSC212 Data Structure - Section RS Lecture 18a Trees, Logs and Time Analysis Instructor: Zhigang Zhu Department of Computer.

Slides:



Advertisements
Similar presentations
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 2: Basics Data Structures.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Zhigang Zhu Department.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Advanced Topics in Algorithms and Data Structures 1 Lecture 4 : Accelerated Cascading and Parallel List Ranking We will first discuss a technique called.
Accelerated Cascading Advanced Algorithms & Data Structures Lecture Theme 16 Prof. Dr. Th. Ottmann Summer Semester 2006.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Graphs & Exam Review 3 Chapter 10 – 13 CS211 CS Dept, MHC.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
CSCI 2670 Introduction to Theory of Computing November 10, 2005.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1.2. Comparing Algorithms. Learning outcomes Understand that algorithms can be compared by expressing their complexity as a function relative to the size.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Lecture 6: An Introduction to Trees Neil Ghani University of Strathclyde.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Analysis of Algorithms
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Lecture 8COMPSCI.220.FS.T Algorithm HeapSort J. W. J. Williams (1964): a special binary tree called heap to obtain an O(n log n) worst-case sorting.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
3.3 Complexity of Algorithms
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City.
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
Breadth First Search and Depth First Search. Greatest problem in Computer Science Has lead to a lot of new ideas and data structures Search engines before.
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
B-Trees Katherine Gurdziel 252a-ba. Outline What are b-trees? How does the algorithm work? –Insertion –Deletion Complexity What are b-trees used for?
Heap Sort Uses a heap, which is a tree-based data type Steps involved: Turn the array into a heap. Delete the root from the heap and insert into the array,
Section 1.7 Comparing Algorithms: Big-O Analysis.
1 Algorithms Searching and Sorting Algorithm Efficiency.
CSC212 Data Structure - Section AB
Data Structures and Algorithms for Information Processing
PROJECT -1 (4 points) Week before midterm, C++.
CSC212 Data Structure - Section AB
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Binary Search Tree Chapter 10.
CSC212 Data Structure - Section RS
CSC212 Data Structure - Section RS
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Introduction to Data Structures
COSC 320 Advanced Data Structures and Algorithm Analysis
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Priority Queues (Chapter 6.6):
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
CSC212 Data Structure - Section KL
Priority Queues (Chapter 6):
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
CSE 373 Data Structures Lecture 8
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

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

@ Zhigang Zhu, Topics p Big-O Notation p Worse Case Times for Tree Operations p Time Analysis for BSTs p Time Analysis for Heaps p Logarithms and Logarithmic Algorithms

@ Zhigang Zhu, Big-O Notation p The order of an algorithm generally is more important than the speed of the processor Input size: n O(log n) O (n) O (n 2 ) # of stairs: n [log 10 n]+1 3n n 2 +2n , ,000,2000

@ Zhigang Zhu, Worst-Case Times for Tree Operations p The worst-case time complexity for the following are all O(d), where d = the depth of the tree: p Adding an entry in a BST, a heap or a B-tree; p Deleting an entry from a BST, a heap or a B-tree; p Searching for a specified entry in a BST or a B-tree. p This seems to be the end of our Big-O story...but

@ Zhigang Zhu, What’s d, then? p Time Analyses for these operations are more useful if they are given in term of the number of entries (n) instead of the tree’s depth (d) p Question: p What is the maximum depth for a tree with n entries?

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

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

@ Zhigang Zhu, Time Analysis for Heaps p A heap is a complete tree p The minimum number of nodes needed for a heap to reach depth d is 2 d : p = ( d-1 ) + 1 p The extra one at the end is required since there must be at least one entry in level n p Question: how to add up the formula?

@ Zhigang Zhu, Time Analysis for Heaps p A heap is a complete tree p The minimum number of nodes needed for a heap to reach depth d is 2 d : p The number of nodes n >= 2 d p Use base 2 logarithms on both side p log 2 n >= log 2 2 d = d p Conclusion: d <= log 2 n

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

@ Zhigang Zhu, Logarithms (log) p Base 10: the number of digits in n is [log 10 n ]+1 p 10 0 = 1, so that log 10 1 = 0 p 10 1 = 10, so that log = 1 p = 32+, so that log = 1.5 p 10 3 = 1000, so that log = 3 p Base 2: p 2 0 = 1, so that log 2 1 = 0 p 2 1 = 2, so that log 2 2 = 1 p 2 3 = 8, so that log 2 8 = 3 p 2 5 = 32, so that log 2 32 = 5 p 2 10 =1024, so that log = 10

@ Zhigang Zhu, Logarithms (log) p Base 10: the number of digits in n is [log 10 n ]+1 p = 32+, so that log = 1.5 p 10 3 = 1000, so that log = 3 p Base 2: p 2 3 = 8, so that log 2 8 = 3 p 2 5 = 32, so that log 2 32 = 5 p Relation: For any two bases, a and b, and a positive number n, we have p log b n = (log b a) log a n = log b a (log a n) p log 2 n = (log 2 10) log 10 n = (5/1.5) log 10 n = 3.3 log 10 n

@ Zhigang Zhu, Logarithmic Algorithms p Logarithmic algorithms are those with worst-case time O(log n), such as adding to and deleting from a heap p For a logarithm algorithm, doubling the input size (n) will make the time increase by a fixed number of new operations p Comparison of linear and logarithmic algorithms p n= m = 1 hour -> log 2 m  6 minutes p n=2m = 2 hour -> log 2 m + 1  7 minutes p n=8m = 1 work day -> log 2 m + 3  9 minutes p n=24m = 1 day&night -> log 2 m  10.5 minutes

@ Zhigang Zhu, Summary p Big-O Notation : p Order of an algorithm versus input size (n) p Worse Case Times for Tree Operations p O(d), d = depth of the tree p Time Analysis for BSTs p worst case: O(n) p Time Analysis for Heaps p worst case O(log n) p Logarithms and Logarithmic Algorithms p doubling the input only makes time increase a fixed number