Linear Time Selection

Size: px
Start display at page:

Download "Linear Time Selection"

Transcription

1 Chapter 2 Linear Time Selection Definition 1 Given a sequence x 1, x 2,..., x n of numbers, the ith order statistic is the ith smallest element. Maximum or Minimum When i = 1, it is the minimum element and when i = n it is the maximum element. We can find either in n 1 comparisons by doing a linear search of the list, from left to right or from right to left. So, n 1 comparisons are sufficient. These many comparisons are also necessary. Think of the process determinimg the maxiumum (minimum), for example, as a tournament in which each comparison is a match to determine the winner (loser). The maximum (minimum) element is the champion-winner (champion-loser). Snce the champion does not lose (win) a match and everyone else must lose (win) at least one match, n 1 comparisons are also necessary. Cormen, Ex 9.1-1, page 185 Solution: To determine the second smallest element or the 2nd order statistic, we first determine the champion-loser. We do this by constructing a (binary) tournament tree of height log n for n players. This is esaily done by cnstructing a left tournament tree on n/2 elements and a right tournament tree on the remaining n/2 elements, continuing the construction recursively for both the subtrees. Since this tree has n leaves, there are n 1 internal nodes and hence as many matches have been played. Now we determine the second largest by determining the champion loser from among at most log n players who have lost to the champion loser. Hence the second smallest can be found in at most n + log n 2 comparisons. An interesting question is whether these many comparisons are necessary. We will discuss this later on in the course. Here s an interesting problem related to the linear search for the minimum (or maximum) element, discussed above. Problem 1 Show that during this linear search the expected number of times the the miniumum (or maximum) value is reset is Θ(log n). 1

2 Solution Let the elements be x 1, x 2, x 3,..., x i, x i+1,..., x n. Let the indicator random variable X i = 1 if the maximum is reset on examining the x i element and 0 otherwise. Thus X = Σ n i=1 X i is the number of times the maximum is reset. Now, by linearity of the expectation operator, E[X] = Σ n i=1 E[X i] is the expected value of the random variable X, where E[X i ] = p i, the probability that on examining x i, the maximum is reset. Suppose we have examined the elements from x 1 to x i 1. The next element x i will reset the maximum if it is the largest of the elements from x 1 to x i. Since all permutations of the first i values are equally likely, any one of the first i values can occur in the ith spot, and, in particular, the maximum, with a probabilty of 1/i. Thus E[X] = Σ n i=11/i. Hence E[X] = Θ(log n). Maximum and Minimum Using the above algorithm we can find both the maximum and the minimum in 2n 2 comparisons. The following cleverer algorithm makes use of at most 3 n/2 comparisons. If n odd, we set the first element to both the maximum and minimum. The remaining elements we compare in pairs, using the smaller (larger) one to reset the minumum (maximum), if necssary. Thus 3 comparisons for (n 1)/2 pairs, for a total of 3 n/2 comparisons. If n is even, we set the smaller of the first pair to be the minimum and the larger to the maximum. We then proceed as in the odd case, for a total of (n/2 1) or 3 n/2 2 comparisons. Hence at most 3 n/2 comparisons are sufficient in all cases. We can improve on the number of comparisons by maintaining two disjoint sets S 1 and S 2 of potential maxima and potential minimum respectively. Initially, S 1 and S 2 are empty. We compare the elements in pairs, adding the smaller to S 1 and the larger to S 2. When we have exhausted all the elements or have just one left, we determine the minimum of S 1 and the maximum of S 2. If there is no leftover element, these are respectively the minimum and maximum of S; else we compare these with the leftover element to determine if these need to be reset. Let n = 2k. Then the number of comparisons made is 3k 2 = 3n/2 2. If n = 2k + 1 then the number of comparisons made is 3k = 3n/2 2. This solves Ex in Cormen partly. The other part is the following problem. Problem 2 Show that 3n/2 2 comparisons are necessary Problem 3 It is possible to solve this problem by a divide-and-conquer approach. We divide the list into two nearly equal halves and apply the algorithm recursively to the two halves. Let max l (max r ) and min l (min r ) be the maximum and minimum elements of the left (right) half. Then max(max l, max r ) is the maximum of the entire list and min(min l, min r ) is the minimum. If T(n) is the number of comparisons made on a list of size n, then T(n) = T( n/2 ) + T( n/2 ) + 2, n 3 Determine a closed form expression for T(n), assuming that T(2) = 1 and T(1) = 0. c Dr. Asish Mukhopadhyay 2

3 Randomized Selection It s similar to randomized quicksort in the sense that we choose a random element with respect to which we partition the input array into a left subarray and a right subarray, locate the subarray that contains the ith order statistic and continue recursively with this subarray. RANDOMIZED SELECT(A, p, r, i) 1. if p = r 2 then return A[p] 3. q RANDOMIZED PARTITION(A, p, r) 4. k q p if i = k 6. then return A[q] 7. elseif i < k 8. then return RANDOMIZED SELECT(A, p, q 1, i) 9. else return RANDOMIZED SELECT(A, q + 1, r, i k) Cormen, Ex This is obvious since k 1 always. When k = 1(= r p + 1), we either return the pivot as answer or continue with the right (left) subarray of non-zero length. Also note that the recursion bottoms out when the array is of size 1. The analysis is interesting. Let T(n) be the time to find the ith order statistic of the input array A[1..n]. The time T(n) is a random variable, since any element A[i] can be chosen as the pivot element. The probability of this is 1/n (which is also the the expected value, E[X k ], of the random variable defined below). We are interested in the expected (or mean) value of T(n). Let X k be a random variable (also called indicator random variable) defined such that: Then, X k = 1 if A[1..q] has k elements X k = 0 otherwise T(n) = Σ n k=1 X k max(t(k 1), T(n k)) + Θ(n), (2.1) where we have assumed that the ith order statistic is in the larger subarray, created by the partition. Applying the expectation operator E[] to both sides of the above equation, we get, from the linearity of this operator, and the independence of the random variables, X k and T(n) that: E[T(n)] = Σ n k=1 E[X k] E[max([T(k 1)], T(n k))] + Θ(n), = Σ n k=1probability (pivot is k th smallest of A[1..n]) E[max(T(k 1), T(n k))] + Θ(n) = Σ n k=11/n E[max(T(k 1), T(n k))] + Θ(n), where we have made the simplifying assumption that the larger subarray contains the ith order statistic. If we assume that T(n) is monotone increasing, then T(k 1) > T(n k) for k 1 > n k or k > (n + 1)/2, while T(k 1) < T(n k) for k 1 < n k and T(k 1) = T(n k) for k = (n + 1)/2 or n/2. Thus the terms in the sum being equal around this middle value, which exists when n is odd, c Dr. Asish Mukhopadhyay 3

4 E[T(n)] = 2/n Σ n 1 k= n/2 E[T(k)] + Θ(n) = 2/n Σ n 1 k=1e[t(k)] 2/n Σ n/2 1 k=1 E[T(k)] + Θ(n) Assume that E[T(n)] cn for a suitable constant c > 0. Then from the above we have that: E[T(n)] 2/n c n(n 1)/2 2/n c ( n/2 1) n/2 /2 + Θ(n) 2/n c n(n 1)/2 2/n c (n/2 1)(n/2 2)/2 + Θ(n) c (n 1) c (n/2 3)/2 + Θ(n) c n c (n/2 1)/2 + Θ(n) c n, provided c (n/2 1)/2 > a n, where we have replaced the Θ(n) term by a n, for some constant a. A small rearrangement reduces the inequality to n (c/4 a) c/2 > 0, so that E[T(n)] c n for n > (c/2)/(c/4 a). By choosing c > 4a, we can set n 0 = 2c/(c 4a). Thus E[T(n)] = O(n) and therefore E[T(n)] = Θ(n) since T(n) has a trivial lower bound of Ω(n). Deterministic Selection A clever deterministic method is used to choose the pivot to lie in the shaded region shown in Fig. 2.1 below. The implication of this is that no matter whether the ith order statistic lies to the left of the pivot or to its right, we are sure to prune the size of the input set by approximately a quarter in each partitioning step. This is a beautiful example of the prune-and-search pardigm. approx n/4 elements < pivot approx n/4 elements > pivot pivot lies somewhere here Figure 2.1: Where the pivot lies This is how it is done. We make groups of 5 out of the n input elements. If n is not a multiple of 5, there is one residual group with up to 4 elements. Choose the median element of each group to give us n/5 elements. We find the median m of these n/5 medians, using this same algorithm; setting m to be the pivot we continue recursively with the subarray that contains the ith order statistic. For the complexity analysis, let us establish a lower bound on the number of elements that are greater than the pivot, m. Barring the group that contains m and the residual group, 1/2 n/5-2 groups have 3 elements greater than m. Thus at least 3n/10 6 elements are greater than the pivot. Similarly, we can establish that at least 3n/10 6 elements are smaller than the pivot. We note that 3n/10 6 n/4 for n 120 and, a fortiori, for n 140. This solves Ex 9.3-2, page 192. If T(n) is the worst-case complexity of this deterministic selection algorithm, then T(n) T(7n/10 + 6) + T( n/5 ) + Θ(n) since we continue recursively with a subarray of size at most 7n/10 + 6, and also call the algorithm recursively to determine the median of a group of n/5 elements. c Dr. Asish Mukhopadhyay 4

5 We use the substitution method again to show that T(n) c n for n n 0, for suitable choices of c and n 0 ; for n < n 0, T(n) = O(1). We have T(n) c (7n/10 + 6) + c ( n/5 ) + a n c (7n/10 + 6) + c (n/5 + 1) + a n c n + c ( 3n/10 + 6) + c (n/5 + 1) + a n c n + (c n/ c + a n) c n, provided c n/ c + a n 0 or c a n/(n/10 7). We can satisfy this last inequality by letting c 20a and 20a 10a n/(n 70). From the latter inequality it follows that n 140. Thus choosing n 0 = 140 and c 20a, we satisfy T(n) c n. Note that for the above analysis to go through we must have 7/10+1/5 = 9/10 < 1. If we choose to make groups of 3, then these fractions are 2/3 and 1/3, adding up to 1, in which case the analysis dose not go through. Groups of 7 also work since in this case the fractions are 5/7 and 1/7. This solves exercise 9.3-1, page 192. Knowing how to find an order staistic in O(n) worst-case time, helps us do Quicksort in O(n log n) worst-case time since this is the solution to the recurrence This solves Cormen, page 192, Ex T(n) = 2 T(n/2) + O(n) The solution to Cormen, page 192, Ex is also easily obtained, using this algorithm. At each step of the deterministic selection algorithm, the status of at least n/4 elements with respect to the ith order statistic are resolved in the sense that these are known to be either greater than or smaller than the ith order statistic. Thus we successively resolve the status of at least 1/4 n, 1/4 3n/4, 1/4 3/4 3n/4,... elements. This gives us an upper bound of 1/4 1/(1 3/4) n = n elements. Thus repeating the step enough times till a subarray of constant size is left, we resolve the status of all elements except for this subarray of constant size. Since we now determine the ith order statistic by brute-force, the status of the remaining elements are simultaneously resolved. Cormen, Ex 9.3-5, page 192 is also easily solved. After each median-finding step, the ith order statistic is located in one of the two half-sized subarrays. Thus the complexity is that of the sum: Cormen, Order Statistics, page 192, c n + c n/2 + c n/ = 2 c n We first find the median of the n numbers. To determine the k numbers that are closest to the median, we find the absolute distances of the remaining numbers from the median, and find the kth smallest of these. These are then used to find the k closest to the median. Cormen, Order Statistics, page 192, c Dr. Asish Mukhopadhyay 5

6 Given two sorted arrays X[] and Y [], each of size n, find their median in O(log n). This problem is interesting. We first compare the x n/2 th element (median) of X[], with the y n/2 th element (median) of Y []. Suppose y n/2 > x n/2. Then between the two arrays there are more than n elements that are less than y n/2, Thus we can prune from consideration the n/2 elements of Y [] that are greater than y n/2. Next, we compare the x 3n/4 th element of X[] with the y n/4 th element of Y []. Let x 3n/4 > y n/4. In this case, we prune the elements of X[] that are greater than x 3n/4 since between the two arrays we have more than n elements that are less than x 3n/4. Next, we compare y 3n/8 with x 5n/8. Thus in O(log n) steps we prune n elements to obtain the median. We terminate when the intervals of indetermination are of size 1, when we determine the median by brute-force. COMBINED-MEDIAN(X[p,q], Y[r,s]) // q-p+1 = s-r+1 1. if (p=q) and (r=s) 2. then return max(x[p], Y[r]) 3. elseif X[(p+q)/2] > Y[(r+s)/2] 4. return COMBINED-MEDIAN(X[p,(p+q)/2], Y[(r+s)/2, s]) //search space reduce by half 5. else 6. return COMBINED-MEDIAN(X[(p+q)/2, q], Y[r,(r+s)/2]) //search space reduce by half Cormen, 192, Suppose there is just one oilfield. The optimal solution is one in which the east-west line goes through the oilfield. When there are two oilfields, the east-west line can have any y-value that lies between the two oilfields. The sum of the distances is always the distance betwen the oilfields. Thus when there are n oilfields, and n is odd the best solution is obtained when the east-west line goes through the oil-well with the median y-value. For n even, the optimal solution is obtained when the eaest west line goes between the wells with the n/2th y-coordinate and the n/2 + 1th y-coordinate values. c Dr. Asish Mukhopadhyay 6

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2) Sorting revisited How did we use a binary search tree to sort an array of elements? Tree Sort Algorithm Given: An array of elements to sort 1. Build a binary search tree out of the elements 2. Traverse

More information

CS473 - Algorithms I

CS473 - Algorithms I CS473 - Algorithms I Lecture 4 The Divide-and-Conquer Design Paradigm View in slide-show mode 1 Reminder: Merge Sort Input array A sort this half sort this half Divide Conquer merge two sorted halves Combine

More information

Analysis of Binary Search algorithm and Selection Sort algorithm

Analysis of Binary Search algorithm and Selection Sort algorithm Analysis of Binary Search algorithm and Selection Sort algorithm In this section we shall take up two representative problems in computer science, work out the algorithms based on the best strategy to

More information

Closest Pair Problem

Closest Pair Problem Closest Pair Problem Given n points in d-dimensions, find two whose mutual distance is smallest. Fundamental problem in many applications as well as a key step in many algorithms. p q A naive algorithm

More information

Near Optimal Solutions

Near Optimal Solutions Near Optimal Solutions Many important optimization problems are lacking efficient solutions. NP-Complete problems unlikely to have polynomial time solutions. Good heuristics important for such problems.

More information

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1. Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Shafi Goldwasser Quiz 1 Quiz 1 Do not open this quiz booklet until you are directed

More information

Converting a Number from Decimal to Binary

Converting a Number from Decimal to Binary Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive

More information

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY 11794 4400

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY 11794 4400 Lecture 18: Applications of Dynamic Programming Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.sunysb.edu/ skiena Problem of the Day

More information

root node level: internal node edge leaf node CS@VT Data Structures & Algorithms 2000-2009 McQuain

root node level: internal node edge leaf node CS@VT Data Structures & Algorithms 2000-2009 McQuain inary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from each

More information

Algorithms. Margaret M. Fleck. 18 October 2010

Algorithms. Margaret M. Fleck. 18 October 2010 Algorithms Margaret M. Fleck 18 October 2010 These notes cover how to analyze the running time of algorithms (sections 3.1, 3.3, 4.4, and 7.1 of Rosen). 1 Introduction The main reason for studying big-o

More information

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay Lecture - 17 Shannon-Fano-Elias Coding and Introduction to Arithmetic Coding

More information

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows: Chapter 12: Binary Search Trees A binary search tree is a binary tree with a special property called the BST-property, which is given as follows: For all nodes x and y, if y belongs to the left subtree

More information

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n)

More information

Approximation Algorithms

Approximation Algorithms Approximation Algorithms or: How I Learned to Stop Worrying and Deal with NP-Completeness Ong Jit Sheng, Jonathan (A0073924B) March, 2012 Overview Key Results (I) General techniques: Greedy algorithms

More information

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * * Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest

More information

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)! The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >

More information

Optimal Binary Search Trees Meet Object Oriented Programming

Optimal Binary Search Trees Meet Object Oriented Programming Optimal Binary Search Trees Meet Object Oriented Programming Stuart Hansen and Lester I. McCann Computer Science Department University of Wisconsin Parkside Kenosha, WI 53141 {hansen,mccann}@cs.uwp.edu

More information

APP INVENTOR. Test Review

APP INVENTOR. Test Review APP INVENTOR Test Review Main Concepts App Inventor Lists Creating Random Numbers Variables Searching and Sorting Data Linear Search Binary Search Selection Sort Quick Sort Abstraction Modulus Division

More information

Mathematical Induction. Lecture 10-11

Mathematical Induction. Lecture 10-11 Mathematical Induction Lecture 10-11 Menu Mathematical Induction Strong Induction Recursive Definitions Structural Induction Climbing an Infinite Ladder Suppose we have an infinite ladder: 1. We can reach

More information

Analysis of Algorithms I: Binary Search Trees

Analysis of Algorithms I: Binary Search Trees Analysis of Algorithms I: Binary Search Trees Xi Chen Columbia University Hash table: A data structure that maintains a subset of keys from a universe set U = {0, 1,..., p 1} and supports all three dictionary

More information

Section IV.1: Recursive Algorithms and Recursion Trees

Section IV.1: Recursive Algorithms and Recursion Trees Section IV.1: Recursive Algorithms and Recursion Trees Definition IV.1.1: A recursive algorithm is an algorithm that solves a problem by (1) reducing it to an instance of the same problem with smaller

More information

Offline sorting buffers on Line

Offline sorting buffers on Line Offline sorting buffers on Line Rohit Khandekar 1 and Vinayaka Pandit 2 1 University of Waterloo, ON, Canada. email: rkhandekar@gmail.com 2 IBM India Research Lab, New Delhi. email: pvinayak@in.ibm.com

More information

Question: What is the probability that a five-card poker hand contains a flush, that is, five cards of the same suit?

Question: What is the probability that a five-card poker hand contains a flush, that is, five cards of the same suit? ECS20 Discrete Mathematics Quarter: Spring 2007 Instructor: John Steinberger Assistant: Sophie Engle (prepared by Sophie Engle) Homework 8 Hints Due Wednesday June 6 th 2007 Section 6.1 #16 What is the

More information

Randomized algorithms

Randomized algorithms Randomized algorithms March 10, 2005 1 What are randomized algorithms? Algorithms which use random numbers to make decisions during the executions of the algorithm. Why would we want to do this?? Deterministic

More information

Algorithms for Computing Convex Hulls Using Linear Programming

Algorithms for Computing Convex Hulls Using Linear Programming Algorithms for Computing Convex Hulls Using Linear Programming Bo Mortensen, 20073241 Master s Thesis, Computer Science February 2015 Advisor: Peyman Afshani Advisor: Gerth Stølting Brodal AARHUS AU UNIVERSITY

More information

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92.

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92. Name: Email ID: CSE 326, Data Structures Section: Sample Final Exam Instructions: The exam is closed book, closed notes. Unless otherwise stated, N denotes the number of elements in the data structure

More information

DESIGN AND ANALYSIS OF ALGORITHMS

DESIGN AND ANALYSIS OF ALGORITHMS CPS 3 DESIGN AND ANALYSIS OF ALGORITHMS Fall 8 Instructor: Herbert Edelsbrunner Teaching Assistant: Zhiqiang Gu CPS 3 Fall Semester of 8 Table of Contents Introduction 3 I DESIGN TECHNIQUES Divide-and-Conquer

More information

Algorithms and Methods for Distributed Storage Networks 9 Analysis of DHT Christian Schindelhauer

Algorithms and Methods for Distributed Storage Networks 9 Analysis of DHT Christian Schindelhauer Algorithms and Methods for 9 Analysis of DHT Institut für Informatik Wintersemester 2007/08 Distributed Hash-Table (DHT) Hash table does not work efficiently for inserting and deleting Distributed Hash-Table

More information

WORKED EXAMPLES 1 TOTAL PROBABILITY AND BAYES THEOREM

WORKED EXAMPLES 1 TOTAL PROBABILITY AND BAYES THEOREM WORKED EXAMPLES 1 TOTAL PROBABILITY AND BAYES THEOREM EXAMPLE 1. A biased coin (with probability of obtaining a Head equal to p > 0) is tossed repeatedly and independently until the first head is observed.

More information

Lecture 1: Course overview, circuits, and formulas

Lecture 1: Course overview, circuits, and formulas Lecture 1: Course overview, circuits, and formulas Topics in Complexity Theory and Pseudorandomness (Spring 2013) Rutgers University Swastik Kopparty Scribes: John Kim, Ben Lund 1 Course Information Swastik

More information

Binary Heap Algorithms

Binary Heap Algorithms CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

More information

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child Binary Search Trees Data in each node Larger than the data in its left child Smaller than the data in its right child FIGURE 11-6 Arbitrary binary tree FIGURE 11-7 Binary search tree Data Structures Using

More information

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb Binary Search Trees Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 27 1 Properties of Binary Search Trees 2 Basic BST operations The worst-case time complexity of BST operations

More information

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

A Note on Maximum Independent Sets in Rectangle Intersection Graphs A Note on Maximum Independent Sets in Rectangle Intersection Graphs Timothy M. Chan School of Computer Science University of Waterloo Waterloo, Ontario N2L 3G1, Canada tmchan@uwaterloo.ca September 12,

More information

Find-The-Number. 1 Find-The-Number With Comps

Find-The-Number. 1 Find-The-Number With Comps Find-The-Number 1 Find-The-Number With Comps Consider the following two-person game, which we call Find-The-Number with Comps. Player A (for answerer) has a number x between 1 and 1000. Player Q (for questioner)

More information

Biostatistics 615/815

Biostatistics 615/815 Merge Sort Biostatistics 615/815 Lecture 8 Notes on Problem Set 2 Union Find algorithms Dynamic Programming Results were very ypositive! You should be gradually becoming g y g comfortable compiling, debugging

More information

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R Binary Search Trees A Generic Tree Nodes in a binary search tree ( B-S-T) are of the form P parent Key A Satellite data L R B C D E F G H I J The B-S-T has a root node which is the only node whose parent

More information

Integration. Topic: Trapezoidal Rule. Major: General Engineering. Author: Autar Kaw, Charlie Barker. http://numericalmethods.eng.usf.

Integration. Topic: Trapezoidal Rule. Major: General Engineering. Author: Autar Kaw, Charlie Barker. http://numericalmethods.eng.usf. Integration Topic: Trapezoidal Rule Major: General Engineering Author: Autar Kaw, Charlie Barker 1 What is Integration Integration: The process of measuring the area under a function plotted on a graph.

More information

Sensitivity Analysis 3.1 AN EXAMPLE FOR ANALYSIS

Sensitivity Analysis 3.1 AN EXAMPLE FOR ANALYSIS Sensitivity Analysis 3 We have already been introduced to sensitivity analysis in Chapter via the geometry of a simple example. We saw that the values of the decision variables and those of the slack and

More information

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and: Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)

More information

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

More information

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS Systems of Equations and Matrices Representation of a linear system The general system of m equations in n unknowns can be written a x + a 2 x 2 + + a n x n b a

More information

Cloud and Big Data Summer School, Stockholm, Aug., 2015 Jeffrey D. Ullman

Cloud and Big Data Summer School, Stockholm, Aug., 2015 Jeffrey D. Ullman Cloud and Big Data Summer School, Stockholm, Aug., 2015 Jeffrey D. Ullman To motivate the Bloom-filter idea, consider a web crawler. It keeps, centrally, a list of all the URL s it has found so far. It

More information

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis CS711008Z Algorithm Design and Analysis Lecture 7 Binary heap, binomial heap, and Fibonacci heap 1 Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China 1 The slides were

More information

From Last Time: Remove (Delete) Operation

From Last Time: Remove (Delete) Operation CSE 32 Lecture : More on Search Trees Today s Topics: Lazy Operations Run Time Analysis of Binary Search Tree Operations Balanced Search Trees AVL Trees and Rotations Covered in Chapter of the text From

More information

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search Chapter Objectives Chapter 9 Search Algorithms Data Structures Using C++ 1 Learn the various search algorithms Explore how to implement the sequential and binary search algorithms Discover how the sequential

More information

CSC2420 Fall 2012: Algorithm Design, Analysis and Theory

CSC2420 Fall 2012: Algorithm Design, Analysis and Theory CSC2420 Fall 2012: Algorithm Design, Analysis and Theory Allan Borodin November 15, 2012; Lecture 10 1 / 27 Randomized online bipartite matching and the adwords problem. We briefly return to online algorithms

More information

Heaps & Priority Queues in the C++ STL 2-3 Trees

Heaps & Priority Queues in the C++ STL 2-3 Trees Heaps & Priority Queues in the C++ STL 2-3 Trees CS 3 Data Structures and Algorithms Lecture Slides Friday, April 7, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

More information

Direct Methods for Solving Linear Systems. Matrix Factorization

Direct Methods for Solving Linear Systems. Matrix Factorization Direct Methods for Solving Linear Systems Matrix Factorization Numerical Analysis (9th Edition) R L Burden & J D Faires Beamer Presentation Slides prepared by John Carroll Dublin City University c 2011

More information

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS 1. SYSTEMS OF EQUATIONS AND MATRICES 1.1. Representation of a linear system. The general system of m equations in n unknowns can be written a 11 x 1 + a 12 x 2 +

More information

Overview of Violations of the Basic Assumptions in the Classical Normal Linear Regression Model

Overview of Violations of the Basic Assumptions in the Classical Normal Linear Regression Model Overview of Violations of the Basic Assumptions in the Classical Normal Linear Regression Model 1 September 004 A. Introduction and assumptions The classical normal linear regression model can be written

More information

CS473 - Algorithms I

CS473 - Algorithms I CS473 - Algorithms I Lecture 9 Sorting in Linear Time View in slide-show mode 1 How Fast Can We Sort? The algorithms we have seen so far: Based on comparison of elements We only care about the relative

More information

Core Maths C1. Revision Notes

Core Maths C1. Revision Notes Core Maths C Revision Notes November 0 Core Maths C Algebra... Indices... Rules of indices... Surds... 4 Simplifying surds... 4 Rationalising the denominator... 4 Quadratic functions... 4 Completing the

More information

1.7 Graphs of Functions

1.7 Graphs of Functions 64 Relations and Functions 1.7 Graphs of Functions In Section 1.4 we defined a function as a special type of relation; one in which each x-coordinate was matched with only one y-coordinate. We spent most

More information

The Taxman Game. Robert K. Moniot September 5, 2003

The Taxman Game. Robert K. Moniot September 5, 2003 The Taxman Game Robert K. Moniot September 5, 2003 1 Introduction Want to know how to beat the taxman? Legally, that is? Read on, and we will explore this cute little mathematical game. The taxman game

More information

Lecture 3: Finding integer solutions to systems of linear equations

Lecture 3: Finding integer solutions to systems of linear equations Lecture 3: Finding integer solutions to systems of linear equations Algorithmic Number Theory (Fall 2014) Rutgers University Swastik Kopparty Scribe: Abhishek Bhrushundi 1 Overview The goal of this lecture

More information

IB Maths SL Sequence and Series Practice Problems Mr. W Name

IB Maths SL Sequence and Series Practice Problems Mr. W Name IB Maths SL Sequence and Series Practice Problems Mr. W Name Remember to show all necessary reasoning! Separate paper is probably best. 3b 3d is optional! 1. In an arithmetic sequence, u 1 = and u 3 =

More information

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012 Binary numbers The reason humans represent numbers using decimal (the ten digits from 0,1,... 9) is that we have ten fingers. There is no other reason than that. There is nothing special otherwise about

More information

1 if 1 x 0 1 if 0 x 1

1 if 1 x 0 1 if 0 x 1 Chapter 3 Continuity In this chapter we begin by defining the fundamental notion of continuity for real valued functions of a single real variable. When trying to decide whether a given function is or

More information

Load Balancing. Load Balancing 1 / 24

Load Balancing. Load Balancing 1 / 24 Load Balancing Backtracking, branch & bound and alpha-beta pruning: how to assign work to idle processes without much communication? Additionally for alpha-beta pruning: implementing the young-brothers-wait

More information

ONLINE DEGREE-BOUNDED STEINER NETWORK DESIGN. Sina Dehghani Saeed Seddighin Ali Shafahi Fall 2015

ONLINE DEGREE-BOUNDED STEINER NETWORK DESIGN. Sina Dehghani Saeed Seddighin Ali Shafahi Fall 2015 ONLINE DEGREE-BOUNDED STEINER NETWORK DESIGN Sina Dehghani Saeed Seddighin Ali Shafahi Fall 2015 ONLINE STEINER FOREST PROBLEM An initially given graph G. s 1 s 2 A sequence of demands (s i, t i ) arriving

More information

Solutions for Practice problems on proofs

Solutions for Practice problems on proofs Solutions for Practice problems on proofs Definition: (even) An integer n Z is even if and only if n = 2m for some number m Z. Definition: (odd) An integer n Z is odd if and only if n = 2m + 1 for some

More information

LINEAR INEQUALITIES. Mathematics is the art of saying many things in many different ways. MAXWELL

LINEAR INEQUALITIES. Mathematics is the art of saying many things in many different ways. MAXWELL Chapter 6 LINEAR INEQUALITIES 6.1 Introduction Mathematics is the art of saying many things in many different ways. MAXWELL In earlier classes, we have studied equations in one variable and two variables

More information

TREE BASIC TERMINOLOGIES

TREE BASIC TERMINOLOGIES TREE Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing hierarchical relationship between the grand father and his children and

More information

Method To Solve Linear, Polynomial, or Absolute Value Inequalities:

Method To Solve Linear, Polynomial, or Absolute Value Inequalities: Solving Inequalities An inequality is the result of replacing the = sign in an equation with ,, or. For example, 3x 2 < 7 is a linear inequality. We call it linear because if the < were replaced with

More information

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally Recurrence Relations Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally modeled by recurrence relations. A recurrence relation is an equation which

More information

The Relative Worst Order Ratio for On-Line Algorithms

The Relative Worst Order Ratio for On-Line Algorithms The Relative Worst Order Ratio for On-Line Algorithms Joan Boyar 1 and Lene M. Favrholdt 2 1 Department of Mathematics and Computer Science, University of Southern Denmark, Odense, Denmark, joan@imada.sdu.dk

More information

Parametric Equations and the Parabola (Extension 1)

Parametric Equations and the Parabola (Extension 1) Parametric Equations and the Parabola (Extension 1) Parametric Equations Parametric equations are a set of equations in terms of a parameter that represent a relation. Each value of the parameter, when

More information

Binary Heaps. CSE 373 Data Structures

Binary Heaps. CSE 373 Data Structures Binary Heaps CSE Data Structures Readings Chapter Section. Binary Heaps BST implementation of a Priority Queue Worst case (degenerate tree) FindMin, DeleteMin and Insert (k) are all O(n) Best case (completely

More information

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later). Binary search tree Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later). Data strutcure fields usually include for a given node x, the following

More information

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A

More information

Dynamic TCP Acknowledgement: Penalizing Long Delays

Dynamic TCP Acknowledgement: Penalizing Long Delays Dynamic TCP Acknowledgement: Penalizing Long Delays Karousatou Christina Network Algorithms June 8, 2010 Karousatou Christina (Network Algorithms) Dynamic TCP Acknowledgement June 8, 2010 1 / 63 Layout

More information

TAKE-AWAY GAMES. ALLEN J. SCHWENK California Institute of Technology, Pasadena, California INTRODUCTION

TAKE-AWAY GAMES. ALLEN J. SCHWENK California Institute of Technology, Pasadena, California INTRODUCTION TAKE-AWAY GAMES ALLEN J. SCHWENK California Institute of Technology, Pasadena, California L INTRODUCTION Several games of Tf take-away?f have become popular. The purpose of this paper is to determine the

More information

28 Closest-Point Problems -------------------------------------------------------------------

28 Closest-Point Problems ------------------------------------------------------------------- 28 Closest-Point Problems ------------------------------------------------------------------- Geometric problems involving points on the plane usually involve implicit or explicit treatment of distances

More information

An example of a computable

An example of a computable An example of a computable absolutely normal number Verónica Becher Santiago Figueira Abstract The first example of an absolutely normal number was given by Sierpinski in 96, twenty years before the concept

More information

the recursion-tree method

the recursion-tree method the recursion- method recurrence into a 1 recurrence into a 2 MCS 360 Lecture 39 Introduction to Data Structures Jan Verschelde, 22 November 2010 recurrence into a The for consists of two steps: 1 Guess

More information

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27 EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27 The Problem Given a set of N objects, do any two intersect? Objects could be lines, rectangles, circles, polygons, or other geometric objects Simple to

More information

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Why? A central concept in Computer Science. Algorithms are ubiquitous. Analysis of Algorithms: A Brief Introduction Why? A central concept in Computer Science. Algorithms are ubiquitous. Using the Internet (sending email, transferring files, use of search engines, online

More information

2010 Solutions. a + b. a + b 1. (a + b)2 + (b a) 2. (b2 + a 2 ) 2 (a 2 b 2 ) 2

2010 Solutions. a + b. a + b 1. (a + b)2 + (b a) 2. (b2 + a 2 ) 2 (a 2 b 2 ) 2 00 Problem If a and b are nonzero real numbers such that a b, compute the value of the expression ( ) ( b a + a a + b b b a + b a ) ( + ) a b b a + b a +. b a a b Answer: 8. Solution: Let s simplify the

More information

1/1 7/4 2/2 12/7 10/30 12/25

1/1 7/4 2/2 12/7 10/30 12/25 Binary Heaps A binary heap is dened to be a binary tree with a key in each node such that: 1. All leaves are on, at most, two adjacent levels. 2. All leaves on the lowest level occur to the left, and all

More information

Dynamic Programming. Lecture 11. 11.1 Overview. 11.2 Introduction

Dynamic Programming. Lecture 11. 11.1 Overview. 11.2 Introduction Lecture 11 Dynamic Programming 11.1 Overview Dynamic Programming is a powerful technique that allows one to solve many different types of problems in time O(n 2 ) or O(n 3 ) for which a naive approach

More information

THE SCHEDULING OF MAINTENANCE SERVICE

THE SCHEDULING OF MAINTENANCE SERVICE THE SCHEDULING OF MAINTENANCE SERVICE Shoshana Anily Celia A. Glass Refael Hassin Abstract We study a discrete problem of scheduling activities of several types under the constraint that at most a single

More information

Homework until Test #2

Homework until Test #2 MATH31: Number Theory Homework until Test # Philipp BRAUN Section 3.1 page 43, 1. It has been conjectured that there are infinitely many primes of the form n. Exhibit five such primes. Solution. Five such

More information

Introduction. Appendix D Mathematical Induction D1

Introduction. Appendix D Mathematical Induction D1 Appendix D Mathematical Induction D D Mathematical Induction Use mathematical induction to prove a formula. Find a sum of powers of integers. Find a formula for a finite sum. Use finite differences to

More information

IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS

IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS Volume 2, No. 3, March 2011 Journal of Global Research in Computer Science RESEARCH PAPER Available Online at www.jgrcs.info IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE

More information

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures CMPSC 465 LECTURES 20-21 Priority Queues and Binary Heaps Adam Smith S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Trees Rooted Tree: collection

More information

AP CALCULUS AB 2009 SCORING GUIDELINES

AP CALCULUS AB 2009 SCORING GUIDELINES AP CALCULUS AB 2009 SCORING GUIDELINES Question 5 x 2 5 8 f ( x ) 1 4 2 6 Let f be a function that is twice differentiable for all real numbers. The table above gives values of f for selected points in

More information

Math 55: Discrete Mathematics

Math 55: Discrete Mathematics Math 55: Discrete Mathematics UC Berkeley, Fall 2011 Homework # 5, due Wednesday, February 22 5.1.4 Let P (n) be the statement that 1 3 + 2 3 + + n 3 = (n(n + 1)/2) 2 for the positive integer n. a) What

More information

Lecture 2 February 12, 2003

Lecture 2 February 12, 2003 6.897: Advanced Data Structures Spring 003 Prof. Erik Demaine Lecture February, 003 Scribe: Jeff Lindy Overview In the last lecture we considered the successor problem for a bounded universe of size u.

More information

Math 120 Final Exam Practice Problems, Form: A

Math 120 Final Exam Practice Problems, Form: A Math 120 Final Exam Practice Problems, Form: A Name: While every attempt was made to be complete in the types of problems given below, we make no guarantees about the completeness of the problems. Specifically,

More information

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each. Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in

More information

Solving Geometric Problems with the Rotating Calipers *

Solving Geometric Problems with the Rotating Calipers * Solving Geometric Problems with the Rotating Calipers * Godfried Toussaint School of Computer Science McGill University Montreal, Quebec, Canada ABSTRACT Shamos [1] recently showed that the diameter of

More information

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite ALGEBRA Pupils should be taught to: Generate and describe sequences As outcomes, Year 7 pupils should, for example: Use, read and write, spelling correctly: sequence, term, nth term, consecutive, rule,

More information

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a CSE 220: Handout 29 Disjoint Sets 1 Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information about relations

More information

CONTINUED FRACTIONS AND PELL S EQUATION. Contents 1. Continued Fractions 1 2. Solution to Pell s Equation 9 References 12

CONTINUED FRACTIONS AND PELL S EQUATION. Contents 1. Continued Fractions 1 2. Solution to Pell s Equation 9 References 12 CONTINUED FRACTIONS AND PELL S EQUATION SEUNG HYUN YANG Abstract. In this REU paper, I will use some important characteristics of continued fractions to give the complete set of solutions to Pell s equation.

More information

Ph.D. Thesis. Judit Nagy-György. Supervisor: Péter Hajnal Associate Professor

Ph.D. Thesis. Judit Nagy-György. Supervisor: Péter Hajnal Associate Professor Online algorithms for combinatorial problems Ph.D. Thesis by Judit Nagy-György Supervisor: Péter Hajnal Associate Professor Doctoral School in Mathematics and Computer Science University of Szeged Bolyai

More information

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case. Algorithms Efficiency of algorithms Computational resources: time and space Best, worst and average case performance How to compare algorithms: machine-independent measure of efficiency Growth rate Complexity

More information

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park Overview Comparison sort Bubble sort Selection sort Tree sort Heap sort Quick sort Merge

More information

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge, The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge, cheapest first, we had to determine whether its two endpoints

More information