COS 226 Algorithms and Data Structures Fall Final Solutions. 10. Remark: this is essentially the same question from the midterm.

Similar documents
D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Material covered. Areas/Topics covered. Logistics. What to focus on. Areas/Topics covered 5/14/2015. COS 226 Final Exam Review Spring 2015

COS 226 Algorithms and Data Structures Fall Final Exam

Final Exam Solutions

d. Solution coming soon. 6. Directed Graphs

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Final Exam. COS 226 Algorithms and Data Structures Fall 2015

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

Questions Total Points Score

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

COS 226 Algorithms and Data Structures Fall Final

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

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Algorithms Dr. Haim Levkowitz

CS61B Spring 2016 Guerrilla Section 6 Worksheet

COS 226 Algorithms and Data Structures Fall Final

Final Exam Solutions

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

( ) n 3. n 2 ( ) D. Ο

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

( D. Θ n. ( ) f n ( ) D. Ο%

University of Waterloo CS240R Fall 2017 Review Problems

Greedy Approach: Intro

Theorem 2.9: nearest addition algorithm

Exam 3 Practice Problems

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

CMSC Theory of Algorithms Second Midterm

COS 226 Final Exam, Spring 2009

Final Examination CSE 100 UCSD (Practice)

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

COS 226 Algorithms and Data Structures Fall Midterm

CSC 373: Algorithm Design and Analysis Lecture 4

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

Data Structures Brett Bernstein

Data Structure and Algorithm Midterm Reference Solution TA

Lecture 7 February 26, 2010

( ) n 5. Test 1 - Closed Book

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

COS 226 Algorithms and Data Structures Spring Final

9. Which situation is true regarding a cascading cut that produces c trees for a Fibonacci heap?

CMPSCI 311: Introduction to Algorithms Practice Final Exam

CSci 231 Final Review

R16 SET - 1 '' ''' '' ''' Code No: R

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

n 2 ( ) ( ) + n is in Θ n logn

COMP3121/3821/9101/ s1 Assignment 1

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

CS521 \ Notes for the Final Exam

Test 1 - Closed Book Last 4 Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

Friday Four Square! 4:15PM, Outside Gates

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

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

ECE242 Data Structures and Algorithms Fall 2008

COMP 251 Winter 2017 Online quizzes with answers

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

( ) 1 B. 1. Suppose f x

CS61B, Fall 2015 Final Examination (with corrections) P. N. Hilfinger

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

University of Waterloo CS240 Spring 2018 Help Session Problems

UCS-406 (Data Structure) Lab Assignment-1 (2 weeks)

CS-301 Data Structure. Tariq Hanif

6. Finding Efficient Compressions; Huffman and Hu-Tucker

Chapter 23. Minimum Spanning Trees

Review of course COMP-251B winter 2010

END-TERM EXAMINATION

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

University of Waterloo CS240R Winter 2018 Help Session Problems

Lecture Summary CSC 263H. August 5, 2016

Algorithms and Data Structures

Linear Work Suffix Array Construction

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

COS 226 Final Exam, Spring 2010

ITCT Lecture 6.1: Huffman Codes

Graph and Digraph Glossary

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Parallel Programming. Easy Cases: Data Parallelism

CS61B, Fall 2011 Final Examination (corrected) P. N. Hilfinger

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

EE 368. Weeks 5 (Notes)

Introduction to Algorithms Third Edition

9/24/ Hash functions

COS 226 Algorithms and Data Structures Spring Second Written Exam

Indexing and Searching

Mining Frequent Patterns without Candidate Generation

University of Waterloo CS240R Fall 2017 Solutions to Review Problems

4.1.2 Merge Sort Sorting Lower Bound Counting Sort Sorting in Practice Solving Problems by Sorting...

CS/COE 1501

CS 125 Section #10 Midterm 2 Review 11/5/14

CS/COE 1501

Solving problems on graph algorithms

Dynamic Programming Algorithms

Lecture 18 April 12, 2005

Data Compression Techniques

Transcription:

COS 226 Algorithms and Data Structures Fall 2011 Final Solutions 1 Analysis of algorithms (a) T (N) = 1 10 N 5/3 When N increases by a factor of 8, the memory usage increases by a factor of 32 Thus, T (N) = an b, where b = log 8 32 = lg 32/ lg 8 = 5/3 Since T (1000) = 10000, we have 10000 = a 1000 5/3, which implies a = 1 10 Remark: this is essentially the same question from the midterm (b) B D F C B E A i Single loop ii Similar to insertion sort iii Similar to enumerating all permutations iv Similar to mergesort since f 1 (N) takes linear time v N + N/2 + N/4 + vi Similar to enumerating all subsets vii Similar to binary search 2 Graph search (a) 2 8 0 7 1 6 3 5 4 (b) 2 0 1 8 6 7 4 3 5 3 Minimum spanning trees (a) 1 2 3 4 5 8 11 15 (b) 1 5 2 4 8 3 11 15 The starting vertex must be either A or F (but it doesn t matter which) 1

4 Shortest paths (a) 3 0 10 5 2 It is not necessary to run the algorithm from the beginning to deduce this because Dijkstra s algorithm considers the vertices in increasing order of distance from the source (b) The next vertex to relax is 4 v distto[] edgeto[] 0 10 3 0 1 170 5 1 2 60 5 2 3 00 null 4 70 0 4 5 50 10 5 6 110 4 6 7 100 4 7 8 80 4 8 9 220 4 9 10 30 3 10 5 String sorting c count[] count[] count[] 'a' 0 0 0 'b' 0 0 3 'c' 3 3 5 'd' 2 5 11 'e' 6 11 17 'f' 6 17 20 'g' 3 20 20 i a[i] 0 blurb 1 climb 2 crumb 3 basic 4 cubic 5 freed 18 dwarf 19 cliff 6 Substring search 0 1 2 3 4 5 6 7 8 9 10 A 0 0 3 0 0 3 7 0 0 10 11 B 1 2 2 4 5 6 2 8 9 6 4 s B B A B B B A B B A A The answer is unique 2

7 Regular expressions The NFAs for (i), (ii), (iii), and (iv) would not be constructed by our RE-to-NFA algorithm (i) There are two missing ɛ-transitions: 2 6 and 6 2 (ii) The edges 2 3 and 7 8 should be match transitions; the edge 3 4 should be an ɛ-transition (iii) There are two missing match transitions: 2 3 and 4 5 Also, the edges 7 8 and 8 9 should be ɛ-transitions instead of match transitions (iv) The ɛ-transition 2 6 should instead be 0 5 (v) Correct 8 Ternary search tries BD C CD E FD JPG PEGS 9 String symbol table implementation BCDE Find the value associated with a given string key in the data structure CDE Associate a value with a string key CDE Delete a string key (and its associated value) from the data structure BCE Find the smallest string key in the data structure BCE Find the smallest string key in the data structure that is greater than or equal to a given string A Unordered array B Ordered array C Red-black BST D Separate-chaining hash table E Ternary search trie E Find the string key in the data structure that is the longest prefix of a given string E How many string keys in the data structure starts with a given prefix? Can also be done with an ordered array (B) or a red-black BST (C) by calling rank() twice, once with the prefix and once with the last character in the prefix incremented by one 3

10 Data compression (a) C A A C B i The frequency of A can be less than the frequency of B or it can be equal to the frequency of B ii Since A and B are merged first, they have are symbols that have the smallest frequencies iii Clearly freq(d) freq(a) Suppose freq(d) = freq(a) Then, since freq(d) freq(c) freq(a), we must have freq(d) = freq(c) = freq(a) In this case, C and D would be merged (instead of C and {A, B}) iv If A, B, and C have frequency 1, then D could have frequency 2 or 3 and produce the same subtree v If the frequency of E is strictly less than that of A, B, and C combined, then so is the frequency of D Hence, D and E would be merged Note that if a character appears 0 times, then it will not appear in the Huffman trie (b) LZW-encoded message original message or brief explanation why invalid 42 41 4E 82 41 80 B A N A N A 42 41 83 80 not possible since 83 would not yet be in table 41 42 81 82 80 A B A B B A 41 42 81 83 80 A B A B A B A (see p 843 of textbook) 42 41 4E 44 41 4E 41 80 not possible because of second occurrence of 41 4E 11 Maximum flow (a) 16 + 13 + 10 = 39 (b) s 1 2 6 5 t (c) After using the previous augmenting Final, Fall 2011 path, there are no more augmenting paths The value of the maximum flow is 41 (10 + 15 + 16) (d) {s, 1, 2, 4} (e) 10 + 4 + 17 + 10 = 41 This equals the value of the maximum flow, as it should 1 10 / 18 4 9 / 11 1 / 6 0 / 7 0 / 15 10 / 10 s 22 / 22 2 17 / 17 5 15 / 16 t 10 / 10 4 / 4 0 / 6 2 / 5 16 / 16 3 14 / 16 6 4

12 Algorithm design (a) Build an edge-weighted graph G containing all of the edges whose weight is strictly less than that of e = v w Find the connected components in G By the cut property, e is in the MST if and only if v and w are in different components (b) E + V 13 Reductions (a) For each i, set b[i] equal to a[i] and c[i] equal to -a[i] b[] -66-30 70 99-33 66 20 50 c[] 66 30-70 -99 33-66 -20-50 (b) Solution 1: For each j, include the integer b[j] + M (guaranteed to be positive); for each k, include the integer -(c[k] + 2*M) (guaranteed to be negative) Any triple in a[] that sums to zero must include two positive integers and one negative integer a[] 1299 1700 1010 1014 997 999 1020-2999 -2019-1996 -2600-2030 -2020 Solution 2: For each j, include the integer 10*b[j] + 1; for each k, include the integer -(10*c[k] + 2) To see why this works, consider the sum of the three integers modulo 10 Note: can replace 10 with any integer greater than or equal to 4 5