Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.

Slides:



Advertisements
Similar presentations
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Mudasser Naseer 1 5/1/2015 CSC 201: Design and Analysis of Algorithms Lecture # 9 Linear-Time Sorting Continued.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Introduction to Algorithms Jiafen Liu Sept
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
The complexity and correctness of algorithms (with binary trees as an example)
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Comp 550.
Introduction to Algorithms
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
Lecture 11: Binary Search Trees Shang-Hua Teng. Data Format KeysEntryKeysSatellite data.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
David Luebke 1 9/8/2015 CS 332: Algorithms Review for Exam 1.
CS 2133: Data Structures Binary Search Trees.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Order Statistics ● The ith order statistic in a set of n elements is the ith smallest element ● The minimum is thus the 1st order statistic ● The maximum.
Binary Search Tree Qamar Abbas.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Many slides here are based on E. Demaine , D. Luebke slides
Binary Search Trees What is a binary search tree?
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Linear-Time Sorting Continued Medians and Order Statistics
CS200: Algorithms Analysis
CS 3013 Binary Search Trees.
Lecture 7 Algorithm Analysis
ძებნის ორობითი ხეები BST (Binary Search Trees)
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Presentation transcript:

Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets

Mudasser Naseer 2 1/25/2016 Review: Radix Sort ● Radix sort: ■ Assumption: input has d digits ranging from 0 to k ■ Basic idea: ○ Sort elements by digit starting with least significant ○ Use a stable sort (like counting sort) for each stage ■ Each pass over n numbers with d digits takes time O(n+k), so total time O(dn+dk) ○ When d is constant and k=O(n), takes O(n) time ■ Fast! Stable! Simple! ■ Doesn’t sort in place

Mudasser Naseer 3 1/25/2016 Review: Bucket Sort ● Bucket sort ■ Assumption: input is n reals from [0, 1) ■ Basic idea: ○ Create n linked lists (buckets) to divide interval [0,1) into subintervals of size 1/n ○ Add each input element to appropriate bucket and sort buckets with insertion sort ■ Uniform input distribution  O(1) bucket size ○ Therefore the expected total time is O(n)

Mudasser Naseer 4 1/25/2016 Review: Order Statistics ● The ith order statistic in a set of n elements is the ith smallest element ● The minimum is thus the 1st order statistic ● The maximum is (thus) the nth order statistic ● The median is the n/2 order statistic ■ If n is even, there are 2 medians ■ The lower median, at i = n/2, and ■ The upper median, at i = n/ ■ We mean lower median when we use the phrase the median

Review: Order Statistics ● Could calculate order statistics by sorting ■ Time: O(n lg n) with comparison sort ■ We can do better Mudasser Naseer 5 1/25/2016

Mudasser Naseer 6 1/25/2016 The Selection Problem ● The selection problem: find the ith smallest element of a set ● Selection problem can be solved in O(n lg n) time ■ Sort the numbers using an O(n lg n)-time algorithm ■ Then return the i th element in the sorted array. ● There are faster algorithms. Two algorithms: ■ A practical randomized algorithm with O(n) expected running time ■ A cool algorithm of theoretical interest only with O(n) worst-case running time

Minimum and maximum MINIMUM(A, n) min ← A[1] for i ← 2 to n do if min > A[i ] then min ← A[i ] return min ● The maximum can be found in exactly the same way by replacing the > with < in the above algorithm. Mudasser Naseer 7 1/25/2016

Simultaneous minimum and maximum ● There will be n − 1 comparisons for the minimum and n − 1 comparisons for the maximum, for a total of 2n −2 comparisons. This will result in  (n) time. ● In fact, at most 3  n/2  comparisons are needed. ■ Maintain the min. and max. of elements seen so far ■ Process elements in pairs. ■ Compare the elements of a pair to each other. ■ Then compare the larger element to the maximum so far, and compare the smaller element to the minimum so far. Mudasser Naseer 8 1/25/2016

Simultaneous minimum and maximum ● This leads to only 3 comparisons for every 2 elements. ● Initial values for the min and max depends on whether n is odd or even ■ If n is even, compare the first two elements and assign the larger to max and the smaller to min. Then process the rest of the elements in pairs. ■ If n is odd, set both min and max to the first element. Then process the rest of the elements in pairs. Mudasser Naseer 9 1/25/2016

Total # of Comparisons ● If n is even, we do 1 initial comparison and then 3(n −2)/2 more comparisons. # of comparisons = 3(n − 2)/2 + 1 = (3n − 6)/2 + 1 = 3n/2 – = 3n/2 − 2. ● If n is odd, we do 3(n − 1)/2 = 3  n/2  comparisons. ● In either case, the maximum number of comparisons is ≤ 3  n/2 . Mudasser Naseer 10 1/25/2016

Mudasser Naseer 11 1/25/2016 Review: Randomized Selection ● Key idea: use partition() from quicksort ■ But, only need to examine one subarray ■ This savings shows up in running time: O(n)  A[q]  A[q] q pr

Mudasser Naseer 12 1/25/2016 Review: Randomized Selection RandomizedSelect(A, p, r, i) if (p == r) then return A[p]; q = RandomizedPartition(A, p, r) k = q - p + 1; if (i == k) then return A[q]; // not in book if (i < k) then return RandomizedSelect(A, p, q-1, i); else return RandomizedSelect(A, q+1, r, i-k);  A[q]  A[q] k q pr

Mudasser Naseer 13 1/25/2016 Review: Randomized Selection ● Average case ■ For upper bound, assume ith element always falls in larger side of partition: ■ T(n) = O(n)

Mudasser Naseer 14 1/25/2016 Dynamic Sets ● In structures for dynamic sets ■ Elements have a key and satellite data ■ Dynamic sets support queries such as: ○ Search(S, k), Minimum(S), Maximum(S), Successor(S, x), Predecessor(S, x) ■ They may also support modifying operations like: ○ Insert(S, x), Delete(S, x)

Mudasser Naseer 15 1/25/2016 Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite data, elements have: ■ key: an identifying field inducing a total ordering ■ left: pointer to a left child (may be NULL) ■ right: pointer to a right child (may be NULL) ■ p: pointer to a parent node (NULL for root)

Mudasser Naseer 16 1/25/2016 Binary Search Trees ● BST property: key[left(x)]  key[x]  key[right(x)] ● Example: F BH KDA

Mudasser Naseer 17 1/25/2016 Inorder Tree Walk ● What does the following code do? TreeWalk(x) TreeWalk(left[x]); print(x); TreeWalk(right[x]); ● A: prints elements in sorted (increasing) order ● This is called an inorder tree walk ■ Preorder tree walk: print root, then left, then right ■ Postorder tree walk: print left, then right, then root

Mudasser Naseer 18 1/25/2016 Inorder Tree Walk ● Example: ● How long will a tree walk take? ● Draw Binary Search Tree for {2,3,5,5,7,8} F BH KDA

Mudasser Naseer 19 1/25/2016 Operations on BSTs: Search ● Given a key and a pointer to a node, returns an element with that key or NULL: TreeSearch(x, k) if (x = NULL or k = key[x]) return x; if (k < key[x]) return TreeSearch(left[x], k); else return TreeSearch(right[x], k);

Mudasser Naseer 20 1/25/2016 BST Search: Example ● Search for D and C: F BH KDA

Mudasser Naseer 21 1/25/2016 Operations on BSTs: Search ● Here’s another function (Iterative) that does the same: TreeSearch(x, k) while (x != NULL and k != key[x]) if (k < key[x]) x = left[x]; else x = right[x]; return x; ● Which of these two functions is more efficient?

Mudasser Naseer 22 1/25/2016 BST Operations: Minimum/Max ● How can we implement a Minimum() query? ● What is the running time? TREE-MINIMUM(x) while left[x] ≠ NIL do x ← left[x] return x TREE-MAXIMUM(x) while right[x] ≠ NIL do x ← right[x] return x

Mudasser Naseer 23 1/25/2016 BST Operations: Successor ● For deletion, we will need a Successor() operation

Mudasser Naseer 24 1/25/2016 ● What is the successor of node 3? Node 15? Node 13? ● Time complexity? ● What are the general rules for finding the successor of node x? (hint: two cases)

Mudasser Naseer 25 1/25/2016 BST Operations: Successor ● Two cases: ■ x has a right subtree: successor is minimum node in right subtree ■ x has no right subtree: successor is first ancestor of x whose left child is also ancestor of x ○ Intuition: As long as you move to the left up the tree, you’re visiting smaller nodes. ● Predecessor: similar algorithm

Mudasser Naseer 26 1/25/2016 Operations of BSTs: Insert ● Adds an element x to the tree so that the binary search tree property continues to hold ● The basic algorithm ■ Like the search procedure above ■ Insert x in place of NULL ■ Use a “trailing pointer” to keep track of where you came from (like inserting into singly linked list)

Insert TREE-INSERT(T, z) 1 y ← NIL; 2 x ← root[T ] 3 while x ≠ NIL 4 do y ← x 5 if key[z] < key[x] 6 then x ← left[x] 7 else x ← right[x] 8 p[z]← y Mudasser Naseer 27 1/25/2016

Insert 9 if y = NIL 10 then root[T ]← z % Tree T was empty 11 else if key[z] < key[y] 12 then left[y]← z 13 else right[y] ← z Mudasser Naseer 28 1/25/2016

Mudasser Naseer 29 1/25/2016 BST Insert: Example ● Example: Insert C F BH KDA C

Mudasser Naseer 30 1/25/2016 BST Search/Insert: Running Time ● What is the running time of TreeSearch() or TreeInsert()? ● A: O(h), where h = height of tree ● What is the height of a binary search tree? ● A: worst case: h = O(n) when tree is just a linear string of left or right children ■ We’ll keep all analysis in terms of h for now ■ Later we’ll see how to maintain h = O(lg n)

Mudasser Naseer 31 1/25/2016 Sorting With Binary Search Trees ● Informal code for sorting array A of length n: BSTSort(A) for i=1 to n TreeInsert(A[i]); InorderTreeWalk(root); ● Argue that this is  (n lg n) ● What will be the running time in the ■ Worst case? ■ Average case? (hint: remind you of anything?)

Mudasser Naseer 32 1/25/2016 Sorting With BSTs ● Average case analysis ■ It’s a form of quicksort! for i=1 to n TreeInsert(A[i]); InorderTreeWalk(root);

Mudasser Naseer 33 1/25/2016 Sorting with BSTs ● Same partitions are done as with quicksort, but in a different order ■ In previous example ○ Everything was compared to 3 once ○ Then those items < 3 were compared to 1 once ○ Etc. ■ Same comparisons as quicksort, different order! ○ Example: consider inserting 5

Mudasser Naseer 34 1/25/2016 Sorting with BSTs ● Since run time is proportional to the number of comparisons, same time as quicksort: O(n lg n) ● Which do you think is better, quicksort or BSTsort? Why?

Mudasser Naseer 35 1/25/2016 Sorting with BSTs ● Since run time is proportional to the number of comparisons, same time as quicksort: O(n lg n) ● Which do you think is better, quicksort or BSTSort? Why? ● A: quicksort ■ Better constants ■ Sorts in place ■ Doesn’t need to build data structure

Mudasser Naseer 36 1/25/2016 More BST Operations ● BSTs are good for more than sorting. For example, can implement a priority queue ● What operations must a priority queue have? ■ Insert ■ Minimum ■ Extract-Min

Mudasser Naseer 37 1/25/2016 BST Operations: Delete ● Deletion is a bit tricky ● 3 cases: ■ x has no children: ○ Remove x ■ x has one child: ○ Splice out x ■ x has two children: ○ Swap x with successor ○ Perform case 1 or 2 to delete it F BH KDA C Example: delete K or H or B

BST Operations: Delete TREE-DELETE(T, z) 1 if left[z] = NIL or right[z] = NIL 2 then y ← z 3 else y ← TREE-SUCCESSOR(z) 4 if left[y] ≠ NIL 5 then x ← left[y] 6 else x ← right[y] 7 if x ≠ NIL 8 then p[x] ← p[y] Mudasser Naseer 38 1/25/2016

Delete 9 if p[y] = NIL 10 then root[T ] ← x 11 else if y = left[p[y]] 12 then left[p[y]] ← x 13 else right[p[y]] ← x 14 if y ≠ z 15 then key[z]← key[y] 16 copy y’s satellite data into z 17 return y Mudasser Naseer 39 1/25/2016

Mudasser Naseer 40 1/25/2016 BST Operations: Delete ● Why will case 2 always go to case 0 or case 1? ● A: because when x has 2 children, its successor is the minimum in its right subtree ● Could we swap x with predecessor instead of successor? ● A: yes. Would it be a good idea? ● A: might be good to alternate

Mudasser Naseer 41 1/25/2016

Mudasser Naseer 42 1/25/2016 The End