COS 226 Algorithms and Data Structures Fall Final Exam

Similar documents
COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall Final

COS 226 Algorithms and Data Structures Spring Final

COS 226 Algorithms and Data Structures Fall Final

Final Exam. COS 226 Algorithms and Data Structures Fall 2015

COS 226 Algorithms and Data Structures Spring Final Exam

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 Written Exam 2 Fall 2016

COS 226 Algorithms and Data Structures Spring Second Written Exam

COS 226 Final Exam, Spring 2009

Final Exam Solutions

COS 226 Final Exam, Spring 2010

Princeton University Computer Science COS226: Data Structures and Algorithms. Final, Spring 2013

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Spring Midterm

Final Exam Solutions

COS 226 Midterm Exam, Spring 2010

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

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Spring Midterm Exam

COS 126 General Computer Science Spring Written Exam 1

COS 226 Algorithms and Data Structures Spring Midterm

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

COS 226 Midterm Exam, Spring 2011

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

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

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

CSE332 Summer 2012 Final Exam, August 15, 2012

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

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

Algorithms: Design & Practice

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

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

COS 226 Midterm Exam, Spring 2009

Questions Total Points Score

CSE 332 Winter 2018 Final Exam (closed book, closed notes, no calculators)

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

Princeton University Computer Science COS226: Data Structures and Algorithms. Midterm, Fall 2013

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

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

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

Final Examination CSE 100 UCSD (Practice)

Your data structure should implement all operations in logarithmic time (or better) as a function of the size of the queue.

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

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

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

Midterm, Spring 2013

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

COS 126 General Computer Science Spring Written Exam 2

Computer Science E-22 Practice Final Exam

END-TERM EXAMINATION

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

Total Score /1 /20 /41 /15 /23 Grader

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

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

Ū P O K O O T E I K A A M Ā U I U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2014 TRIMESTER 1 *** WITH SOLUTIONS ***

COS 226 Algorithms and Data Structures Spring Midterm

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

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

COS 126 General Computer Science Fall Written Exam 1

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

( ) 1 B. 1. Suppose f x

COS 126 Written Exam 2 (Spring 2015)

Prelim 2. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

Table of Contents. Chapter 1: Introduction to Data Structures... 1

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

Questions Total Points Score

Index. stack-based, 400 A* algorithm, 325

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

University of Waterloo CS240R Fall 2017 Review Problems

1. AVL Trees (10 Points)

CS323: Data Structures and Algorithms Final Exam (December 17, 2014) Name:

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

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

COS 126 Written Exam 2, Fall 2009

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CS 216 Fall 2007 Final Exam Page 1 of 10 Name: ID:

Applied Databases. Sebastian Maneth. Lecture 14 Indexed String Search, Suffix Trees. University of Edinburgh - March 9th, 2017

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

ICS 161 Algorithms Winter 1998 Final Exam. 1: out of 15. 2: out of 15. 3: out of 20. 4: out of 15. 5: out of 20. 6: out of 15.

Algorithms. Algorithms 4.4 SHORTEST PATHS. APIs properties Bellman Ford algorithm Dijkstra s algorithm ROBERT SEDGEWICK KEVIN WAYNE

Graph Traversals. Ric Glassey

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

d. Solution coming soon. 6. Directed Graphs

Problem Score Maximum MC 34 (25/17) = 50 Total 100

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

COS 126 MIDTERM 1, FALL

University of Waterloo CS240R Winter 2018 Help Session Problems

CSE373 Fall 2013, Midterm Examination October 18, 2013

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

CS 314 Final Fall 2012

Graph Algorithms (part 3 of CSC 282),

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

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

Virtual University of Pakistan

Transcription:

COS 226 lgorithms and Data Structures Fall 2012 Final Exam This test has 16 questions worth a total of 100 points. You have 180 minutes. The exam is closed book, except that you are allowed to use a one page cheatsheet (8.5-by-11, both sides, in your own handwriting). No calculators or other electronic devices are permitted. Give your answers and show your work in the space provided. Write out and sign the Honor Code pledge before turning in the test. I pledge my honor that I have not violated the Honor Code during this examination. Problem Score Problem Score 0 8 1 9 2 10 3 11 4 12 5 13 6 14 7 15 Sub 1 Sub 2 Total Name: Login: Room: Precept: P01 F 11 Maia Ginsburg P02 F 12:30 Diego Perez Botero P03 F 1:30 Diego Perez Botero P03B F 1:30 Dushyant rora P04 Th 2:30 Maia Ginsburg P04 Th 2:30 Dan Larkin Jan 22: 653e 10f3 8823 1

2 PRINCETON UNIVERSITY 0. Initialization. (1 point) Write your name and Princeton NetID in the space provided on the front of the exam; circle your precept number; and write and sign the honor code. 1. nalysis of algorithms. (8 points) (a) Suppose that you observe the following running times for a program with an input of size N. N time 5,000 0.2 seconds 10,000 1.2 seconds 20,000 3.9 seconds 40,000 16.0 seconds 80,000 63.9 seconds Estimate the running time of the program (in seconds) on an input of size N =200,000. (b) How many bytes of memory does a KMP object consume as a function of the length of the pattern M and the size of the alphabet R? Use tilde notation to simplify your answer. public class KMP { private int[][] dfa; private char[] pat; } public KMP(String pattern, int R) { int M = pattern.length(); dfa = new int[r][m]; pat = new char[m];... }...

COS 226 FINL, FLL 2012 3 2. Graphs. (5 points) Consider the following Java class. ssume that digraph G has no parallel edges. public class Mystery { private boolean[] marked; public Mystery(Digraph G, int s) { marked = new boolean[g.v()]; mystery(g, s); } private void mystery(digraph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) mystery(g, w); } } public boolean marked(int v) { return marked[v]; } (a) Describe in one sentence what the method marked(v) returns for vertex v after calling the constructor with a digraph G and a vertex s. (b) Suppose that a Digraph is represented using the adjacency-lists representation. What is the order of growth of the running time of the constructor in the worst case? 1 V E E + V V 2 EV E 2 (c) Suppose that a Digraph is represented using the adjacency-lists representation. What is the order of growth of the running time of the constructor in the best case? 1 V E E + V V 2 EV E 2 (d) Suppose that a Digraph is represented using the adjacency-matrix representation. What is the order of growth of the running time of the constructor in the worst case? 1 V E E + V V 2 EV E 2

Final, Fall 2012 4 PRINCETON UNIVERSITY 3. Graph search. (6 points) Consider the following digraph. ssume the adjacency lists are in sorted order: for example, when iterating through the edges pointing from 2, consider the edge 2 7 before 2 8. 0 1 2 3 4 5 6 7 8 9 Run depth-firstpostorder: search on the digraph, C starting I H J from E vertex D 0. B G F preorder: B G F C H I D J E (a) List the vertices in reverse postorder. 0 (b) List the vertices in preorder. 0

Final, Fall 2012 COS 226 FINL, FLL 2012 5 4. Minimum spanning trees. (8 points) Suppose that a MST of the following edge-weighted graph contains the edges with weights x, y, and z. 130 B z C 20 D y E 110 100 140 80 120 90 70 60 50 F x G 40 H 30 I 10 J MST (a) List the weights of the other edges in the MST in ascending order of weight. 10 (b) Circle which one or more of the following can be the value of x? 5 15 25 35 45 55 65 75 85 95 105 115 125 135 145 (c) Circle which one or more of the following can be the value of y? 5 15 25 35 45 55 65 75 85 95 105 115 125 135 145 (d) Circle which one or more of the following can be the value of z? 5 15 25 35 45 55 65 75 85 95 105 115 125 135 145

Final, Fall 2012 6 PRINCETON UNIVERSITY 5. Shortest paths. (8 points) Suppose that you are running Dijkstra s algorithm on the edge-weighted digraph below, starting from vertex 0. 1 15 4 weight 2 5 1 4 x 0 15 2 6 5 y 7 23 7 22 29 18 3 12 6 The table below gives the edgeto[] and distto[] values immediately after vertex 4 has been deleted from the priority queue and relaxed. v distto[] edgeto[] 0 0.0 null 1 2.0 0 1 2 13.0 5 2 3 23.0 0 3 4 11.0 5 4 5 7.0 1 5 6 36.0 5 6 7 19.0 4 7

COS 226 FINL, FLL 2012 7 (a) Give the order in which the first 4 vertices were deleted from the priority queue and relaxed. 4 (b) What are all possible values of the weight of the edge x? (c) What are all possible values of the weight of the edge y? (d) Which is the next vertex to be deleted from the priority queue and relaxed? (e) In the table below, fill in those entries (and only those entries) in the edgeto[] and distto[] arrays that change (from the corresponding entries on the facing page) when the next vertex is deleted from the priority queue and relaxed. v distto[] edgeto[] 0 1 2 3 4 5 6 7

8 PRINCETON UNIVERSITY 6. Maximum flow. (8 points) Final, Fall 2012 Consider the following flow network and feasible flow f from from the source vertex to the sink vertex J. source flow capacity 9 / 9 B 4 / 10 C 7 / 7 D 5 / 6 E 7 / 7 9 / 14 5 / 5 0 / 4 3 / 8 5 / 5 4 / 6 11 / 11 5 / 5 F 7 / 10 G 21 / 21 H 13 / 20 I 9 / 15 J sink augmenting path: -G-B-C-H-I-J" min cut: {, B, C, F, G } max flow value = 28 (a) What is the value of the flow f? (b) Starting from the flow f given above, perform one iteration of the Ford-Fulkerson algorithm. List the sequence of vertices on the augmenting path. (c) What is the value of the maximum flow? (d) List the vertices on the source side of the minimum cut in alphabetical order. (e) What is the capacity of the minimum cut?

COS 226 FINL, FLL 2012 9 7. String sorting algorithms. (7 points) The column on the left is the original input of strings to be sorted; the column on the right are the strings in sorted order; the other columns are the contents at some intermediate step during one of the algorithms listed below. Match up each algorithm by writing its number under the corresponding column. You may use a number more than once. KISS BB ENY BB ENY CDC SOD SDE BB ENY CDC INXS CDC BB BB WHM CKE CDC INXS QU DIDO QU QU QU BB CRS QU STYX BECK CRS BECK CDC BUSH MOBY JYZ BECK SOD BLUR CDC BLUR SOD BLUR BECK BB BLUR CDC BUSH FUEL BUSH CKE BECK CDC CDC BUSH KORN CKE BUSH CKE MUSE CKE SDE BECK CKE FUEL CRS BB CRS HOLE CRS DIDO WHM CRS BUSH DIDO QU DIDO SDE DIDO FUEL DIDO DIDO BB ENY CKE ENY BUSH ENY CKE KISS ENY WHM FUEL BLUR FUEL RUSH FUEL HOLE BLUR FUEL CKE HOLE JYZ HOLE BECK HOLE TSOL INXS HOLE BLUR INXS BECK INXS FUEL INXS KORN ENY INXS MUSE JYZ HOLE JYZ TSOL JYZ CRS SOD JYZ BECK KISS KORN KISS WHM KISS MUSE MOBY KISS MOBY KORN KISS KORN KORN KORN BUSH HOLE KORN HOLE MUSE TSOL TSOL DIDO MUSE RUSH KORN MOBY TSOL MOBY MOBY MOBY BLUR MOBY KISS QU MUSE JYZ RUSH MUSE MUSE KISS RUSH QU TSOL RUSH QU STYX SDE SDE INXS STYX BLUR STYX SDE SDE SOD WHM WHM CRS SOD INXS FUEL SOD CRS SDE SOD SOD STYX SDE ENY MUSE STYX DIDO TSOL RUSH RUSH MOBY TSOL STYX BUSH TSOL RUSH WHM STYX STYX JYZ WHM JYZ RUSH WHM ---- ---- ---- ---- ---- ---- ---- ---- ---- 0 1 (0) Original input (1) Sorted (2) LSD radix sort (3) MSD radix sort (4) 3-way string quicksort (no shuffle)

10 PRINCETON UNIVERSITY 8. Ternary search tries. (6 points) Consider the following ternary search trie, where the values are shown next to the nodes of the corresponding string keys. Final, Fall 2012 C 7 T 3 G G C 5 13 C 8 T 9 4 11 17 T G T 12 (a) Circle which one or more of the following strings are keys in the TST? G C C CC CT CG CGC T TC TC TGT TT TTT (b) Insert the two strings CGTT and TG into the TST with the associated values 0 and 99, respectively; update the figure above to reflect the changes.

COS 226 FINL, FLL 2012 11 9. Knuth-Morris-Pratt substring search. (5 points) Below is a partially-completed Knuth-Morris-Pratt DF for a string s of length 12 over the alphabet {, B, C }. Reconstruct the string s in the space below. (You need not fill in the first three rows of the table, but they may be used to award partial credit.) 0 1 2 3 4 5 6 7 8 9 10 11 1 11 12 B 0 5 C 3 3 0 0 s

12 PRINCETON UNIVERSITY 10. Boyer-Moore substring search. (5 points) Suppose that you run the Boyer-Moore algorithm (the basic version considered in the textbook and lecture) to search for the pattern I D O F T H E in the text Final, Fall 2012 M E N D E R O F R O D S W I T H T H E I D O F T H E Give the trace of the algorithm in the grid below, circling the characters in the pattern that get compared with the text. M E N D E R O F R O D S W I T H T H E I D O F T H E I D O F T H E

Final, Fall 2012 COS 226 FINL, FLL 2012 13 11. Regular expressions. (6 points) Suppose that we run the RE-to-NF construction algorithm from the lecture and textbook on the regular expression ( B ( C D * ) * ). The match transitions are shown below. 0 1 2 3 4 5 6 7 8 9 10 11 ( B ( C D * ) * ) Circle which one or more of the following edges are in the ɛ-transition digraph. 0 2 0 3 0 4 0 8 2 8 2 9 2 10 2 11 3 4 3 6 3 8 3 9 5 6 5 7 6 5 6 7 8 10 9 2 9 3 9 8

14 PRINCETON UNIVERSITY 12. Huffman codes. (5 points) (a) Draw the Huffman trie corresponding to the encoding table below. char freq encoding B 2 01111 F 1 01110 H 3 0110 I? 00 L 5 010 M 15 10 S 15 11 (b) Circle which one or more of the following are possible values for the frequency of the character I. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

COS 226 FINL, FLL 2012 15 13. Data compression. (6 points) What is the compression ratio achieved by the following algorithms and inputs? Write the best-matching letter from the right-hand column in the space provided. For Huffman and LZW, assume that the input is a sequence of 8-bit characters (R = 256). Recall, the compression ratio is the number of bits in the compressed message divided by the number of bits in the original message.. 1/4096 Run-length coding with 8-bit counts for best-case inputs of N bits. B. 1/3840 C. 1/2731 D. 1/2560 Run-length coding with 8-bit counts for worst-case inputs of N bits. E. 1/320 F. 1/256 G. 1/255 Huffman coding for best-case inputs of N characters. H. 1/128 I. 1/127 J. 1/32 Huffman coding for worst-case inputs of N characters. LZW coding for best-case inputs of N characters using 12- bit codewords. Recall: no new codewords are added to the table if the table already has 2 12 = 4096 entries. K. 8/255 L. 1/16 M. 1/8 N. 1/7 O. 1/4 P. 1/2 LZW coding for worst-case inputs of N characters using with 12-bit codewords. Recall: no new codewords are added to the table if the table already has 2 12 = 4096 entries. Q. 2/3 R. 1 S. 3/2 T. 2 U. 3 V. 4 W. 7 X. 8

16 PRINCETON UNIVERSITY 14. lgorithm design. (8 points) Two strings s and t are cyclic rotations of one another if they have the same length and s consists of a suffix of t followed by a prefix of t. For example, "suffixsort" and "sortsuffix" are cyclic rotations. Given N distinct strings, each of length L, design an algorithm to determine whether there exists a pair of distinct strings that are cyclic rotations of one another. For example, the following list of N = 12 strings of length L = 10 contains exactly one pair of strings ("suffixsort" and "sortsuffix") that are cyclic rotations of one another. algorithms polynomial sortsuffix boyermoore structures minimumcut suffixsort stackstack binaryheap digraphdfs stringsort digraphbfs For full credit, the order of growth of the running time should be NL 2 (or better) in the worst case. You may assume that the alphabet size R is a small constant. Your answer will be graded on correctness, efficiency, clarity, and succinctness. (a) Describe your algorithm in the space below. (b) What is the order of growth of the running time of your algorithm (in the worst case) as a function of both N and L?

15. Reductions. (8 points) Consider the following two graph problems: LongestPath. Given an undirected graph G and two distinct vertices s and t, find a simple path (no repeated vertices) between s and t with the most edges. LongestCycle. Given an undirected graph G, find a simple cycle (no repeated vertices or edges except the first and last vertex) with the most edges. Final, Fall 2012 Longest Path (a) Show that LongestPath linear-time reduces to LongestCycle. Give a brief description of your reduction. To illustrate your reduction, superimpose the Longest- Cycle instance G that it constructs in order to solve the following LongestPath instance G: s 1 2 3 t 4 5 6 (b) Circle which one or more of the following that can you infer from the facts that LongestPath is NP-complete and that LongestPath linear-time reduces to LongestCycle. i. If there exists an N 3 algorithm for LongestCycle, then P = NP. ii. If there does not exist an N 3 algorithm for LongestCycle, then P NP. iii. If there exists an N 3 algorithm for LongestCycle, then there exists an N 3 algorithm for LongestPath. iv. If there exists an N 3 algorithm for LongestPath, then there exists an N 3 algorithm for LongestCycle. 17