Core Algorithm Overview Ryan Hildebrant C950: Data Structures & Algorithms II Student # XXXXXXXXXX Core Algorithm Overview Stated Problem: The purpose of this project is to determine the best route...

1 answer below »
I only need 1 or 2 sentences for the write-up part (A,B,C, etc.). I m mostly struggling with the coding part because I don't have a computer to download the IDE to do the project.I am also attaching an example of another student project found on Github. This project has to be free of plagiarism. The school has a program to run all the assigments and if it matches another student submission I will automatically fail.


Core Algorithm Overview Ryan Hildebrant C950: Data Structures & Algorithms II Student # 000709587 Core Algorithm Overview Stated Problem: The purpose of this project is to determine the best route and delivery distribution for the Western Governors University Parcel Service (WGUPS) using a common high-level programming language (Python 3.7). There are 40 packages that must be split up across two trucks with 3 different drivers. Some packages have delivery constraints, and some have been delayed. Additionally, there is a package that initially has the wrong address and packages that must go out on the second truck. To solve this problem, we must first use a series of conditional statements to load the trucks based on the data that is provided. We then must implement a greedy algorithm to optimize delivery of each package along the truck route. This algorithm is greedy because it determines the shortest available path from its current location then continues to do this until no additional packages remain. This article will analyze the use of this algorithm and provide a descriptive overview of the applications methods and components. Algorithm Overview: The greedy algorithm is executed by doing the following… 1) A list of packages on the truck is passed in along with the associated truck number and the current location (always at hub by default) 2) The current location is compared to the locations of all the packages in the truck to determine the closest location. 3) The lowest value is determined once all objects in the truck have been compared then that value is removed from the truck list and the truck moves to that address location 4) That value is appended to an optimized location list than the algorithm updates current location with the new address and calls the function again with the smaller truck list. The space-time complexity of this self-adjusting greedy algorithm has a worst case runtime O(N^2) and a best case runtime of O(1). The worst case is almost always guaranteed as the best case is only possible when the list of packages being loaded onto a truck is empty. A pseudocode representation is provided below to prove this… Greedy Algorithm A. Take the following parameters 1. truck_list (represents a nested list of packages on a given truck) Ryan Hildebrant C950: Data Structures & Algorithms II Student # 000709587 2. truck_number (represents the truck you are working with) 3. current_location (recursive variable that is used to show where the truck is) B. Create the base case to break the recursion in the algorithm (see to see recursive step) if length of truck_list is 0 then return the empty list C. Enter the recursive sections if the truck_list is not empty set lowest_value to 50.0 (represents the closest deliverable location from the current location in miles, ex: 3.2) set new_location to 0 (represents where the truck is moving next to deliver a package) if length of truck_list is not 0: for index in truck_list: if check_current_distance of current_location and the next index in the truck package list <= lowest_value: (searches all reachable locations and updates the lowest_value if a smaller mileage path is found). new lowest_value exists then update lowest_value and current_path the time complexity of the algorithm so far is as follows: a) o(1) or constant time b.) o(1) or constant time c.) o(n) o(1) + o(1) + o(n) = o(n) d. enter the second for loop in the recursive section of the algorithm for index in truck_list: if check_current_distance of current_location and the next index in the truck package list is equal to lowest_value: (search through the list of packages again to find the package address value that is equal to lowest_value (see section c). this allows us to deliver multiple packages to the same address if they are in the same truck) if truck_number is equal to 1, 2, or 3 (determines which truck object was originally passed in so it can create an optimized list for that truck) optimized_truck.append(current package) (append the selected package from truck_list to the optimized_truck list) optimized_truck_index.append(current package index) (append the selected package address index from truck_list to the optimized_truck index list) ryan hildebrant c950: data structures & algorithms ii student # 000709587 pop_value = truck_list.index(current package) truck_list.pop(pop_value) (remove the selected package from truck_list) current_location = new_location (update the current location to show the truck has moved to a new location) calculate_shortest_distance(truck_list, truck_number, current_location) calls algorithm function again to loop through the shorter truck_list with an updated location the total time complexity of the algorithm is as follows: a) o(1) or constant time b.) o(1) or constant time c.) o(n) d.) o(n^2) o(1) + o(1) + o(n) + o(n^2) = o(n^2) below is a table breakdown of the worst-case space-time complexity of each file in the python application: hashtable.py readcsv.py method line number space complexity time complexity __init__ 12 o(1) o(1) _get_hash 20 o(1) o(1) insert 26 o(n) o(n) update 42 o(1) o(n) get 55 o(n) o(n) delete 64 o(n) o(n) total 3n + 3 = o(n) 4n + 2 = o(n) method line number space complexity time complexity none 15 o(n) o(n) get_hash_map 60 o(1) o(1) check_first_truck_trip 65 o(1) o(1) ryan hildebrant c950: data structures & algorithms ii student # 000709587 packages.py main.py distances.py check_second_truck_trip 70 o(1) o(1) check_third_truck_trip 75 o(1) o(1) total n + 4 = o(n) n + 4 = o(n) method line number space complexity time complexity none 43 o(1) o(n) none 51 o(n^2) o(n^2) none 66 o(n) o(n) none 80 o(1) o(n) none 88 o(n^2) o(n^2) none 102 o(n) o(n) none 117 o(1) o(n) none 125 o(n^2) o(n^2) none 139 o(n) o(n) total_distance 153 o(1) o(1) total 3n^2 + 3n + 3 = o(n^2) 3n^2 + 6n + 1 = o(n^2) method line number space complexity time complexity none 15 o(1) o(n) none 24 o(n) o(n^2) total n + 1 = o(n) 2n = o(n^2) method line number space complexity time complexity ryan hildebrant c950: data structures & algorithms ii student # 000709587 memory and computational time remain nearly linear throughout the entire application. this allows the available set of inputs to scale without being overburdened by memory availability constraints. bandwidth is not a factor in the current implementation as the application is run and managed on a local machine that does not require network resources. advantages of chosen algorithm this greedy algorithm preforms all the required functions to meet the project constraints and delivers the packages within the range of 100 miles. the biggest strength of this algorithm is its ability to quickly find the optimal path for a given truck (defined above). another huge advantage of the greedy algorithm approach is that it can scale with any set of data and addresses provided to it. additionally, the user interface provides the ability for a user to see all package details and their delivery status at a given time. it can also search for an individual package based on its package id. another algorithmic approach i could have used to optimize the packages was a dynamic programming approach. “dynamic programming is a problem solving technique that splits a problem into smaller subproblems” (zybooks, 3.5). i use an exhaustive approach instead of check_distance 17 o(1) o(1) check_current_distance 27 o(1) o(1) check_time_first_truck 41 o(n) o(n) check_time_second_truck 53 o(n) o(n) check_time_third_truck 65 o(n) o(n) check_address 79 o(1) o(1) calculate_shortest_distance 110 o(n^2) o(n^2) first_optimized_truck_index 150 o(1) o(1) first_optimized_truck_list 155 o(1) o(1) second_optimized_truck_index 160 o(1) o(1) second_optimized_truck_list 164 o(1) o(1) third_optimized_truck_index 170 o(1) o(1) third_optimized_truck_list 174 o(1) o(1) total n^2 + 3n + 9 = o(n^2) n^2 + 3n + 9 = o(n^2) ryan hildebrant c950: data structures & algorithms ii student # 000709587 trying to break the program down into smaller functions. the advantage of using a dynamic approach is that i can store paths along a route and check if there is a faster way to get to a location by first traveling to another location. storing these different paths may have created a large space complexity but it could have yielded a shorter path. a second algorithm i could have implemented for the optimal route is a self-adjusting heuristic. this approach would start at the hub then determine the closest path to the hub. it would then determine all packages that need to be delivered to that truck and load them into the truck. from there it would start at the new location and determine the shortest path from that location. if a location was already visited by that truck than it would move on the to the next closest location until all 40 packages had been allocated to a truck and a path was set. my approach and the self-adjusting heuristic share a similar attribute in the sense that the shortest available path is always chosen. programming models: the programming model for this application is limited as it is currently hosted on a local machine. the application is written using python 3.7 and is executed in the pycharm ide. there is no communication protocol present as the application pulls data from csv files that are in the project folder on the local machine. to do this we use the csvreader function from the csv library in python. thus, data exchanges are limited to the interactions of the application and the local machine. additionally, there is no target host environment for the application as a network connection is not needed or established. in terms of information semantics, the current software requirements are organized in a functional manner. defining a set of interaction semantics to determine the flow of connection, data, and the resulting information would not be required with the application being hosted on a local machine. ability to adapt the core functions of the application are designed to be able to scale and addresses changes in the number of packages, the number of trucks, and the number of locations. minor changes are required to scale with any of these components. for example, another set of location csv files can be inputted into the application to calculate a new route and determine an optimal path. additional packages can be inserted and the program will determine where to place the packages. this approach also allows a great deal of control when implementing numerous sub- applications as the design allows the input set to change freely. a lowest_value:="" (searches="" all="" reachable="" locations="" and="" updates="" the="" lowest_value="" if="" a="" smaller="" mileage="" path="" is="" found).="" new="" lowest_value="" exists="" then="" update="" lowest_value="" and="" current_path="" the="" time="" complexity="" of="" the="" algorithm="" so="" far="" is="" as="" follows:="" a)="" o(1)="" or="" constant="" time="" b.)="" o(1)="" or="" constant="" time="" c.)="" o(n)="" o(1)="" +="" o(1)="" +="" o(n)="O(N)" d.="" enter="" the="" second="" for="" loop="" in="" the="" recursive="" section="" of="" the="" algorithm="" for="" index="" in="" truck_list:="" if="" check_current_distance="" of="" current_location="" and="" the="" next="" index="" in="" the="" truck="" package="" list="" is="" equal="" to="" lowest_value:="" (search="" through="" the="" list="" of="" packages="" again="" to="" find="" the="" package="" address="" value="" that="" is="" equal="" to="" lowest_value="" (see="" section="" c).="" this="" allows="" us="" to="" deliver="" multiple="" packages="" to="" the="" same="" address="" if="" they="" are="" in="" the="" same="" truck)="" if="" truck_number="" is="" equal="" to="" 1,="" 2,="" or="" 3="" (determines="" which="" truck="" object="" was="" originally="" passed="" in="" so="" it="" can="" create="" an="" optimized="" list="" for="" that="" truck)="" optimized_truck.append(current="" package)="" (append="" the="" selected="" package="" from="" truck_list="" to="" the="" optimized_truck="" list)="" optimized_truck_index.append(current="" package="" index)="" (append="" the="" selected="" package="" address="" index="" from="" truck_list="" to="" the="" optimized_truck="" index="" list)="" ryan="" hildebrant="" c950:="" data="" structures="" &="" algorithms="" ii="" student="" #="" 000709587="" pop_value="truck_list.index(current" package)="" truck_list.pop(pop_value)="" (remove="" the="" selected="" package="" from="" truck_list)="" current_location="new_location" (update="" the="" current="" location="" to="" show="" the="" truck="" has="" moved="" to="" a="" new="" location)="" calculate_shortest_distance(truck_list,="" truck_number,="" current_location)="" calls="" algorithm="" function="" again="" to="" loop="" through="" the="" shorter="" truck_list="" with="" an="" updated="" location="" the="" total="" time="" complexity="" of="" the="" algorithm="" is="" as="" follows:="" a)="" o(1)="" or="" constant="" time="" b.)="" o(1)="" or="" constant="" time="" c.)="" o(n)="" d.)="" o(n^2)="" o(1)="" +="" o(1)="" +="" o(n)="" +="" o(n^2)="O(N^2)" below="" is="" a="" table="" breakdown="" of="" the="" worst-case="" space-time="" complexity="" of="" each="" file="" in="" the="" python="" application:="" hashtable.py="" readcsv.py="" method="" line="" number="" space="" complexity="" time="" complexity="" __init__="" 12="" o(1)="" o(1)="" _get_hash="" 20="" o(1)="" o(1)="" insert="" 26="" o(n)="" o(n)="" update="" 42="" o(1)="" o(n)="" get="" 55="" o(n)="" o(n)="" delete="" 64="" o(n)="" o(n)="" total="" 3n="" +="" 3="O(N)" 4n="" +="" 2="O(N)" method="" line="" number="" space="" complexity="" time="" complexity="" none="" 15="" o(n)="" o(n)="" get_hash_map="" 60="" o(1)="" o(1)="" check_first_truck_trip="" 65="" o(1)="" o(1)="" ryan="" hildebrant="" c950:="" data="" structures="" &="" algorithms="" ii="" student="" #="" 000709587="" packages.py="" main.py="" distances.py="" check_second_truck_trip="" 70="" o(1)="" o(1)="" check_third_truck_trip="" 75="" o(1)="" o(1)="" total="" n="" +="" 4="O(N)" n="" +="" 4="O(N)" method="" line="" number="" space="" complexity="" time="" complexity="" none="" 43="" o(1)="" o(n)="" none="" 51="" o(n^2)="" o(n^2)="" none="" 66="" o(n)="" o(n)="" none="" 80="" o(1)="" o(n)="" none="" 88="" o(n^2)="" o(n^2)="" none="" 102="" o(n)="" o(n)="" none="" 117="" o(1)="" o(n)="" none="" 125="" o(n^2)="" o(n^2)="" none="" 139="" o(n)="" o(n)="" total_distance="" 153="" o(1)="" o(1)="" total="" 3n^2="" +="" 3n="" +="" 3="O(N^2)" 3n^2="" +="" 6n="" +="" 1="O(N^2)" method="" line="" number="" space="" complexity="" time="" complexity="" none="" 15="" o(1)="" o(n)="" none="" 24="" o(n)="" o(n^2)="" total="" n="" +="" 1="O(N)" 2n="O(N^2)" method="" line="" number="" space="" complexity="" time="" complexity="" ryan="" hildebrant="" c950:="" data="" structures="" &="" algorithms="" ii="" student="" #="" 000709587="" memory="" and="" computational="" time="" remain="" nearly="" linear="" throughout="" the="" entire="" application.="" this="" allows="" the="" available="" set="" of="" inputs="" to="" scale="" without="" being="" overburdened="" by="" memory="" availability="" constraints.="" bandwidth="" is="" not="" a="" factor="" in="" the="" current="" implementation="" as="" the="" application="" is="" run="" and="" managed="" on="" a="" local="" machine="" that="" does="" not="" require="" network="" resources.="" advantages="" of="" chosen="" algorithm="" this="" greedy="" algorithm="" preforms="" all="" the="" required="" functions="" to="" meet="" the="" project="" constraints="" and="" delivers="" the="" packages="" within="" the="" range="" of="" 100="" miles.="" the="" biggest="" strength="" of="" this="" algorithm="" is="" its="" ability="" to="" quickly="" find="" the="" optimal="" path="" for="" a="" given="" truck="" (defined="" above).="" another="" huge="" advantage="" of="" the="" greedy="" algorithm="" approach="" is="" that="" it="" can="" scale="" with="" any="" set="" of="" data="" and="" addresses="" provided="" to="" it.="" additionally,="" the="" user="" interface="" provides="" the="" ability="" for="" a="" user="" to="" see="" all="" package="" details="" and="" their="" delivery="" status="" at="" a="" given="" time.="" it="" can="" also="" search="" for="" an="" individual="" package="" based="" on="" its="" package="" id.="" another="" algorithmic="" approach="" i="" could="" have="" used="" to="" optimize="" the="" packages="" was="" a="" dynamic="" programming="" approach.="" “dynamic="" programming="" is="" a="" problem="" solving="" technique="" that="" splits="" a="" problem="" into="" smaller="" subproblems”="" (zybooks,="" 3.5).="" i="" use="" an="" exhaustive="" approach="" instead="" of="" check_distance="" 17="" o(1)="" o(1)="" check_current_distance="" 27="" o(1)="" o(1)="" check_time_first_truck="" 41="" o(n)="" o(n)="" check_time_second_truck="" 53="" o(n)="" o(n)="" check_time_third_truck="" 65="" o(n)="" o(n)="" check_address="" 79="" o(1)="" o(1)="" calculate_shortest_distance="" 110="" o(n^2)="" o(n^2)="" first_optimized_truck_index="" 150="" o(1)="" o(1)="" first_optimized_truck_list="" 155="" o(1)="" o(1)="" second_optimized_truck_index="" 160="" o(1)="" o(1)="" second_optimized_truck_list="" 164="" o(1)="" o(1)="" third_optimized_truck_index="" 170="" o(1)="" o(1)="" third_optimized_truck_list="" 174="" o(1)="" o(1)="" total="" n^2="" +="" 3n="" +="" 9="O(N^2)" n^2="" +="" 3n="" +="" 9="O(N^2)" ryan="" hildebrant="" c950:="" data="" structures="" &="" algorithms="" ii="" student="" #="" 000709587="" trying="" to="" break="" the="" program="" down="" into="" smaller="" functions.="" the="" advantage="" of="" using="" a="" dynamic="" approach="" is="" that="" i="" can="" store="" paths="" along="" a="" route="" and="" check="" if="" there="" is="" a="" faster="" way="" to="" get="" to="" a="" location="" by="" first="" traveling="" to="" another="" location.="" storing="" these="" different="" paths="" may="" have="" created="" a="" large="" space="" complexity="" but="" it="" could="" have="" yielded="" a="" shorter="" path.="" a="" second="" algorithm="" i="" could="" have="" implemented="" for="" the="" optimal="" route="" is="" a="" self-adjusting="" heuristic.="" this="" approach="" would="" start="" at="" the="" hub="" then="" determine="" the="" closest="" path="" to="" the="" hub.="" it="" would="" then="" determine="" all="" packages="" that="" need="" to="" be="" delivered="" to="" that="" truck="" and="" load="" them="" into="" the="" truck.="" from="" there="" it="" would="" start="" at="" the="" new="" location="" and="" determine="" the="" shortest="" path="" from="" that="" location.="" if="" a="" location="" was="" already="" visited="" by="" that="" truck="" than="" it="" would="" move="" on="" the="" to="" the="" next="" closest="" location="" until="" all="" 40="" packages="" had="" been="" allocated="" to="" a="" truck="" and="" a="" path="" was="" set.="" my="" approach="" and="" the="" self-adjusting="" heuristic="" share="" a="" similar="" attribute="" in="" the="" sense="" that="" the="" shortest="" available="" path="" is="" always="" chosen.="" programming="" models:="" the="" programming="" model="" for="" this="" application="" is="" limited="" as="" it="" is="" currently="" hosted="" on="" a="" local="" machine.="" the="" application="" is="" written="" using="" python="" 3.7="" and="" is="" executed="" in="" the="" pycharm="" ide.="" there="" is="" no="" communication="" protocol="" present="" as="" the="" application="" pulls="" data="" from="" csv="" files="" that="" are="" in="" the="" project="" folder="" on="" the="" local="" machine.="" to="" do="" this="" we="" use="" the="" csvreader="" function="" from="" the="" csv="" library="" in="" python.="" thus,="" data="" exchanges="" are="" limited="" to="" the="" interactions="" of="" the="" application="" and="" the="" local="" machine.="" additionally,="" there="" is="" no="" target="" host="" environment="" for="" the="" application="" as="" a="" network="" connection="" is="" not="" needed="" or="" established.="" in="" terms="" of="" information="" semantics,="" the="" current="" software="" requirements="" are="" organized="" in="" a="" functional="" manner.="" defining="" a="" set="" of="" interaction="" semantics="" to="" determine="" the="" flow="" of="" connection,="" data,="" and="" the="" resulting="" information="" would="" not="" be="" required="" with="" the="" application="" being="" hosted="" on="" a="" local="" machine.="" ability="" to="" adapt="" the="" core="" functions="" of="" the="" application="" are="" designed="" to="" be="" able="" to="" scale="" and="" addresses="" changes="" in="" the="" number="" of="" packages,="" the="" number="" of="" trucks,="" and="" the="" number="" of="" locations.="" minor="" changes="" are="" required="" to="" scale="" with="" any="" of="" these="" components.="" for="" example,="" another="" set="" of="" location="" csv="" files="" can="" be="" inputted="" into="" the="" application="" to="" calculate="" a="" new="" route="" and="" determine="" an="" optimal="" path.="" additional="" packages="" can="" be="" inserted="" and="" the="" program="" will="" determine="" where="" to="" place="" the="" packages.="" this="" approach="" also="" allows="" a="" great="" deal="" of="" control="" when="" implementing="" numerous="" sub-="" applications="" as="" the="" design="" allows="" the="" input="" set="" to="" change="" freely.="">
Answered 6 days AfterApr 28, 2021

Answer To: Core Algorithm Overview Ryan Hildebrant C950: Data Structures & Algorithms II Student # XXXXXXXXXX...

Sandeep Kumar answered on May 04 2021
156 Votes
New/DeliveryStatus.py
IN_ROUTE = "In route"
DELIVERED = "Delivered"
AT_HUB = "At hub"
New/DistanceData.py
import csv
import datetime
from truck import truck, TRUCK_SPEED
# Read the map of distances between locations
with open('WGUPS_DISTANCE_FILE.csv') as input_file:
distance_list = csv.reader(input_file, delimiter=',')
distance_list = list(distance_list)
#(distance_list)
# Read the csv of address locations
with open('WGUPS_LOCATION_FILE.csv') as input_file:
location_list = csv.reader(input_file, delimiter=',')
location_list = list(location_list)
#print(location_list)
#Computes distance from point A to point B
def point_distance(a, b):
d = distance_list[a][b]
#Try it transposed if we can't find it
if d is '':
d = distance_list[b][a]
return d
New/Distances.py
import datetime
from truck import truck, TRUCK_SPEED
from IngestData import dist
ances,locations
# a list of row/column values are inserted into this function. This function then calculates the total distance
# that distance is then returned and each iteration represents a distance between two locations
# Space-time complexity is O(1)
# Computes distance from point A to point B
def point_distance(a, b):
d = distances[a][b]
# Try it transposed if we can't find it
if d is '':
d = distances[b][a]
return float(d)
def check_distance(row_value, column_value, sum_of_distance):
distance = distances[row_value][column_value]
if distance is '':
distance = distances[column_value][row_value]
sum_of_distance += float(distance)
return sum_of_distance
# this function is very similar to the function above but returns a current distance
# Space-time complexity is O(1)
def check_current_distance(row_value, column_value):
distance = distances[row_value][column_value]
#Transpose the X and Y if we can't find it
if distance is '':
distance = distances[column_value][row_value]
return float(distance)
# this is the time that the first truck leaves the hub
first_time_list = ['8:00:00']
second_time_list = ['9:10:00'] #Don't let second truck leave until package is corrected
third_time_list = ['11:00:00']
# this function takes a distance then divides it by TRUCK_SPEED. It then uses divmod to display a time and appends 00
# this string that is a timestamp is then split and turned into a datetime timedelta object
# that object is then added to sum which represents total distance for a particular truck
# runtime of function is O(N)
def check_time_first_truck(distance):
new_time = distance / TRUCK_SPEED
distance_in_minutes = '{0:02.0f}:{1:02.0f}'.format(*divmod(new_time * 60, 60))
final_time = distance_in_minutes + ':00'
first_time_list.append(final_time)
sum = datetime.timedelta()
for i in first_time_list:
(h, m, s) = i.split(':')
d = datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
sum += d
return sum
# Repeated function for second truck
def check_time_second_truck(distance):
new_time = distance / TRUCK_SPEED
distance_in_minutes = '{0:02.0f}:{1:02.0f}'.format(*divmod(new_time * 60, 60))
final_time = distance_in_minutes + ':00'
second_time_list.append(final_time)
sum = datetime.timedelta()
for i in second_time_list:
(h, m, s) = i.split(':')
d = datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
sum += d
return sum
# Repeated function for the third truck
def check_time_third_truck(distance):
new_time = distance / TRUCK_SPEED
distance_in_minutes = '{0:02.0f}:{1:02.0f}'.format(*divmod(new_time * 60, 60))
final_time = distance_in_minutes + ':00'
third_time_list.append(final_time)
sum = datetime.timedelta()
for i in third_time_list:
(h, m, s) = i.split(':')
d = datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
sum += d
return sum
# this function returns the time objects to use in the Packages.py file
# Space-time complexity is O(1)
def check_address():
return locations
# these lists represent the sorted trucks that are put in order of efficiency in the function below
first_optimized_truck = []
first_optimized_truck_index_list = []
second_optimized_truck = []
second_optimized_truck_index_list = []
third_optimized_truck = []
third_optimized_truck_index_list = []
def calculate_shortest_distance(truck_distance_list, truck_number, current_location): # section 1
if len(truck_distance_list) == 0: # section 2
return truck_distance_list
else: #
try:
lowest_value = 50.0
new_location = 0
for index in truck_distance_list:
if check_current_distance(current_location, int(index[1])) <= lowest_value:
lowest_value = check_current_distance(current_location, int(index[1])) # section 3
new_location = int(index[1])
for index in truck_distance_list: # section 4
if check_current_distance(current_location, int(index[1])) == lowest_value:
if truck_number == 1:
first_optimized_truck.append(index)
first_optimized_truck_index_list.append(index[1])
pop_value = truck_distance_list.index(index)
truck_distance_list.pop(pop_value)
current_location = new_location
calculate_shortest_distance(truck_distance_list, 1, current_location)
elif truck_number == 2:
second_optimized_truck.append(index)
second_optimized_truck_index_list.append(index[1])
pop_value = truck_distance_list.index(index)
truck_distance_list.pop(pop_value)
current_location = new_location
calculate_shortest_distance(truck_distance_list, 2, current_location)
elif truck_number == 3:
third_optimized_truck.append(index)
third_optimized_truck_index_list.append(index[1])
pop_value = truck_distance_list.index(index)
truck_distance_list.pop(pop_value)
current_location = new_location
calculate_shortest_distance(truck_distance_list, 3, current_location)
except IndexError:
pass
first_optimized_truck_index_list.insert(0, '0')
# Space-time complexity is O(1)
def first_optimized_truck_index():
return first_optimized_truck_index_list
# Space-time complexity is O(1)
def first_optimized_truck_list():
return first_optimized_truck
second_optimized_truck_index_list.insert(0, '0')
# Space-time complexity is O(1)
def second_optimized_truck_index():
return second_optimized_truck_index_list
# Space-time complexity is O(1)
def second_optimized_truck_list():
return second_optimized_truck
third_optimized_truck_index_list.insert(0, '0')
# Space-time complexity is O(1)
def third_optimized_truck_index():
return third_optimized_truck_index_list
# Space-time complexity is O(1)
def third_optimized_truck_list():
return third_optimized_truck
New/HashTable.py
class HashTable:
# Just a convenience method to print out the whole table
def print(self):
print(*self.hashtable)
# Constructor
# space-time complexity O(1)
def __init__(self, size=40):
# initialize the hash table with empty bucket list entries.
self.hashtable = []
for kvp in range(size):
self.hashtable.append([])
# Getter to generate a key
# space-time complexity O(1)
def _get_hashkey(self, key):
return int(key) % len(self.hashtable)
# Inserts a new PackageEntry into the HashTable
# space-time complexity O(N)
def insert(self, key, value):
kv = [key, value]
hashkey = self._get_hashkey(key)
# Brand new insertion
if self.hashtable[hashkey] is None:
self.hashtable[hashkey] = kv
return True
else:
# Overwrite any previous value
for kvp in self.hashtable[hashkey]:
if kvp[0] == key:
kvp[1] = kv
return True
self.hashtable[hashkey].append(kv)
return True
# Remove a value from the hash table
# runtime is O(N)
def delete(self, key):
hashkey = self._get_hashkey(key)
# Nothing to delete
if self.hashtable[hashkey] is None:
return False
# Search the whole table for the key
for kvp in range(0, len(self.hashtable[hashkey])):
if self.hashtable[hashkey][kvp][0] == key:
self.hashtable[hashkey].pop(kvp)
return True
# Was none
return False
# Returns a value associated with a key
# space-time complexity O(N)
def get(self, key):
hashkey = self._get_hashkey(key)
if self.hashtable[hashkey] is not None:
for kvp in self.hashtable[hashkey]:
if kvp[0] == key:
return kvp[1]
# Couldn't find a value
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here