Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

Students also viewed these Databases questions