Assignment 5: Solutions

Similar documents
Priority queues implemented via heaps

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Divide-and-Conquer Algorithms Part Two

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Part IA Algorithms Notes

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

Binary Search Trees. Dictionary Operations: Additional operations: find(key) insert(key, value) erase(key)

Fundamental Algorithms

CS Data Structures and Algorithm Analysis

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

Advanced Implementations of Tables: Balanced Search Trees and Hashing

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

The Complexity of Constructing Evolutionary Trees Using Experiments

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

The null-pointers in a binary search tree are replaced by pointers to special null-vertices, that do not carry any object-data

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

Randomized Algorithms III Min Cut

CMPUT 675: Approximation Algorithms Fall 2014

CS 240 Data Structures and Data Management. Module 4: Dictionaries

Dictionary: an abstract data type

Binary Search Trees. Motivation

Advanced Data Structures

CSE 202: Design and Analysis of Algorithms Lecture 3

Dictionary: an abstract data type

Algorithm for exact evaluation of bivariate two-sample Kolmogorov-Smirnov statistics in O(nlogn) time.

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Each internal node v with d(v) children stores d 1 keys. k i 1 < key in i-th sub-tree k i, where we use k 0 = and k d =.

Data Structures and Algorithms " Search Trees!!

Algorithm Design and Analysis

Binary Decision Diagrams. Graphs. Boolean Functions

Data Structures 1 NTIN066

Advanced Data Structures

Insertion Sort. We take the first element: 34 is sorted

IS 709/809: Computational Methods in IS Research Fall Exam Review

Inge Li Gørtz. Thank you to Kevin Wayne for inspiration to slides

Problem. Maintain the above set so as to support certain

Programming Binary Search Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 12

CS361 Homework #3 Solutions

d(ν) = max{n N : ν dmn p n } N. p d(ν) (ν) = ρ.

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

CS 161: Design and Analysis of Algorithms

Heaps and Priority Queues

On improving matchings in trees, via bounded-length augmentations 1

Functional Data Structures

6.854 Advanced Algorithms

7.3 AVL-Trees. Definition 15. Lemma 16. AVL-trees are binary search trees that fulfill the following balance condition.

Partitions and Covers

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

Improved Submatrix Maximum Queries in Monge Matrices

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

k-protected VERTICES IN BINARY SEARCH TREES

Binary Decision Diagrams

INF2220: algorithms and data structures Series 1

7 Dictionary. 7.1 Binary Search Trees. Binary Search Trees: Searching. 7.1 Binary Search Trees

7 Dictionary. EADS c Ernst Mayr, Harald Räcke 109

Data Structures for Disjoint Sets

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

Chapter 11. Min Cut Min Cut Problem Definition Some Definitions. By Sariel Har-Peled, December 10, Version: 1.

NAOMI NISHIMURA Department of Computer Science, University of Waterloo, Waterloo, Ontario, N2L 3G1, Canada.

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Recitation 7. Treaps and Combining BSTs. 7.1 Announcements. FingerLab is due Friday afternoon. It s worth 125 points.

arxiv: v1 [math.co] 22 Jan 2013

An Algebraic View of the Relation between Largest Common Subtrees and Smallest Common Supertrees

Notes on Logarithmic Lower Bounds in the Cell Probe Model

Fundamental Algorithms

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

BFS Dijkstra. Oct abhi shelat

Cographs; chordal graphs and tree decompositions

Dynamic Ordered Sets with Exponential Search Trees

Lecture 6 January 15, 2014

Identifying an m-ary Partition Identity through an m-ary Tree

CS60007 Algorithm Design and Analysis 2018 Assignment 1

CS 161 Summer 2009 Homework #2 Sample Solutions

Data Structure. Mohsen Arab. January 13, Yazd University. Mohsen Arab (Yazd University ) Data Structure January 13, / 86

Optimal Color Range Reporting in One Dimension

Data Structures and Algorithms

Single Source Shortest Paths

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Average Case Analysis of QuickSort and Insertion Tree Height using Incompressibility

Reckhow s Theorem. Yuval Filmus. November 2010

Heapable subsequences and related concepts

Breadth-First Search of Graphs

Data Structures 1 NTIN066

1. Prove: A full m- ary tree with i internal vertices contains n = mi + 1 vertices.

Review Of Topics. Review: Induction

7 Dictionary. Harald Räcke 125

Dominating Set Counting in Graph Classes

Transcription:

Comp 21: Algorithms and Data Structures Assignment : Solutions 1. Heaps. (a) First we remove the minimum key 1 (which we know is located at the root of the heap). We then replace it by the key in the position n of the heap. That is, we move key to the root. This gives the following tree: 1 12 1 The heap property is now violated at the root. So we apply heapifydown from the root to reestablish the heap property. This process, showing how key moves down the tree, is illustrated below. 1 12 1 1 12 1 1

1 12 1 (b) The set W of keys of value at most k can be found in O( W ) time. The reason for this is that the keys with low value are at the top of the heap and the keys with high value are near the bottom of the heap. In particular, the keys along ant path from the root to a leaf are increasing (or nondecreasing). This means we can find W using depth first search (or breadth first search) on the heap. We start at the root. If the key of the root node is at most k then we return the node and recurse on the children of the root. If the key is greater than k, then we do not need to recurse on the children (as their keys and the keys of their descendent must then also be larger than k). Now let s examine the running time. The DFS/BFS algorithm examines each node in W exactly once. In addition, it must examine the children of every node in W (to either find more nodes in W or to determine that the keys are now larger than k and we do not need to search in the corresponding subtree). Since we have a binary heap, each node in W has at most two children so the total number of nodes we examine in O( W ). 2

2. Heaps. (a) A heap with n vertices has depth Θ(log d n), so heapify-up takes time O(log d n). (b) heapify-down takes time O(d log d n) as, at each step, we need to compare the parent key to the keys of its d children. (c) Recall Dijkstra s algorithms may require n applications of extract-min and m applications of key-decrease. The former uses heapify-down and the latter uses heapify-up. Thus the total run time is O(n d log d n + m log d n) (d) The quickest implementation occurs if we equate n d log d n and m log d n. Thus we want n d = m. Since m = n 1+ɛ, we set d = n ɛ. Therefore n = d 1 ɛ. But then log d n = 1 ɛ. The total run time is then O( 1 ɛ m).

. Hash Tables. (a) We compute the hash function h(k) = k + 2 (mod ) for each value key k and insert it to the corresponding slot in the Hash table. h() = ( + 2) mod = 2 h() = ( + 2) mod = 2 h(2) = ( 2 + 2) mod = 1 h() = ( + 2) mod = 1 h() = ( + 2) mod = h(1) = ( 1 + 2) mod = h(20) = ( 20 + 2) mod = 6 Each slot in the Hash table maintains a list (e.g. a linked list) of all the keys that hash to it. This is illustrated below: (b) Hash tables are not very useful if we want to keep keep track of the minimum key. In particular, the running time to find a minimum key is O(m + n) if the hash table has m slots and n keys. This is because we have to examine all m slots (even those that are empty) and we also have to go through each linked-list in every slot and thus examine all n keys.

. Binary Search Trees. (a) The binary search tree that we build is: (b) Suppose P = {k 1, k 2,..., k l } are the keys we find when searching for k. (In these examples the search was successful so k l = k.) Now let X = {x 1, x 2,..., x r } be those keys of P (in order) that are at most k and let Y = {y 1, y 2,..., y s } be those keys of P (in order) that are at least k. In a search on a BST, it must be the case that x 1 x 2 x r. Suppose not and that x i > x i+1. Since x i k, after we examine x i we then search its right subtree R i. But, by definition, every key in R i is larger than x i. In particular, as x i+1 R i we must have x i x i+1, a contradiction. A similar argument shows that y 1 y 2 y r. i. X = {2,,, 6, 1} and Y = {,,,, 0, 1}. This is not a valid BST search sequence as < in Y. ii. X = {1, 6, 6,, 1, 0,, 6,, 1} and Y = {6,, 1,, 6,, 1}. This is a valid BST search sequence. iii. X = {1, 1, 0, 1, 0, 6, 1} and Y = {0,, 2, 1}. This is not a valid BST search sequence as 0 < 1 in X.

. Binary Search Trees. Let P be the path from v 0 up to the root r. Define X = {x 0, x 1, x 2,..., x t } where x 0 = v 0 and, for i 0, x i+1 is the closest ancestor of x i on P whose right child is an ancestor of x i. (Possibly x t is the root, but it need not be.) It takes O(d) time to find all the vertices in X as the length of P is at most d = depth(t ). Let L i be the left subtree at x i, and let L i = λ i. Now every vertex that is not in X or one of its left subtrees has a key greater than v 0 = x 0. So to find the {v 1,..., v l } it suffices to consider just the nodes in X t i=0 L i. Observe that vertices in L i are in the right subtree of x i+1, thus they have larger keys than x i+1. It follows that the keys increase according to the order: {L t, x t, L t 1, x t 1,..., L 1, x 1, L 0, x 0 } So the nodes in L 0 are the immediate predecessors of x 0 = v 0, next comes x 1, then L 1, etc. Assume that {L s, x s, L s 1, x s 1,..., L 1, x 1, L 0 } l and that {L s 1, x s 1,..., L 1, x 1, L 0 } < l. We saw in class the we can sort L i (or any subtree of a BST) in linear time via in-order DFS. Thus we can sort all of L 0, L 1,..., L s 1 in time O( s 1 i=0 λ i) = O(l). The largest l {x s, L s 1, x s 1,..., L 1, x 1, L 0 } keys in L s are then required to complete the list {v l,..., v 2, v 1 }. If λ s = O(l) we can just sort L s. Otherwise, we find the maximum key in L s by following the rightmost path Q = {q 1, q 2,..., q d } we then recursively order their left subtrees ˆL d, ˆL d 1,... until we have found l {x s, L s 1, x s 1,..., L 1, x 1, L 0 } keys in L s. The total run time is O(d + l). 6

6. Data Structures for Disjoint Sets. The depth of the resulting tree T is again O(log T ). vertices the find(v) operation still takes O(log n) time. So for a tree with n We prove by induction that a tree with depth d has at least 2 d vertices. For the base cases, if d = 0 then T has at least 1 = 2 0 vertices as required; similarly if d = 1 then T has at least 2 = 2 1 vertices as required. Now consider a tree T with depth d + 1. Consider the time that T first had depth d+1. Assume this happened when we applied the operation merge(x, y) to merge two trees T x and T y. We may assume that x pointed to y because T x = ω(x) ω(y) = T y. Before the merge it must be the case that depth(t x ) was exactly d. Thus by induction T x 2 d. But by definition T x T y. So T 2 T x 2 d+1 as desired.