<<

Name: 60 points total

CSE 3353 Homework 4 Part II Spring 2014 Assignment is due at 2:00pm on May 5. Submit a hard copy of the assignment to CSE Departmental Secretary Debra McDowell, including a copy of your code and outputs as requested in the assignment. You should also turn in a copy of the code to Blackboard in a .zip file. The Python file for Q1 must be named playlist.py, and you must not modify the names of any of the given functions or their defined parameters. You may add your own functions as you see fit. Any outputs from running the code should be included in files called playlist output.txt, also included in the zip file. Both of these files must be included in a directory called LastnameFirstname. Please write your name(s) as a comment in the first line of code in the python files and output files. If you are working with a partner, turn in one set of code and one set of answers. You may choose to work with a partner on the code questions, the written questions, or both. PLEASE NOTE: If you worked with a partner on HW3, you may work with a partner again, but it must be a different partner than for HW3. You may work with the same partner from HW4 part I, but you do not have to do so. You may submit a lateness coupon request BEFORE the assignment is due by emailing [email protected] with Subject “CSE3353 Lateness Coupon”. All other late work will receive a 10 percentage point deduction per day (including weekends), No late work is accepted beyond 48 hours after the assignment is due. A maximum of two lateness coupons may be used on this assignment

Q1 (20)

Q2 (4)

Q3 (10)

Q4 (10)

Q5 (10)

Q6 (6)

Total (60)

1 Q1. (20 points) A music playlist is an ordered list of songs. Songs can be represented as the triple (track name, artist, genre). Write Python code to compute the “edit distance” between two music playlists, that is, the minimum number of alter- ations required to make the playlists equivalent. Implement the function playlist transform, whose behavior is specified by the following docstring: def p l a y l i s t transform (s,t ,compareType=”Song”): ””” Computes the edit distance for two playlists s and t, and prints the minimal edits required to transform playlist s into playlist t. I n p u t s : s: 1st playlist (format: list of (track name, artist , genre) triples) t: 2nd playlist (format: list of (track name, artist , genre) triples) compareType: String indicating the type of comparison to make. ”Song” (default): songs in a playlist are considered equivalent if the (song name, artist , genre) triples match. ”Genre”: songs in a playlist are considered equivalent if the same genre is used. ”Artist”: songs in a playlist are considered equivalent if the same artist is used. Output: The minimum edit distance and the minimal edits required to transform playlist s into playlist t. ””” Your code should be modular and follow a dynamic programming strategy. You are welcome to base your own code on code used in lectures. Here is example output for invoking playlist transform that transforms the playlist located at http: //lyle.smu.edu/˜tylerm/courses/cse3353/blues1.csv to the playlist located at http://lyle.smu.edu/ ˜tylerm/courses/cse3353/blues2.csv, using compareType =“Song”.

Comparing playlist similarity by song 6 edits required to turn playlist 1 into playlist 2. Insert [’ flood’, ’’, ’’] Leave ["Ain’t nothing wrong with that", ’Robert Randolph and the Family Band’, ’Rock’] unchanged Leave ["Couldn’t stand the weather", ’Stevie Ray Vaughan’, ’Blues’] unchanged Replace [’Power to love’, ’Jimi Hendrix’, ’Rock’] with [’San Francisco Bay Blues’, ’’, ’Blues’] Leave [’Going in the right direction’, ’Robert Randolph and the Family Band’, ’Blues’] unchanged Replace [’Red house’, ’Jimi Hendrix’, ’Blues’] with [’Purple Haze’, ’Jimi Hendrix’, ’Rock’] Replace [’Purple Haze’, ’Jimi Hendrix’, ’Rock’] with [’Red house’, ’Jimi Hendrix’, ’Blues’] Leave [’My way down’, ’Chris Duarte Group’, ’Rock’] unchanged Replace [’The thrill is gone’, ’B.B. King’, ’Blues’] with [’The thrill is gone’, ’Chris Duarte Group’, ’Blues’] Replace [’The thrill is gone’, ’Chris Duarte Group’, ’Blues’] with [’The thrill is gone’, ’B.B. King’, ’Blues’]

Additionally, you must include at least two of your own playlists for testing. Include the playlists and the output with your assignment (turn in both a hard and a copy on Blackboard). You may download starter code from http://lyle.smu.edu/ ˜tylerm/courses/cse3353/code/playlist_starter.txt, as well as make use of the provided read playlist function that processes a CSV playlist and returns a list of triples.

2 Q2. (4 points) Consider the following code. def two pow ( i ) : i f i == 0 : return 1 return two pow ( i −1)+two pow ( i −1)

a. What is the running time if no memoization is used?

b. What is the running time if memoization is used?

3 Q3. (10 points) Job name Start time End time Job weight A 7 11 4 B 4 14 6 C 15 17 2 Consider the following 8 jobs: D 10 16 5 E 3 8 3 F 6 12 5 G 1 5 2 H 9 13 3 Solve this weighted interval scheduling problem using a dynamic programming approach.

a. Draw the jobs on the following axis in the correct order. Label the job name next to the corresponding index.

Job Name Index

1

2

3

4

5

6

7

8

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

b. Fill out the following table.

Job name Index j Weight vj p(j) vj + M[p(j)] M[j − 1] M[j]

1 2 3 4 5 6 7 8

c. What is the total weight of the optimal job selection?

4 Q4. (10 points) Calculate the edit distance between the two strings “coral” and “koraal” (the Dutch word for coral). a. Complete the edit-distance table, including arrows to indicate which operation took place.

C k o r a a l

c

o

r

a

l

b. Based on the table, what is the minimum number of steps to transform “coral” into “koraal”?

c. Explain precisely the sequence of steps (including the operation and character affected) required to turn the string “coral” into “koraal”.

d. Do you think edit distance would be useful when designing automated language translation software such as Google Translate? Why or why not?

5 Q5. (10 points) Consider the following weighted directed graph:

4

6 a b −1

− 3 − 4 3 8 c 4 6 2 −8 d e a. Find the weight of the shortest path from c to all nodes in the graph using the Bellman-Ford al- gorithm. Complete the table that indicates intermediate values of the upper bound d of the dis- tance from c after each iteration of the outermost for loop. The evaluation order of edges is: (a, b), (a, c), (a, d), (b, a), (b, e), (c, a), (c, d), (c, e), (d, a), (d, e), (e, b). Show your work, including intermediate values of d within each column (cross out the values as they are replaced).

d[Node]: distance from c to Node after each iteration of the while loop

Node init. 1 2 3 4 5

a ∞

b ∞

c 0

d ∞

e ∞

b. Based on your answer in part (a), are there any negative cycles in this graph? Explain why or why not. If this cannot be determined, please say so.

c. Based on your answer in part (a), what is the cost of the shortest path from c to e? If this cannot be determined, please say so.

6 Q6. (6 points) Suppose there are two functions: findBestWidget(input), which finds the optimal widget configuration for a given input, and certifyBestWidget(input), which returns True if the input widget configuration is optimal, and False otherwise.

a. Suppose that findBestWidget(n) executes in Θ(2n) time, but certifyBestWidget(n) runs in Θ(n3) time. Based on this information, which class of problems does BestWidget selection belong to? Justify your answer.

b. Suppose that a new function findBestWidgetFast(n) is developed that executes in Θ(n4) time. Given this new development, which class of problems does BestWidget selection belong to now? Justify your answer.

7