Question: For this task, you need to apply the greedy algorithm defined to address the previous task. Below I provide a function testGreedy that displays the

For this task, you need to apply the greedy algorithm defined to address the previous task. Below I provide a function testGreedy that displays the process time in terms of nanoseconds when the greedy algorithm is applied to process Cow data in a given file with a specific weight limit and a chosen sorting method.
#Using Greedy
import time
def testGreedy(filename, constraint, method=1, toPrint=False):
items = load_cows(filename)
start = time.process_time_ns()
print("At the beginning of processing {}".format(filename))
print("Start time (in nanoseconds): {}
".format(start))
taken, val = greedy(items, constraint, method)
end = time.process_time_ns()
print("At the end of processing {}".format(filename))
print("End time (in nanoseconds): {}".format(end))
print("
Elapsed time to process {} in {} nanoseconds.".format(filename,end-start))
print('
Total value of items taken ={}'.format(val))
for item in taken:
if toPrint:
print('', item)
testGreedy("cows1.txt",100)
testGreedy("cows1.txt",100,2)
testGreedy("cows2.txt",100)
testGreedy("cows2.txt",100,2)
Based on the above applications of testGreedy function, you need to answer the following questions:
Compare the process time results by running the greedy algorithm based on sorted on the two data sets, describe the difference. Are you able to complete the sorting operation on the second data set?
Compare the process time results by running the greedy algorithm based on insertionSort on the two data sets, describe the difference. Are you able to complete the sorting operation on the second data set? (If you cannot get a result on the second data set, don't feel bad as it takes too long to get a result. I had to stop running the program without a result.)
Explain the processing-time differences to run your greedy algorithm on the two data sets.
Feel free to change the function call arguments when applying testGreedy.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!