Question: Write a function shortest _ path ( spatial _ network, source, target, max _ bound ) that takes a spatial network, initial ( source )

Write a function shortest_path(spatial_network, source, target, max_bound) that takes a spatial network, initial (source) location, target location and the maximum distance (that a trader located in the initial location can travel) as its input and returns a tuple with a shortest path and its total distance.
Input:
spatial_network: a list of tuples, where each tuple is of the form (location, neighbours) and neighbours is of the form [(neighbour1, distance1),(neighbour2, distance2),...]. This corresponds with the output of the function you wrote for Q1a.
source: the location label (string) of the initial location.
target: the location label (string) of the target location.
max_bound: an integer (or None) that specifies the maximum total distance that your trader can travel. If max_bound is None then always return the path with minimum distance.
Your function should return a tuple (path, total_distance), where path is a string of each location label in the path separated by a - hyphen character, and total_distance is the total of the distances along the path.
If there's two paths with the same minimum total distance, choose the path with more locations on it. If there's two paths with the same minimum total distance and they have the same number of locations on the path then choose alphanumerically smaller path string.
If there is no path with a total distance within the max_bound then your function should return (None, None).
You may assume:
Inputs are correctly formatted data structures and types.
Distances are non-negative integer values.
The network is connected, so a path always exists, although it may not have a total distance within the maximum bound.
shortest_path(spatial_network, '11','13',50)
('L1-L2-L3',30
> shortest_path(spatial_network, 'L1','13',0)
(None, None)
shortest_path(spatial_network, 'L1','L3',10)
(None, None)
shortest_path(spatial_network, 'L1','L3', None)
('L1-L2-L3',30)
 Write a function shortest_path(spatial_network, source, target, max_bound) that takes a spatial

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!