Professional Documents
Culture Documents
Full S. R. No:
Instructions:
b) Your friend replaces j < n with j > i && j < n on Line 4. Clearly explain whether
this change makes the function better (e.g., it fixes the error, or it improves the function in
some other way) or worse (e.g., it creates a new error/warning).
c) What is the worst-case time complexity of the original and modified max_profit()
function in terms of the array length n? Justify your answers.
d) Rewrite the max_profit() function correctly, making as few changes as possible. For
each change, write a short comment explaining your reason for the change.
To verify this, run Dijkstra’s algorithm on the above graph, with vertex a as the source and
identify exactly where and why the algorithm fails. For every vertex, process its neighbours in
alphabetic order. For example, process vertex c’s neighbours in the order: a, b, d, e.
Question 5 (10 marks): Consider the following variation of the job scheduling problem where
there are 𝑛 jobs. We can only work on one job at a time, and we can start a new job immediately
after finishing the previous job. Each job 𝑗 takes time 𝑡𝑗 > 0 and has a value 𝑣𝑗 > 0. We can
start job 𝑗 at any time 𝑡, and we will finish it at time 𝑡 + 𝑡𝑗 (since it takes time 𝑡𝑗 to complete).
Given the information (𝑡𝑗 , 𝑣𝑗 ) for each of the 𝑛 jobs, we want an efficient algorithm to find the
maximum value that can be earned doing jobs whose total time is ≤ 𝑇.
Show that the following “most-value-per-unit-time job first” greedy strategy does not work:
v. Initialize 𝑡 = 0 and value = 0
vi. Sort jobs in descending order of value-per-unit-time 𝑣𝑗 /𝑡𝑗
vii. For each job 𝑗 in this order:
If 𝑡 + 𝑡𝑗 ≤ 𝑇 then:
Update 𝑡 = 𝑡 + 𝑡𝑗 // do job 𝑗
Update value = value + 𝑣𝑗
viii. Return value
Your answer must be a counter-example which provides the following information:
• The number of jobs 𝑛 (a positive integer)
• For each job 𝑗, the time to do the job 𝑡𝑗 and its value 𝑣𝑗 (both must be positive real numbers)
• The correct answer (along with an explanation)
• The incorrect answer returned by the above algorithm (trace the execution of the algorithm
on your counter-example)