Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Similar documents
Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Lecture 6: Analysis of Algorithms (CS )

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Associate Professor Dr. Raed Ibraheem Hamed

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

SFU CMPT Lecture: Week 9

Search Trees. Undirected graph Directed graph Tree Binary search tree

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Binary Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Lecture: Analysis of Algorithms (CS )

Binary Trees. Recursive definition. Is this a binary tree?

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Programming II (CS300)

CS102 Binary Search Trees

CS301 - Data Structures Glossary By

Data Structure. IBPS SO (IT- Officer) Exam 2017

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Binary Search Trees Treesort

Binary Trees and Binary Search Trees

Trees. (Trees) Data Structures and Programming Spring / 28

Search Trees: BSTs and B-Trees

Binary Trees, Binary Search Trees

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

) $ f ( n) " %( g( n)

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

A set of nodes (or vertices) with a single starting point

Binary Heaps in Dynamic Arrays

Programming II (CS300)

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Binary Search Trees. See Section 11.1 of the text.

CS350: Data Structures Red-Black Trees

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Garbage Collection: recycling unused memory

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Priority Queues, Binary Heaps, and Heapsort

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Augmenting Data Structures

COMP Analysis of Algorithms & Data Structures

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Algorithms. Deleting from Red-Black Trees B-Trees

Lecture Notes for Advanced Algorithms

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

TREES. Trees - Introduction

Algorithms. AVL Tree

COMP Analysis of Algorithms & Data Structures

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

CS 241 Analysis of Algorithms

Binary search trees (BST) Binary search trees (BST)

Lecture 7. Binary Search Trees / AVL Trees

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

Binary Search Trees. Analysis of Algorithms

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

Trees. A tree is a directed graph with the property

Binary Search Trees. What is a Binary Search Tree?

Data Structures and Algorithms for Engineers

Priority Queues and Heaps. Heaps of fun, for everyone!

Heapsort. Heap data structure

Data Structures and Algorithms

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Analysis of Algorithms

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

Programming II (CS300)

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)

Priority Queues and Binary Heaps

INF2220: algorithms and data structures Series 1

CSCI Trees. Mark Redekopp David Kempe

Recall: Properties of B-Trees

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Trees. Tree Structure Binary Tree Tree Traversals

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Chapter 20: Binary Trees

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

COMP : Trees. COMP20012 Trees 219

Friday Four Square! 4:15PM, Outside Gates

Search Trees. The term refers to a family of implementations, that may have different properties. We will discuss:

In a non-ideal situation, we can allow the binary tree to grow to twice the height of the perfect tree (2 lg n) and periodically balance it

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Data Structure and Algorithm Homework #3

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

We assume uniform hashing (UH):

B-Trees and External Memory

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Binary Search Tree (3A) Young Won Lim 6/6/18

CSI33 Data Structures

Transcription:

Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs

ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in C++ (Third Edition), 1998.

ISE 407 Lecture 16 2 Priority Queues A priority queue is a data structure for maintaining a list of items that have associated priorities. The usual operations are construct a queue from a list of items. find the item with the highest priority. insert an item. delete an item. change the priority of an item. Any implementation of a priority queue can be used to sort a list of items. Put the items in a priority queue. Delete the maximum item n times.

ISE 407 Lecture 16 3 Heaps A heap is a balanced binary tree with additional structure that allows it to function efficiently as a priority queue. The additional structure needed to support these operations is that the record stored at each node has a higher priority than either of its children. Any node with this property is said to satisfy the heap property. Consider a tree in which all nodes except for the root have the heap property. We can easily transform this into a tree in which every node has the heap property (how?). This operation is called heapify(). By calling heapify() on each node, starting at the lowest level and working upward, we can transform an unordered binary tree into a heap.

ISE 407 Lecture 16 4 Operations on a Heap The node with the highest priority is always the root. To delete a record Exchange its record with that of a leaf. Delete the leaf. Call heapify(). To add a record Create a new leaf. Exchange the new record with that of the parent node if it has a higher priority. Continue to do this until all nodes have the heap property. Note that we can change the priority of a record in a similar fashion.

ISE 407 Lecture 16 5 Heap Sort Suppose the list of items to be sorted are in an array of size n. The heap sort algorithm is as follows. Put the array in heap order as described above. In the i th iteration, exchange the item in position 0 with the item in position n i and call heapify(). Why is this algorithm correct? How do we analyze the running time?

ISE 407 Lecture 16 6 Binary Search Trees To use the BST data structure, the keys must have an order. As with heaps, a binary search tree is a binary tree with additional structure to support more operations (at a cost). Listing the items in sorted order Finding the k th items in sorted order In a binary tree, the key value of any node is greater than or equal to the key value of all nodes in its left subtree; less than or equal to the key value of all nodes in its right subtree. For now, we will assume that all keys are unique. With this simple structure, we can implement all functions efficiently.

ISE 407 Lecture 16 7 Searching Search in a BST can be implemented recursively in a fashion similar to binary search, starting with the root as the current node. If the pointer to the current node is None, then return None. Otherwise, compare the search key to the current node s key, if it exists. If the keys are equal, then return a pointer to the current node. If the search key is smaller, recursively search in the left subtree. If the search key is larger, recursively search in the right subtree. What is the running time of this operation?

ISE 407 Lecture 16 8 Inserting a Node The procedure for inserting a node is similar to that for searching. As before, we will assume there is no item with an identical key already in the tree. We simply perform an unsuccessful search and insert the node in place of the final None pointer at the end of the search path. This places it where we would expect to find it the next time we look. The running time is the same as searching. Constructing a BST from a given list of elements can be done by iteratively inserting each element.

ISE 407 Lecture 16 9 Finding the Minimum and Maximum Finding the minimum and maximum is a simple procedure. The minimum is the leftmost node in the tree. The maximum is the rightmost node in the tree.

ISE 407 Lecture 16 10 Sorting We can easily read off the items from a BST in sorted order. This involves walking the tree in a specified way. Walking the tree is done recursively by first walking the left subtree and then the right subtree. Recall that this leads to three different orders in which we can display the key values in the tree. To display the values in preorder, print the value of the current node before recursively walking the two subtrees. To display the values in inorder, print the value of the current node after walking the left subtree, but before walking the right subtree. To display the values in postorder, print the value of the current node after walking both subtrees. Which display order will result in the printing of a sorted list?

ISE 407 Lecture 16 11 Finding the Predecessor and Successor To find the successor of a node x, think of an inorder tree walk. After visiting a given node, what is the next value to get printed out? We need to examine two cases. If x has a right child, then the successor is the node with the minimum key in the right subtree (easy to find). Otherwise, the successor is the lowest ancestor of x whose left child is also an ancestor of x (why?). To find such a node, we follow the path to the root until we reach a node that is the left child of its parent. Note that if a node has two children, its successor cannot have a left child (why not?). Finding the predecessor works the same way.

ISE 407 Lecture 16 12 Deleting a Node Deleting a node z from a BST is more complicated than other operations because of the rigid structure that must be maintained. There are a number of algorithms for doing this. The most straightforward implementation considers three cases. If z has no children, then simply set the pointer to z in the parent to be None. If z has one child, then replace z with its child. If z has two children, then delete either the predecessor or the successor and then replace z with it. Why does this work?

ISE 407 Lecture 16 13 Performance of BSTs Efficiency of the basic operations depends on the depth of the tree. Consider the search operation: what is the best case? The best case is to make the same comparisons as in binary search. However, this can only happen if the root of each subtree is the median element of that subtree, i.e., the tree is balanced. Fortunately, if keys are added at random, this should be the case on average. Like quicksort, the average performance is very good, but worst case behavior is easy to find (where?). In fact, quicksort and BSTs exhibit worst case behavior on the same inputs! As with quicksort, one can show that for a random sequence of keys, the average depth of the tree is 2 ln n 1.39 lg n. Again, the average depth is only 40% higher than the best possible. Building a binary search tree has the same running time as quicksort!

ISE 407 Lecture 16 14 Handling Duplicate Keys What happens when the tree may contain duplicate keys? To make things easier, we can always insert items with duplicate keys in the right subtree. To find all items with the same key, search for the first item and then recursively search for the same item in the right subtree. Alternatively, we could maintain a linked list of items with the same key at each node in the tree.

ISE 407 Lecture 16 15 Selection Recall that the selection problem is that of finding the k th element in an ordered list. Selection can be done using an algorithm similar to the quicksort algorithm (notice the connection again). However, we need an additional data member count in the node class that tracks the size of the subtree rooted at each node. With this additional data member, we can recursively search for the k th element. Starting at the root, if the size of the left subtree is k 1, return a pointer to the root. If the size of the left subtree is more than k 1, recursively search for the k th element of the left subtree. Otherwise, recursively search for the (k t 1) th element of the right subtree, where t is the size of the left subtree. Note that maintaining the count data member can be expensive.

ISE 407 Lecture 16 16 Rotation and Balancing To guard against poor performance, we would like to have a scheme for keeping the tree balanced. There are many schemes for automatically maintaining balance. We describe here a method of manually rebalancing the tree. The basic operation that we ll need is that of rotation. Rotating the tree means changing the root from the current root to one of its children, while maintaining the BST structure. To change the right child of the current root into the new root. Make the current root the left child of the new root. Make the left child of the new root the right child of the old root. Note that we can make any node the root of the BST through a sequence of rotations.

ISE 407 Lecture 16 17 Partitioning and Rebalancing To partition the list around the k th item, select the k th item and rotate it to the root. This can be implemented easily in a recursive fashion. The left and right subtrees form the desired partition. To (re)balance a BST Partition around the middle node. Recursively balance the left and right subtrees. This operation can be called periodically What is the running time of this operation?

ISE 407 Lecture 16 18 Another Implementation of Delete Using the partition operation, we can implement delete in a slightly different way. Partition the right subtree of the node to be deleted around its smallest element x. Make the root of the left subtree the left child of x.

ISE 407 Lecture 16 19 Root Insertion and Joining Often it is useful to be able to insert a node as the root of the BST. This can be done easily by inserting it as usual and then rotating it to the root, i.e., partition around it. With root insertion, we can define a recursive method to join two BSTs. Insert the root of the first tree as the root of the second. Recursively join the pairs of left and right subtrees.

ISE 407 Lecture 16 20 Randomized BSTs Recall that we used randomization to guard against the worst case behavior of quicksort. We can do the same here. The procedure for randomly inserting into a BST of size n is as follows. With probability 1/(n + 1), perform root insertion. Otherwise, recursively insert into the right or left subtree, as appropriate, using the same method. One can prove mathematically that this is the same as randomly ordering the elements first and then inserting them as usual. Hence, this should guard against common worst-case inputs.