<<

Name: 100 points total

CSE 3353 Homework 4 Spring 2013 Assignment is due at 9:30am on May 2. Submit a hard copy of the assignment, 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 spdag.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 spdag output.txt, also included in the zip file. Both of these files must be included in a directory called LastnameFirstname. Similar instructions apply for Q2, but the file must be called playlist.py and playlist output.txt. 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 for questions Q1a–c, Q3–Q9. 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 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 one week after the assignment is due.

Q1 (25)

Q2 (30)

Q3 (4)

Q4 (8)

Q5 (12)

Q6 (10)

Q7 (4)

Q8 (6)

Q9 (1)

Total (100)

1 Programming

Q1. (25 points) Consider the problem of finding the shortest path in a weighted, directed, acyclic graph.

a. (2 points) Draw the graph represented by the variable G1 in spdag starter.py: G1 ={ ’ a ’ : { ’b’:3, ’c’:4 } , ’ b ’ : { ’ c ’ : 7 } , ’ c ’ : { ’d’:9, ’f’:8 } , ’ d ’ : { ’ e ’ : 2 , ’ f ’ : −6}, ’ e ’ : { ’ f ’ : 3 } , ’ f ’ : {} }

b. (2 points) Using a greedy algorithm that always selects the shortest available edge, identify the path selected from a to f in G1. List the nodes in the order in which they are visited and report the total cost of the path.

c. (6 points) Following the “shortest path in a weighted DAG” algorithm given in Listing 8-4 on p. 183 of Python Algorithms, identify the shortest path from a to f. List the nodes in the order in which they are visited and report the total cost of the path. Show your work by completing the following table that indicates intermediate values of d after each iteration of the loop.

d[Node]: value after each iteration of the loop Node init. 1 (u=a) 2 (u=b) 3 (u=c) 4 (u=d) 5 (u=e) 6 (u=f) a 0 b ∞ c ∞ d ∞ e ∞ f ∞

2 Q1. (continued) d. (15 points) Consider function implemented in Listing 8-4 on p. 183 of Python Algorithms: def d ag s p (W, s , t ) : # Shortest path from s to t d = {u:float(’inf’) f o r u in W} # Distance estimates d [ s ] = 0 # Start node: Zero distance f o r u in topsort (W): # In top−sorted order... i f u == t : break # Have we arrived? f o r v in W[ u ] : # For each out−edge . . . d[v] = min(d[v], d[u] + W[u][v]) # Relax the edge return d [ t ] # Distance to t (from s)

This function returns the cost of the shortest path, but not the nodes along the shortest path. To obtain the shortest path, the code needs to be modified to keep track of node’s parents. Using the starter code from http://lyle.smu.edu/ ˜tylerm/courses/cse3353/code/spdag_starter.txt, modify the dag spp function to return the triple (min- imum distance, shortest path, parent dictionary). Implement the parent dictionary using the variable P and the shortest path using variable path. Please attach a printout of the code which executes the code inside the if name == ‘ main ’ branch. Q2. (30 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.

3 Dynamic Programming

Q3. (4 points) Consider the following code. def two pow ( i ) : i f i == 0 : return i 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?

4 Q4. (8 points) a. Calculate the edit distance between the two strings “coral” and “koraal” (the Dutch word for coral). Construct a cost table and use it to explain precisely how to turn the string “coral” into “koraal”.

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

5 Weighted Graph Algorithms

Q5. (12 points) Consider the following weighted undirected graph:

d

2 9 6 b f

7 3 8 c 13 7 5 a e 5

a. Construct a minimum spanning tree using Prim’s algorithm starting at node a. Number each edge by the order in which it is added to the tree. Show your work by including a priority queue with edges and their corresponding weights, crossing off edges as they are selected for the MST.

b. Construct a minimum spanning tree using Kruskal’s algorithm. Number each edge by the order in which it is added to the tree.

6 Q6. (10 points) Consider the following weighted directed graph:

3 a d 6 8 7 1 2

5 c 8 2 4 1 7 b e

Find the weight of the shortest path from a to all nodes in the graph using Dijkstra’s algorithm. Complete the table that indicates intermediate values of the upper bound d of the distance from a after each iteration of the while loop. Fill in the column heading with the node that is considered at each iteration. Show your work by including the contents of the priority queue. d[Node]: distance from a to Node after each iteration of the while loop Node init. 1 ( ) 2 ( ) 3 ( ) 4 ( ) 5 ( ) a 0 b ∞ c ∞ d ∞ e ∞

7 Intractability

Q7. (4 points) Consider the longest increasing subsequence problem. Briefly explain using pseudocode how this problem can be solved by reducing it to the edit distance problem.

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

a. Suppose that findBestWidget(n) executes in Θ(2n) time, but verifyBestWidget(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.

Q9. (1 point) How long (in hours) did you spend on this assignment? Please estimate separately how long you spent on programming (Q1d and Q2) and the other questions (Q1a–c,Q3–Q8) (full credit for any truthful answer)

8