Technical University of Denmark

Similar documents
Technical University of Denmark

Technical University of Denmark

String Matching. Pedro Ribeiro 2016/2017 DCC/FCUP. Pedro Ribeiro (DCC/FCUP) String Matching 2016/ / 42

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

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

COS 226 Algorithms and Data Structures Spring Second Written Exam

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

String matching algorithms تقديم الطالب: سليمان ضاهر اشراف المدرس: علي جنيدي

Problem Set 9 Solutions

: Fundamental Data Structures and Algorithms. June 06, 2011 SOLUTIONS

4. Suffix Trees and Arrays

SORTING. Practical applications in computing require things to be in order. To consider: Runtime. Memory Space. Stability. In-place algorithms???

END-TERM EXAMINATION

Applications of Suffix Tree

UNIVERSITY OF TORONTO Department of Computer Science April 2014 Final Exam CSC373H1S Robert Robere Duration - 3 hours No Aids Allowed.

Assignment 2 (programming): Problem Description

CSci 231 Final Review

1 (15 points) LexicoSort

CSci 231 Homework 7. Red Black Trees. CLRS Chapter 13 and 14

CS 170 Algorithms Spring 2009 David Wagner MT2

Lecture 7 February 26, 2010

CSci 231 Homework 7. Red Black Trees. CLRS Chapter 13 and 14

Remaining Enhanced Labs

CMSC Theory of Algorithms Second Midterm

Data Structure and Algorithm Midterm Reference Solution TA

4. Suffix Trees and Arrays

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Indexing and Searching

COS 226 Algorithms and Data Structures Fall Final

Exact String Matching. The Knuth-Morris-Pratt Algorithm

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Thursday 27th January 2011 Time: 14:00-16:00

COMP 261 ALGORITHMS and DATA STRUCTURES

Ū 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 2015 TRIMESTER 1 *** WITH SOLUTIONS ***

O T E Ū 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 2015 TRIMESTER 1 *** WITH SOLUTIONS ***

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

UNIVERSITY REGULATIONS

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

Algorithms and Data Structures

Algorithms and Data Structures Lesson 3

Final Exam Solutions

Ū 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 ***

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

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

ECE 250 Algorithms and Data Structures

Questions Total Points Score

COMPUTER SCIENCE TRIPOS

MULTIPLE OPERAND ADDITION. Multioperand Addition

CS 4349 Lecture October 18th, 2017

XVIII Open Cup named after E.V. Pankratiev Stage 1: Grand Prix of Romania, Sunday, September 17, 2017

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 25 Suffix Arrays

Solution for Homework set 3

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

Interlude: Problem Solving

Introduction to Algorithms February 24, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Handout 8

Question Points Score TOTAL 50

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

Knuth-Morris-Pratt. Kranthi Kumar Mandumula Indiana State University Terre Haute IN, USA. December 16, 2011

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

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

Revising CS-M41. Oliver Kullmann Computer Science Department Swansea University. Robert Recorde room Swansea, December 13, 2013.

CSE Qualifying Exam, Spring February 2, 2008

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

STUDENT LESSON A12 Iterations

( ) n 5. Test 1 - Closed Book

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

: Principles of Imperative Computation Victor Adamchik. Practice Exam - I

String Matching Algorithms

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

1. To reduce the probability of having any collisions to < 0.5 when hashing n keys, the table should have at least this number of elements.

Revising CS-M41. Oliver Kullmann Computer Science Department Swansea University. Linux Lab Swansea, December 13, 2011.

Homework 1 Due Tuesday, January 30, 2018 at 8pm

Solutions to Final. Solutions to Final. May 18th CS170 David Wolfe ALWAYS TRUE SOMETIMES TRUE NEVER TRUE

Figure 4.1: The evolution of a rooted tree.

Faculty of Mathematics and Natural Sciences

CPSC W2: Midterm Exam

ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

OF VICTORIA EXAMINATIONS- DECEMBER 2011 CSC

Final Exam. Jonathan Turner 5/12/2010. CS 542 Advanced Data Structures and Algorithms

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

Data Structures and Algorithms

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

tree follows. Game Trees

CSCI S-Q Lecture #13 String Searching 8/3/98

CS 157: Assignment 5

Solution to CSE 250 Final Exam

Suffix links are stored for compact trie nodes only, but we can define and compute them for any position represented by a pair (u, d):

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

CSE332 Summer 2010: Final Exam

Final Exam. 1. (8 pts) Consider the following directed graph. If you iterate through all of the vertices as

COMP 250 Midterm #2 March 11 th 2013

LING/C SC/PSYC 438/538. Lecture 18 Sandiway Fong

CSE 241 Algorithms and Data Structures Fall Semester, Midterm Exam

CPSC 35 Midterm Exam

Exam 2. Name: UVa ID:

CS125 : Introduction to Computer Science. Lecture Notes #40 Advanced Sorting Analysis. c 2005, 2004 Jason Zych

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

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

Transcription:

page 1 of 12 pages Technical University of Denmark Written exam, December 11, 2015. Course name: Algorithms and data structures. Course number: 02110. Aids allowed: All written materials are permitted. Exam duration: 4 hours Weighting: Question 1: 6% - Question 2: 24% - Question 3: 15% - Question 4: 15% - Question 5: 11% - Question 6: 18% - Question 7: 11%. The weighting is only an approximative weighting. You can answer the exam in either Danish or English. All questions should be answered by filling out the blank space below the question. As exam paper just hand in this and the following pages filled out. If you need more space you can use extra paper that you hand in together with the exam paper. The exam set continues on the next page 1

page 2 of 12 pages Question 1 In the following two questions, there might be more than one statement that is true (there might also be 0). Question 1.1 In which of the following data structures does an insertion take worst case O(log n) time: A Red-black tree B Splay tree C 2-3-4 tree Question 1.2 In which of the following data structures does n insertions take worst case O(n log n) time: A Red-black tree B Splay tree C 2-3-4 tree Question 1.3 Let T be a red-black tree and let S be a splay tree both with n nodes. Let d T (x) be the depth of node x in T and let d S (x) be the depth of node x in S. Let insert(x) be the last operation performed in both trees. Then: A d T (x) d S (x) B d S (x) d T (x) C d T (x) = O(log n) D d S (x) = O(log n) The exam set continues on the next page 2

page 3 of 12 pages Question 2 Question 2.1 Draw the suffix tree for the string pinepie$ (don t replace the labels by indexes into the string, just write the labels in the vertices): Question 2.2 pinepie: Compute the prefix function as used in the Knuth-Morris-Pratt algorithm for the string i 1 2 3 4 5 6 7 P [i] p i n e p i e π[i] The exam set continues on the next page 3

page 4 of 12 pages Question 2.3 Draw the string matching finite automaton for the string pinepie: The exam set continues on the next page 4

page 5 of 12 pages Question 3 Consider the network N below with capacities on the edges. 4 7 A B C 7 3 1 6 3 6 4 5 s D E F t 8 8 2 7 9 G H I Question 3.1 Give a maximum flow from s to t in the network N (write the flow for each edge along the edges on the graph below), give the value of the flow, and give a minimum s t cut (give the partition of the vertices). A B C s D E F t G H I value of flow: minimum cut: The exam set continues on the next page 5

page 6 of 12 pages Question 3.2 Use Edmonds-Karp s algorithm to compute a maximum flow in the network N. For each augmenting path write the nodes on the path and the value you augment the path with in the table below. augmenting path value The exam set continues on the next page 6

page 7 of 12 pages Question 4 The Dean has asked you to arrange the annual Christmas party for the students at DTU. You have to make a plan for how to place the tables in the hall. The local fire department has divided the hall up into an n m grid of subsquares and declared that you can place at most two tables in each row and at most one in each column. Unfortunately, the Dean who loves Christmas has put up Christmas trees in many of the subsquares. You cannot place a table in a subsquare with a Christmas tree. Example Here n = 4 and m = 8. The are Christmas trees and T are tables. In the example the maximum number of tables that can be placed is 7. T T T T T T T Question 4.1 Model the problem as a graph problem. Explain how you model the problem as a graph problem and draw the graph corresponding to the example above. The exam set continues on the next page 7

page 8 of 12 pages Question 4.2 Describe an algorithm that given n, m, and the placement of the Christmas trees computes the maximum number of tables you can place in the hall. Analyze the asymptotic running time of your algorithm. Remember to argue that your algorithm is correct. The exam set continues on the next page 8

page 9 of 12 pages Question 5 You are putting together a set of Christmas songs that will be handed out at the party. The Dean has declared that every song must contain the sentence Merry Christmas Dear Dean, where denotes a blank space. E.g. the song: We wish you a Merry Christmas We wish you a Merry Christmas We wish you a Merry Christmas Dear Dean Dear Dean contains one occurrence of of the sentence Merry Christmas Dear Dean (line breaks are disregarded). Formally, you are given a set S of songs S 1,..., S k and a sentence P. Song S i contains n i characters and P contains m characters. Let n = k i=1 n i denote the total number of characters in the songs. All the strings are over an alphabet of size O(1). Describe an algorithm that returns all the songs that contain P. Analyze the asymptotic running time of your algorithm. Remember to argue that your algorithm is correct. The exam set continues on the next page 9

page 10 of 12 pages Question 6 At the Christmas party there will be a game called Bing or Ding. There is a row of n red socks, each with an integer on it, which can be positive, negative, or zero. Let s i denote the number on sock i. The rules are as follows: You have to consider all socks in order from sock 1 to n. At each sock you must say either Bing! or Ding!. If you say Bing! at the ith sock you get s i Christmas cookies. If you say Ding! at the ith sock you must pay s i Christmas cookies. It is possible to end up with a negative number of Christmas cookies. It is forbidden to say the same word more than two times in a row. For example if you say Bing! at sock 5 and 6 you must say Ding! at sock 7. Let L(1, i) denote the maximum number of Christmas cookies you can have after the first i socks if you say Bing! at sock i. Similarly, let L(0, i) denote the maximum number of Christmas cookies you can have after the first i if you say Ding! at sock i. Let L(b, 0) = 0 for b {0, 1}. Question 6.1 Fill out the table below for L(b, i) when S = [s 1, s 2, s 3, s 4 ] = [3, 4, 8, 4]. L(b, i) 0 1 2 3 4 0 1 Question 6.2 Which of the following recurrences correctly computes L(b, i): 0 if i = 0 A L(b, i) = L(0, i 1) + s i if b = 1 L(1, i 1) s i if b = 0 0 if i = 0 s 1 if b = 1 and i = 1 B L(b, i) = s 1 if b = 0 and i = 1 max{l(1, i 2) s i 1, L(0, i 2) + s i 1 } + s i if b = 1 and i 2 max{l(1, i 2) + s i 1, L(0, i 2) s i 1 } s i if b = 0 and i 2 0 if i = 0 s 1 if b = 1 and i = 1 C L(b, i) = s 1 if b = 0 and i = 1 max{l(0, i 1), L(0, i 2) + s i 1 } + s i if b = 1 and i 2 max{l(1, i 1), L(1, i 2) s i 1 } s i if b = 0 and i 2 The exam set continues on the next page 10

page 11 of 12 pages Question 6.3 Write pseudocode for an algorithm based on dynamic programming and the recurrence from Question 6.2 that finds the maximum number of Christmas cookies you can end up with given n socks. Analyze the space usage and running time of your algorithm in terms of n. The exam set continues on the next page 11

page 12 of 12 pages Question 7 During the party the Dean suddenly wants to know who won most Christmas cookies in the Bing or Ding game. He suggests the following algorithm: Algorithm 1: Find student with most cookies max s null Randomly order the students. Let s 1,..., s n be the students in this random order. Let c i denote the number of cookies won by student s i. for i = 1,..., n do if end end return s if c i > max then max c i and s s i (*) In the following assume that all students won a different amount of cookies. That is, c i c j for all i j. Question 7.1 What is the probability that the line (*) is executed at the last iteration? Question 7.2 Let X i be a random variable that is 1 if line (*) is executed in iteration i and 0 otherwise. What is the probability that X i = 1? Question 7.3 What is the expected number of times line (*) is executed? 12