COS 226 Written Exam 2 Fall 2016

Similar documents
COS 226 Final Exam, Spring 2009

COS 226 Algorithms and Data Structures Fall 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 Final Exam, Spring 2010

COS 226 Algorithms and Data Structures Spring Final

COS 226 Algorithms and Data Structures Fall Final

COS 226 Algorithms and Data Structures Fall Final

COS 226 Midterm Exam, Spring 2011

Final Exam. COS 226 Algorithms and Data Structures Fall 2015

COS 226 Algorithms and Data Structures Spring Second Written Exam

Graph Algorithms (part 3 of CSC 282),

Graph Algorithms (part 3 of CSC 282),

COS 226 Midterm Exam, Spring 2009

COS 226 Algorithms and Data Structures Spring Final Exam

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

Final Exam Solutions

COS 126 Written Exam 2, Fall 2009

Final Exam Solutions

Review of course COMP-251B winter 2010

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

END-TERM EXAMINATION

COS 226 Algorithms and Data Structures Fall Midterm

Midterm 1 Written Exam

COS 226 Algorithms and Data Structures Fall Midterm

COS 126 Written Exam 2 (Spring 2015)

COS 126 Midterm 1 Written Exam Fall 2012

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

6 NFA and Regular Expressions

lec3:nondeterministic finite state automata

Exam 3 Practice Problems

Homework Assignment #3 Graph

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

CMPSCI 250: Introduction to Computation. Lecture 20: Deterministic and Nondeterministic Finite Automata David Mix Barrington 16 April 2013

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

COS 126 Midterm 1 Written Exam Fall 2011

University of Waterloo CS240R Fall 2017 Review Problems

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

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

Design and Analysis of Algorithms - - Assessment

COS 126 General Computer Science Spring Written Exam 1

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

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

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

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.

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

CISC 320 Midterm Exam

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

CSE332 Summer 2012 Final Exam, August 15, 2012

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

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

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Spring Midterm

CSci 231 Final Review

Indexing and Searching

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

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

COS 126 Midterm 1 Written Exam Fall 2014!

COS 226 Algorithms and Data Structures Spring Midterm

University of Waterloo CS240R Winter 2018 Help Session Problems

Midterm I (Solutions) CS164, Spring 2002

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

Total Score /15 /20 /30 /10 /5 /20 Grader

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

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

CSE 21 Summer 2017 Homework 4

COS 226 Midterm Exam, Spring 2010

CMSC351 - Fall 2014, Final Exam

Chapter 23. Minimum Spanning Trees

Theory Bridge Exam Example Questions Version of June 6, 2008

Introduction to Algorithms. Lecture 11

Theory of Computations Spring 2016 Practice Final Exam Solutions

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

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

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

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

CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings.

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

Lecture 6 Basic Graph Algorithms

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

COS 126 General Computer Science Fall Midterm 1

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

COS 226 Algorithms and Data Structures Fall Midterm

Review for Midterm Exam

COS 226 Algorithms and Data Structures Spring Midterm

ECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

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

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

ECE4050 Data Structures and Algorithms. Lecture 8: Graphs

Algorithms: Design & Practice

COS 126 Midterm 1 Written Exam, Fall 2009

Dijkstra s algorithm for shortest paths when no edges have negative weight.

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

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

Solutions to Exam Data structures (X and NV)

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

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

Konigsberg Bridge Problem

Automating Construction of Lexers

Transcription:

COS 226 Written Exam 2 Fall 2016 There are ten questions on this exam, weighted as indicated at the bottom of this page. There is one question per lecture, numbered corresponding Lectures 12 21, not in order of difficulty. If a question seems difficult to you, skip it and come back to it. Policies. The exam is closed book, though that you are allowed to use a single-page one-sided hand-written cheatsheet. No calculators or other electronic devices are permitted. Give your answers and show your work in the space provided. You will have 80 minutes to complete the test. This exam is preprocessed by computer. If you use pencil (and eraser), write darkly. Blacken circles or squares completely when asked. Write all answers inside the designated rectangles. Do not write on the corner marks. This page. Print your name, login ID, and precept number on this page (now), and write out and sign the Honor Code pledge. Discussing this exam. As you know, discussing the contents of this exam before solutions have been posted is a serious violation of the Honor Code. Name Login Precept I pledge my honor that I have not violated the Honor Code during this examination. [copy the pledge here] [signature] Q12 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21 TOTAL /7 /7 /7 /7 /7 /7 /7 /7 /7 /7 /70

Q12. Undirected graphs. A. Consider the following implementation of a graph-processing class that is supposed to allow clients to test whether two vertices are connected: public class CC { private int[] id; private int count = 1; public CC(Graph G) { id = new int[g.v()];; for (int s = 0; s < G.v(); s++) if (id[s] == 0) { dfs(g, s); count++; } } private void dfs(graph G, int v) { /* MISSING LINE OF CODE */ for (int w : G.adj(v)) if (id[w] == 0) dfs(g, w); } } public boolean connected(int v, int w) { return id[v] == id[w]; } In the box below, write the one line of Java code that is missing from the private dfs(). B. Blacken a circle on each line to indicate whether each statement is True or False. DFS is not an appropriate strategy for determining whether a graph contains a cycle. With DFS, we can support constant-time connectivity queries in an undirected graph, using linear space and linear preprocessing time. Union-find is preferable to DFS for connectivity when edge insertion must be supported. True False

Q13. Digraphs. Consider the digraph drawn at right. Assume that, in the internal representation, all vertices appear in numerical order in each adjacency list. In the table below, indicate the order in which the vertices appear in reverse postorder for a depth-first search starting at 0. Your answer must have exactly one blackened circle in each row and each column. first second third fourth fifth sixth seventh 0 1 2 3 4 5 6

Q14. MSTs. Consider the weighted graph drawn at right. Assume that, in the internal representation, all vertices appear in numerical order in each adjacency list. In the table below, indicate the order in which the vertices are added to the MST for Prim's algorithm starting at 0. Your answer must have exactly one blackened circle in each row and each column. first second third fourth fifth sixth seventh 0 1 2 3 4 5 6

Q15. Shortest paths. In each square at right, write the letter corresponding to the algorithm with the best worst-case running time to find a shortest paths tree for the corresponding type of graph, assuming that at least half of the vertices are reachable from the starting point. You may use each letter once, more than once, or not at all. A Kosaraju-Sharir B Bellman-Ford digraph with positive edge weights C brute force DAG with positive edge weights D Dijkstra DAG with edge weights that could be negative E topological sort digraph with edge weights that could be negative F Kruskal digraph with edge weights that could be negative but no negative cycles G Prim digraph with negative cycles not reachable from the start H none of the above

Q16. Maxflow. Consider the flow network drawn at right. As usual, each directed edge is labeled with its flow and its capacity, separated by a slash. A. The table below lists a number of paths in the underlying undirected graph. Considering these paths individually, blacken the circle corresponding to the correct characterization of each path. Your answer must have one blackened circle in each row except if you think that one or more of the paths is both shortest and fattest. not an augmenting path a shortest augmenting path a fattest augmenting path 0-1-4-6 0-1-4-3-6 0-5-3-6 0-5-2-3-6 0-2-3-4-6 B. Blacken the one circle corresponding to the value of the maxflow in this network. 7 8 9 10 11 12 13 14 15

Q17. String sorts. The column on the left is an array of strings to be sorted. The column on the right is in sorted order. The other columns are the contents of the array at some intermediate step during one of the algorithms below. Write the letter corresponding to the correct algorithm under the corresponding column. You will need to use some letters more than once. Hint : Do not trace code think about algorithm invariants. input sorted result ghana spain aruba aruba aruba chile ghana nepal aruba sudan italy benin benin burma egypt malta sudan benin wales ghana burma burma benin benin china qatar burma nepal gabon china congo china congo aruba macau chile italy libya congo chile congo burma kenya aruba china malta macau chile china chile china india yemen congo niger sudan egypt egypt egypt aruba libya niger egypt china india ghana gabon ghana gabon burma wales gabon yemen niger gabon ghana gabon ghana zaire congo ghana haiti chile haiti haiti haiti haiti chile india haiti aruba china italy kenya italy kenya haiti spain india kenya zaire india spain india spain nepal benin italy spain haiti kenya india kenya india sudan chile kenya india wales libya libya libya libya yemen italy libya libya malta malta yemen malta yemen spain burma macau burma yemen macau macau macau macau gabon ghana malta macau congo nepal niger nepal niger benin china nepal gabon benin niger malta niger malta congo gabon niger congo kenya qatar italy qatar italy niger egypt qatar benin nepal sudan nepal sudan nepal qatar zaire spain egypt burma spain zaire spain zaire wales malta sudan zaire qatar wales wales wales wales egypt haiti wales chile aruba yemen qatar yemen qatar macau kenya yemen qatar egypt zaire sudan zaire sudan italy libya zaire A E A. input B. LSD radix sort C. MSD radix sort D. 3-way radix quicksort (no shuffle) E. sorted result

Q18. Tries. A. Consider the TST drawn at right. In the table below, blacken each of the s q u a r e s t h a t correspond to the strings that were used to build it. You do not need to write the value just blacken the square. ca ga gc gg tg gaa gag gca gtg gcag gccg gcacg gcaga B. Blacken a circle on each line to indicate whether each statement is True or False. True False The shape of a TST depends only on the set of keys that were used to build it, not the order in which they were inserted. Search time in a TST built from randomly ordered keys is logarithmic in the number of keys on the average. The height of a TST is dependent on the size of the alphabet

Q19. Substring search. Here is a trace of a Boyer-Moore algorithm (using only the mismatched character heuristic). Two of the characters in the pattern have been replaced with x and y. i j i+j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 E O T H E R B U S I N E S S O F T E L L S y N x H y U x E S 0 6 6 y N x H y U x 7 5 12 y N x H y U x 8 6 14 y N x H y U x 10 6 16 y N x H y U x 17 5 22 y N x H y U x 21 0 21 y N x H y U x A. Blacken the one circle corresponding to the character that must be represented by y. B E F H I L N O R S T U B. Blacken the one circle corresponding to the character that must be represented by x. B E F H I L N O R S T U

Q20. REs. A. Drawn below is an NFA (nondeterministic finite state automaton) that recognizes the same language that the regular expression ((A*B C )* D) describes, except that it is missing four of its ϵ-transitions. In the table below the drawing, blacken the squares corresponding to the missing transitions. 0-10 1-4 1-6 1-8 1-10 3-2 4-1 4-3 5-6 5-7 9-10 10-1 B. It is easy to build an NFA (nondeterministic finite state automata) corresponding to a regular expression, and a basic theorem from automata theory says that we can convert every NFA to a DFA (deterministic finite state automata). Why do we not do so to implement RE pattern matching? Blacken one circle corresponding to the correct answer. The NFA might loop. The DFA might have an exponential number of states. There might exist a string that the NFA recognizes but the DFA does not. There might exist a string that the DFA recognizes but the NFA does not. The proof of the theorem is not constructive (does not tell us how to construct the DFA from the NFA).

Q21. Data compression. A. Suppose that you receive the following message that was encoded using LZW compression: 42 41 41 81 82 84 80 Your job is to finish decoding the message, by writing one letter in each square: B A B. Which of the following best describes the length of the code produced by the LZW compression algorithm for a string consisting of N characters that are all the same? Blacken one circle corresponding to the correct answer. log N (log N ) 2 N 1/2 N N log N