(D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.

Similar documents
Part 1: Multiple Choice Circle only ONE answer. Each multiple choice question is worth 3.5 marks.

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Recitation 9. Prelim Review

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

CSL 201 Data Structures Mid-Semester Exam minutes

Quiz 1 Practice Problems

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE 373 Spring Midterm. Friday April 21st

Second Examination Solution

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

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS301 - Data Structures Glossary By

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Section 05: Midterm Review

ECE250: Algorithms and Data Structures Midterm Review

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Midterm solutions. n f 3 (n) = 3

Quiz 1 Practice Problems

THE UNIVERSITY OF WESTERN AUSTRALIA

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

CSE wi: Practice Midterm

CPSC 331 Term Test #2 March 26, 2007

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Binary Trees, Binary Search Trees

Friday Four Square! 4:15PM, Outside Gates

Multi-Way Search Tree

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CS302 - Data Structures using C++

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS 315 Data Structures mid-term 2

Section 05: Solutions

CSCE 2014 Final Exam Spring Version A

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

INSTITUTE OF AERONAUTICAL ENGINEERING

Multiple-choice (35 pt.)

Section 05: Solutions

CSE373 Fall 2013, Midterm Examination October 18, 2013

COMP 250 Midterm #2 March 11 th 2013

Module 2: Classical Algorithm Design Techniques

Data Structures and Algorithms

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

Data Structures and Algorithms for Engineers

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

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

TREES. Trees - Introduction

Section 1: True / False (1 point each, 15 pts total)

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5.

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Final Exam in Algorithms and Data Structures 1 (1DL210)

Binary Search Trees Treesort

CSE373 Midterm I Fall 2009 (Closed book, closed notes)

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

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

COMP251: Background. Jérôme Waldispühl School of Computer Science McGill University

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Sample Exam 1 Questions

Final Examination CSE 100 UCSD (Practice)

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

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

Data Structures Question Bank Multiple Choice

CS 61BL Midterm 2 Review Session. Chris Gioia, Patrick Lutz, Ralph Arroyo, Sarah Kim

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

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Programming II (CS300)

CS:3330 (22c:31) Algorithms

Chapter 20: Binary Trees

Computer Science E-22 Practice Final Exam

Priority Queues Heaps Heapsort

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Cpt S 122 Data Structures. Data Structures Trees

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

COMP Analysis of Algorithms & Data Structures

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

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

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

EE 368. Week 6 (Notes)

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING ECE ALGORITHMS AND DATA STRUCTURES Total:

Transcription:

Part : Multiple Choice Enter your answers on the Scantron sheet. We will not mark answers that have been entered on this sheet. Each multiple choice question is worth. marks. Note. when you are asked to compute the order of the time complexity function you need to give the tightest order. So, while it is true that the function f(n) = n+ is O(n ), you should indicate that f(n) is O(n) and not that f(n) is O(n ).. Two algorithms, A and B with time complexities f A (n) and f B (n), respectively, are to be executed in the same computer. Assume that f A (n) is O(f B (n)) and f B (n) is not O(f A (n)). Which of the following statements is true? (A) A is faster than B for all inputs. (B) B is faster than A for all inputs. (C) There is a constant value n0 such that A is faster than B for every input of size n n 0. (D) There is a constant value n 0 such that B is faster than A for every input of size n n 0. (E) Depending on the computer used, it could be that A is faster than B for all large size inputs, or it could be that B is faster than A for all large size inputs.. Let f(n), g(n), and h(n) be three functions with positive values for every n 0. Assume that f(n) < g(n), and h(n) < g(n) for all n 0. Which of the following statements must be true for every triplet of functions f(n), g(n), h(n) as above? (A) f(n) is O(h(n)) (B) g(n) is not O(f(n)) (C) (f(n)+h(n)) is not O(g(n)) (D) f(n) is O(g(n)) (E) (f(n) h(n)) is not O(g(n)). Consider the following algorithm. Algorithm eval(r, k) Input: Root r of a proper binary tree and value k 0. if r is a leaf then return k else return eval(left child of r, k +) + eval(right child of r, k +) Assume that above algorithm eval is performed over the following tree. The initial call is i eval(r,0). What value will the algorithm return? (A) 0 r (B) (C) 9 (D) 6 (E) 4 6 6

4. Consider the following algorithms, where A is an array storing n positive integer values. Algorithm process(a, n) Input: Array A of size n for k 0 to n do { change(a, n) Algorithm change(a, n) Input: Array A of size n i 0 while i < n do { j 0 while j n do { A[j] A[j]+ i i+ j j + What is the worst case time complexity of algorithm process? (A) O(n) (B) O(n logn) (C) O(n ) (D) O(n logn) (E) O(n ). Let T be a proper binary tree (so each internal node has two children) with root r. Consider the following algorithm. Algorithm traverse(r) Input: Root r of a proper binary tree. if r is a leaf then return 0 else { t traverse(left child or r) s traverse(right child or r) return +s+t What does the algorithm do? (A) It computes the number of edges in the tree. (B) It computes twice the number of leaves in the tree. (C) It computes twice the value of the height of the tree. (D) It computes twice the total number of nodes in the tree. (E) It computes the number of internal nodes in the tree plus.

6. Let T be a proper binary tree with 7 nodes: a,b,c,d,e,f,g. A preorder traversal of T visits the nodes in this order: a,b,c,d,e,f,g. A postorder traversal of T visits the nodes in this order: c,e,f,d,b,g,a. Which node is at a distance from the root of T? (Hint. In tree T, node d is the right child of node b.) (A) a (B) b (C) c (D) d (E) e 7. A set of n keys: {k,...,k n is to be stored in an initially empty binary search tree. Which of the following statements is always true? (A) If ki is the smallest key, then in every binary search tree storing the above set of keys, the left child of the node storing k i is a leaf. (B) The resulting binary search tree has the same height, regardless of the order in which the keys are inserted in the tree. (C) A preorder traversal of the tree visits the keys in increasing order of value. (D) After inserting the keys, the key stored at the root of the tree is the same regardless of the order in which the keys are inserted. (E) None of the above statements is always true. 8. An Updatable Dictionary is an ADT that can store a set of n data items with integer keys, and it provides the following four operations: find(key), smallest(), successor(key) and increasekey(key). Duplicated keys are not allowed in the dictionary. Operations find, smallest, and successor are as in the Ordered Dictionary ADT that we studied in class. Operation increasekey(key) finds the data item with the given key, if it exists, and it increases its key value by one if doing so does not create duplicated keys; otherwise, no keys are modified. Which of the following statements regarding the most efficient way to implement the above operations of a Updatable Dictionary is true? (A) A hash table with a good hash function and separate chaining is the best choice as all operations would have O() average running time. (B) A binary search tree is the best data structure, as all operations can be performed in O(logn) time in the worst case, and these trees are much simpler than AVL trees. (C) A sorted linked list is the best data structure as smallest can be performed in O() time in the worst case, while find, successor, and increasekey need O(logn) time in the worst case. Furthermore, insertions can be performed in O(log n) time. (D) A hash table using double hashing is the best choice as all operations can be performed in O(logn) time in average. (E) A sorted array is the best data structure, as operations find, increasekey, and successor can be performed in O(log n) time in the worst case. Operation smallest only takes O() time in the worst case. 4

Part : Written Answers Write your answers directly on these sheets. You might find these facts useful: In a proper binary tree with n nodes the number of leaves is (n+)/ and the number of internal nodes is (n )/. 9. [4 marks] Consider a proper binary tree where each node p stores a label p.label. A proper binary tree is well labelled if for every internal node the label of the internal node is different from the labels of its children. For example, the trees below with roots r and r are well labelled, but the tree with root r is not as node a and its left child have the same label,. Write an algorithm well labelled(r) that receives a input the root of a proper binary tree and returns true if the tree is well labelled and it returns false otherwise. Use r.left and r.right to denote the children of r. r 8 8 7 7 r r Algorithm well labelled(r) Input: Root r of a proper binary tree Output: true if the tree is well balanced; false otherwise if r is a leaf then return true else if (r.label = r.left.label) or (r.label = r.right.label) then return false else if well labelled(r.left) = false then return false else return well labelled(r.right) 4 a 0. [ marks] Explain what the worst case for the algorithm is. [4. marks] Compute the time complexity of the above algorithm in the worst case. You need to explain how you computed the time complexity. [0. marks] Compute the order ( bigh Oh ) of the time complexity. The worst case for the algorithm is when the inputtree is well balanced as then both recursive calls need to be made per node, and hence, the algorithm does not terminate early. Ignoring recursive calls, the algorithm performs a constant number c of operations when the node being visited is a leaf, and it performs a constant number c of operations when the node is internal. The algorithm performs a preorder traversal of the tree, so the algorithm makes one recursive call per node. Therefore, the total number of operations performed by the algorithm is c n+ n number of leaves + c number of internal nodes = c +c. Ignoring constant factors in the time complexity, we see that the complexity is O(n).

. [4 marks] Given an array A storing m integer values, and an array B storing n integer values, write in pseudocode an algorithm subarray(a,b,m,n) that returns the value true if A is a sub-array of B and it returns false otherwise. A is a sub-array of B iff there is a position 0 j n m such that A[i] = B[i+j] for all i = 0,,...m. For example, for the arrays A and B shown below the algorithm must return the value true, but for the arrays A and B, the algorithm must return the value false. A 8 4 7 B 4 7 8 4 7 A 8 Algorithm subarray(a, B, m, n) Input: Array A storing n integer values and array B storing m n integer values. Output true if A is a subarray of B; false otherwise. for i B 0 to n m do { i A 0 while (i A < m) and (A[i A ] = B[i A +i B ]) do i A i A + if i A = m then return true return false. [ marks] Explain what the worst case for the algorithm is. [. marks] Compute the worst case time complexity of the above algorithm as a function of m and n. You need to explain how you computed the time complexity. [0. marks] Compute the order ( bigh Oh ) of the time complexity. The worst case for the algorithm is when A is not a subarray of B and every value in A, except the last one, is in B. For example, the first m values in A could be all equal to and the last one could be, while all values in B are. This ensures that the while loop performs the maximum number of iterations and the algorithm does not terminate early. Each iteration of the while loop performs a constant number c of operations and the loop is repeated m times in the worst case, as described above, so the number of operations performed by this loop is c (m ). Outsidethewhileloop, butinsidetheforloopanadditionalconstantnumberc ofoperations are performed, so each iteration of the for loop performs c +c (m ) operations. Since the for loop is repeated n m times, the total number of operations is (n m)(c +c (m )). Ignoring constant terms, the time complexity is O(nm). 6

. [7 marks] Consider the following algorithm. A is an array of size n. Algorithm rec(n) In: Integer value n. if n = then return A[0] else { i rec(n ) A[n ] A[n ]+i return A[n ] Write a recurrence equation for the time complexity of this algorithm. f() = c, where c is the constant number of operations performed in the base case f(n) = c +f(n ) for n >, where c is the constant number of operations performed by the algorithm ignoring the recursive call. 4. [6. marks] Solve the above recurrence equation by repeated substitution. You need to show how you solved the equation. [0. marks] Compute the order of the time complexity. f(n) = c +f(n ) f(n ) = c +f(n ) f(n ) = c +f(n ). f() = c +f() Replacing the value of f(n ) in the expression for f(n) and then replacing the value of f(n ) in the resulting expression, and so on, we get f(n) = c +c +c + +c +f() = c +c +c + +c +c. To determine the number of times that the term c appears in the above summation, note that the above set of equations, for f() to f(n), includes n equations. Therefore, the term c appears n times, and so f(n) = (n )c +c. Ignoring constant terms, we get that f(n) is O(n). 7

. Consider a hash table of size 7 with hash function h(k) = kmod7. Draw the contents of the table after inserting, in the given order, the following values into the table: 9, 6,, 6, and : [4 marks] (a) when linear probing is used to resolve collisions [8 marks] (b) when double hashing with secondary hash function h (k) = (kmod) is used to resolve collisions. 0 4 6 Linear probing 6 9 6 0 4 6 Double hashing 6 9 6 6. [ marks] Consider the following binary search tree. Insert the key and then insert the key 6 into the tree. Draw the tree after each insertion. You must use the algorithms described in class for inserting data in a binary search tree. 0 insert 0 0 0 Insert 6 0 0 6 8

7. [4 marks] Consider the following binary search tree. Remove the key from the tree and draw the resulting tree. Then remove the key from this new tree and show the final tree (so in the final tree both keys, and, have been removed). You must use the algorithms described in class for removing data from a binary search tree. remove 0 0 4 0 4 0 0 0 remove 0 4 0 0 9

Western University Department of Computer Science PART I PART II Last Name: Fist Name: CS0 Class list number: Student Number: CS0A Midterm Examination November 7, 06 9 pages, 7 questions 0 minutes 9 0 4 6 7 Total Instructions Write your name, CS0 class list number, and student number on the space provided. Please check that your exam is complete. It should have 9 pages and 7 questions. The examination has a total of 00 marks. When you are done, call one of the TA s and they will pick your exam up.