<<

Name: 100 points total

CSE 3353 Homework 3 Fall 2014 Assignment is due at 11am on October 23. Submit a scanned copy of the non-coding portion of the assignment to Scoryst. Turn in a copy of the code and outputs in a .zip file on Blackboard. The Python file for Q1 must be named countinvert.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 countinvert 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 for questions Q2–Q7. 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 HW2, you may work with a partner again, but it must be a different partner than for HW2. You may submit a lateness coupon request BEFORE the assignment is due by sending a private Piazza message 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 (50)

Q2 (10)

Q3 (8)

Q4 (8)

Q5 (12)

Q6 (10)

Q7 (2)

Total (100)

1 Q1. (50 points) Your task is to write code to solve the count inversions problem, as explained in Section 5.3 of Algorithm De- sign. The code should operate in Θ(n log n) time and be implemented using a divide-conquer-glue strategy. Down- load starter code from http://lyle.smu.edu/˜tylerm/courses/cse3353/code/countinvert_ starter.txt. Name the file countinvert.py and leave the function names unchanged. In particular, your code should print out the inverted elements as they are encountered. The function should execute successfully the code inside the if name ==¨ main ¨ clause. Include output from running the code in countinvert output.txt. The output should adhere to the following form:

$ python countinvert_sol.py [7, 10, 18, 3, 14, 17, 23, 2, 11, 16] 3 conflicts with 18 14 conflicts with 18 3 conflicts with 7, 10 2 conflicts with 17, 23 11 conflicts with 17, 23 16 conflicts with 17, 23 2 conflicts with 3, 7, 10, 14, 18 11 conflicts with 14, 18 16 conflicts with 18 17 conflicts with 18 # Inversions: 19 ... [(3, ’The Lumineers: Ho Hey’), (4, ’: Chasing Pavements’), (2, ’Jimi Hendrix: Voodoo Chile’), (1, "Stevie Ray Vaughan: Couldn’t Stand the Weather"), (8, ’: Clocks’), (6, ’Aretha Franklin: I Will Survive’), (5, ’Cake: I Will Survive’), (7, ’Beyonce: All the Single Ladies’), (9, ’Nickelback: Gotta be Somebody’), (10, ’Garth Brooks: Friends in Low Places’)] (1, "Stevie Ray Vaughan: Couldn’t Stand the Weather") conflicts with (2, ’Jimi Hendrix: Voodoo Chile’) (1, "Stevie Ray Vaughan: Couldn’t Stand the Weather") conflicts with (3, ’The Lumineers: Ho Hey’), (4, ’Adele: Chasing Pavements’) (2, ’Jimi Hendrix: Voodoo Chile’) conflicts with (3, ’The Lumineers: Ho Hey’), (4, ’Adele: Chasing Pavements’) (5, ’Cake: I Will Survive’) conflicts with (6, ’Aretha Franklin: I Will Survive’) (5, ’Cake: I Will Survive’) conflicts with (8, ’Coldplay: Clocks’) (6, ’Aretha Franklin: I Will Survive’) conflicts with (8, ’Coldplay: Clocks’) (7, ’Beyonce: All the Single Ladies’) conflicts with (8, ’Coldplay: Clocks’) # Inversions: 9

Please note: you may not consult online resources displaying Python code implementing a solution to the count inversions problem. Doing so will be considered cheating, and dealt with accordingly.

2 Q2. (10 points) Consider the following directed graph G and its transpose:

Graph G Transpose(G)

a b c d a b c d

h g f e h g f e

The topological sort of G is [g, a, b, c, d, f, e, h]. Using Kosaruju’s algorithm for finding strongly connected components, list the strongly connected components of G in the order in which they are discovered.

3 Q3. (8 points) def mySort(seq ): prev = seq[0] noconf = True f o r e l t in seq : i f e l t < prev : noconf = False break prev = e l t i f noconf : return seq f o r i in range ( l e n ( seq ) ) : j = i while j >0 and seq [ j ]< seq [ j −1]: seq[j],seq[j −1]= seq [ j −1] , seq [ j ] j =j −1 return seq

a. What is the best-case performance for mySort? Given an example input that attains the best-case performance.

b. What is the worst-case performance for mySort? Give an example input that attains the worst-case perfor- mance.

4 Q4. (8 points) a. Suppose you are using a Quicksort algorithm that always selects the first value in the list as pivot. Give an example input sequence that takes Θ(n2) time to complete.

b. Suppose quicksort could always pivoted on the median of the current sublist. How many comparisons would Quicksort make then in the worst case?

5 Q5. (12 points) Use the master theorem to give tight asymptotic bounds for the following recurrences:

n a. T (n) = 2T ( 2 ) + 7n

n 2 b. T (n) = 2T ( 2 ) + n

n c. T (n) = 3T ( 2 ) + 2n

n 2 d. T (n) = 7T ( 3 ) + 4n

6 Q6. (10 points) Consider the following recursive function. def myFun(seq ): i f l e n ( seq )==1: return seq [ 0 ] l e = l e n ( seq ) / 3 lo, mid, hi = seq[:le], seq[le:2 ∗ l e ] , seq [2∗ l e : ] plo = myFun(lo) pmid = myFun(mid) phi = myFun(hi) t o t a l =0 f o r x in plo+pmid+phi : f o r y in plo+pmid+phi : t o t a l +=x∗y return t o t a l

a. Write a recurrence relation for myFun.

b. Use the master theorem to obtain a tight asymptotic bound for myFun.

Q7. (2 points) How long (in hours) did you spend on this assignment? Please estimate separately how long you spent on programming (Q1) and the other questions (Q2–Q6) (full credit for any truthful answer)

7