University of New Mexico Department of Computer Science. Midterm Examination. CS 561 Data Structures and Algorithms Fall, 2006

Similar documents
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)

Lecture Notes on Binary Search Trees

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

Binary Heaps. CSE 373 Data Structures

Sample Questions Csci 1112 A. Bellaachia

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

Full and Complete Binary Trees

Ordered Lists and Binary Trees

Binary Heap Algorithms

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

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

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Data Structures and Algorithms Written Examination

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

Data Structures Fibonacci Heaps, Amortized Analysis

Persistent Binary Search Trees

PES Institute of Technology-BSC QUESTION BANK

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

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Binary Search Trees CMPSC 122

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Data Structures and Algorithms

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

Section IV.1: Recursive Algorithms and Recursion Trees

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

Analysis of Binary Search algorithm and Selection Sort algorithm

Exam study sheet for CS2711. List of topics

Symbol Tables. Introduction

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

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,

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

Binary Search Trees (BST)

Data Structure [Question Bank]

DATA STRUCTURES USING C

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:

From Last Time: Remove (Delete) Operation

3. Mathematical Induction

Introduction to data structures

Approximation Algorithms

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

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

Lecture Notes on Binary Search Trees

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Lecture Notes on Linear Search

Converting a Number from Decimal to Binary

How To Create A Tree From A Tree In Runtime (For A Tree)

TREE BASIC TERMINOLOGIES

recursion, O(n), linked lists 6/14

Sequential Data Structures

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Algorithm Design and Analysis Homework #1 Due: 5pm, Friday, October 4, 2013 TA === Homework submission instructions ===

Analysis of Algorithms I: Binary Search Trees

Algorithms. Margaret M. Fleck. 18 October 2010

Cpt S 223. School of EECS, WSU

CS211 Spring 2005 Final Exam May 17, Solutions. Instructions

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Sample Induction Proofs

Dynamic Programming. Lecture Overview Introduction

Lecture 1: Course overview, circuits, and formulas

Algorithms and Data Structures

COMPUTER SCIENCE. Paper 1 (THEORY)

MATHEMATICS Y3 Using and applying mathematics 3810 Solve mathematical puzzles and investigate. Equipment MathSphere

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

CSE 326: Data Structures B-Trees and B+ Trees

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

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

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

The Butterfly, Cube-Connected-Cycles and Benes Networks

Randomized algorithms

6 March Array Implementation of Binary Trees

Python for Rookies. Example Examination Paper

CompSci-61B, Data Structures Final Exam

DATABASE DESIGN - 1DL400

Algorithms Chapter 12 Binary Search Trees

Chapter 13: Query Processing. Basic Steps in Query Processing

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

1 Review of Least Squares Solutions to Overdetermined Systems

Fast Sequential Summation Algorithms Using Augmented Data Structures

A Comparison of Dictionary Implementations

The Set Data Model CHAPTER What This Chapter Is About

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Battleships Searching Algorithms

Data Structures. Level 6 C Module Descriptor

Rotation Operation for Binary Search Trees Idea:

6.042/18.062J Mathematics for Computer Science. Expected Value I

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Drawing a histogram using Excel

Row Echelon Form and Reduced Row Echelon Form

Zabin Visram Room CS115 CS126 Searching. Binary Search

First programming project: Key-Word indexer

Persistent Data Structures and Planar Point Location

CS 300 Data Structures Syllabus - Fall 2014

Transcription:

University of New Mexico Department of Computer Science Midterm Examination CS 561 Data Structures and Algorithms Fall, 2006 Name: Email: Print your name and email, neatly in the space provided above; print your name at the upper right corner of every page. Please print legibly. This is an closed book exam. You are permitted to use only two pages of cheat sheets that you have brought to the exam and a calculator. Nothing else is permitted. Do all problems in this booklet. Show your work! You will not get partial credit if we cannot figure out how you arrived at your answer. Write your answers in the space provided for the corresponding problem. Let us know if you need more paper. Don t spend too much time on any single problem. The questions are weighted equally. If you get stuck, move on to something else and come back later. If any question is unclear, ask us for clarification. Question Points Score Grader 1 20 2 20 3 20 4 20 5 20 Total 100

1. Asymptotic Analysis and Recurrence Relations Consider the functions 2 n and 4 n. Is 2 n = Θ(4 n ) or is 2 n = o(4 n )? Prove your answer using definitions of asymptotic notation given in the book and in class and solve for the values required to show the definitions hold. Solution: 2 n = o(4 n ). To show this, we need to show that for any positive constant c > 0, there exists a constant n 0 such that 0 f(n) cg(n) for all n n 0. This is equivalent to saying that 2 n c4 n (1/2) n c 2 n 1/c n log(1/c) Thus we need to choose n 0 = log(1/c) Recurrence Relations: Consider the Recurrence T (n) = T (n 1) + 6T (n 2) + n. Write down the general form of the solution for this recurrence (i.e. don t solve for the constants). Solution: The homogeneous part is annihilated by L 2 L 6 which factors into (L 3)(L+2). The nonhomogeneous part is annihilated by (L 1) 2. Looking this up in the lookup table gives us that the general solution is of the form T (n) = c 1 3 n + c 2 ( 2) n + c 3 n + c 4.

2. Sorting Consider the following sorting program: SillySort(A,i,j){ if i+1 > j then return; Let m be the index of the minimum element in A[i..j]; exchange A[i] and A[m]; SillySort(A,i+1,j); } Assume it takes n operations to find the value m in the third step of the algorithm for a list of size n. Write and solve a recurrence relation for the run time of SillySort when called on the array A[1..n]. Solution: T (n) = T (n 1) + (n + 1). (L 1) annihilates the homogeneous part and (L 1) 2 annihilates the non-homogeneous part. So the complete annihilator is (L 1) 3. The general form of the solution is thus c 1 n 2 + c 2 n + c 3 = O(n 2 ) Prove succinctly that SillySort correctly sorts a list of n elements by induction on n. Don t forget to include the base case, inductive hypothesis and inductive step. Solution: We will do induction on n = j i + 1 to show that SillySort sorts the array A[i..j]. B.C. if n = 1, there is only one element in the list and thus the list is already sorted. Hence SillySort is correct in this case by simply returning without doing anything. I.H. SillySort correctly sorts lists of size less than n. I.S. When faced with a list of size n, SillySort first moves the minimum element to the front of the list. It then calls it self recursively on the remainder of the list. We can assume by the I.H., that the recursive call correctly sorts the remaining elements. Thus the entire array A[i..j] is in sorted order when the algorithm exits.

3. Probability Hat check: Each of n people give their hats to a drunken hat check person at a restaurant. The hat check person gives back the hats in a random order. What is the expected number of customers that get back their own hat? Solution: For each person i, let X i be an indicator random variable that is 1 iff person i gets back their own hat. Let X = n i=1 X i be a random variable that equals the number of hats returned to the right person. Then E(X) = E( n i=1 X i ) = n i=1 E(X i ), where the last step follows by linearity of expectation. REMEMBER: Linearity of Expectation holds even if the random variables aren t independent. Finally note that E(X i ) = 1/n since this is the probability that person i s hat is returned correctly. Thus E(X) = 1. Minimum Value: Assume that a set of n unique values are inserted in random order into a binary tree. What is the expected number of times that minimum value in the tree changes? Hint: Let X i be an indicator random variable that is 1 iff the i-th element inserted is smaller than the first i 1 elements inserted. Solution: Let X be a random variable giving the number of times that the minimum value in the tree changes. Note that X = n i=1 X i. Thus E(X) = E( n i=1 X i ) = n i=1 E(X i ). Finally note that E(X i ) = 1/i since this is the probability that the i-th element is the smallest element among the first i elements (since we re assuming the values are inserted in random order). Thus E(X) = n i=1 1/i. This last summation is ln n + O(1) as we showed in class.

4. Sums Imagine you are given a list of n integers and a single integer x. Your goal is to determine if there is a pair of numbers in the list that sum to the value x. Note that a naive algorithm for this problem is to just consider all pairs of numbers in the list and see if any of them sum to x. This naive algorithm takes O(n 2 ) time. This problem consists of two parts. First, devise an algorithm to solve this problem with good worst case run time. Second devise an algorithm for this problem with good expected run time. Note that the word good is deliberately ambiguous, part of this problem is to try to get as low as possible. Hint: Make use of data structures and algorithms discussed in class. Solution: To get the best worst case bound, first traverse each value in the list and insert it into a balanced BST (such as a red black tree). Next traverse each value y in the list and do a search for the value x y in the BST. If this value is ever found, then return the pair y, x y, otherwise return that there is no pair. This algorithm takes O(n log n) time to insert the values in the list and O(n log n) time to do all the searches. To get the best expected time, perform the same algorithm except insert the values x y into a hash table with n buckets. Assuming a good hash function, the expected time for all the insertions and all the deletions will be O(n).

5. Matrix Maximum In this problem, you are given a n by n matrix, M of integers. You will create a data structure that will provide the following operations: IncrementRow, IncrementColumn and GetMaximum. IncrementRow takes as input a row number and increases every entry in that row by 1. IncrementColumn takes as input a column number and increases every entry in that column by 1. GetMaximum returns the maximum element in the matrix (breaking ties arbitrarily). The data structure is initialized with the start matrix M and you can take any amount of time for processing at initialization. After this, every call to GetMaximum must take O(1) time and every call to IncrementRow and IncrementColumn must take O(n log n) time. Describe how you would create this data structure and sketch your analysis of its efficiency. Hint: Make use of data structures and algorithms discussed in class. Solution: On Initialization, create a heap of the n 2 items in the matrix. Keep a pointer from each item in the matrix to its corresponding node in the heap (these pointers will be used for HEAP-INCREASE-KEY Operations). On a call to GetMaximum, just return the element at the root of the heap (which will be the largest element). On a call to IncrementRow(i), traverse each element in row i and call HEAP-INCREASE-KEY on each of these elements in the heap to increase the key by exactly 1. HEAP-INCREASE-KEY takes O(log n 2 ) = O(log n) time per call and it s called n times to give a total cost of O(n log n). IncrementColumn is similar. Note that the easiest way to keep pointers between the elements of the matrix and the elements of the heap is for the heap to not be implemented as an array but rather with nodes and child and parent pointers. This does not significantly change the heap data structure.