Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find total cost for completing all shipments in python
We need to find the total cost for completing all shipments between ports, where the cost is the shortest path distance between each pair of ports. This problem uses the Floyd-Warshall algorithm to find all shortest paths in a graph.
Problem Understanding
Given a list of ports where ports[i] represents connected ports from port i, and shipments as pairs [i, j], we need to find the minimum total cost. Each edge has weight 1, so the cost is the shortest path length.
For the example: ports = [[1, 4],[2],[3],[0, 1]], shipments = [[1, 4]], the path from port 1 to port 4 is: 1 ? 2 ? 3 ? 0 ? 4, giving a cost of 4.
Algorithm: Floyd-Warshall
The Floyd-Warshall algorithm finds shortest paths between all pairs of vertices in O(n³) time ?
Implementation
class Solution:
def solve(self, ports, shipments):
n = len(ports)
INF = 10 ** 10
# Initialize distance matrix
dist = [[INF for _ in range(n)] for _ in range(n)]
# Distance from a port to itself is 0
for i in range(n):
dist[i][i] = 0
# Set direct connections with distance 1
for i in range(n):
for j in ports[i]:
dist[i][j] = 1
# Floyd-Warshall algorithm
for k in range(n):
for i in range(n):
for j in range(n):
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
# Calculate total cost for all shipments
return sum(dist[i][j] for i, j in shipments if dist[i][j] != INF)
# Example usage
ob = Solution()
ports = [[1, 4], [2], [3], [0, 1], []]
shipments = [[1, 4]]
result = ob.solve(ports, shipments)
print(f"Total cost: {result}")
Total cost: 4
How It Works
- Initialize: Create a distance matrix with infinity for all pairs except diagonal (0)
- Direct connections: Set distance 1 for directly connected ports
- Floyd-Warshall: For each intermediate vertex k, update shortest paths through k
- Calculate cost: Sum the shortest distances for all required shipments
Example with Multiple Shipments
class Solution:
def solve(self, ports, shipments):
n = len(ports)
INF = 10 ** 10
dist = [[INF for _ in range(n)] for _ in range(n)]
for i in range(n):
dist[i][i] = 0
for i in range(n):
for j in ports[i]:
dist[i][j] = 1
for k in range(n):
for i in range(n):
for j in range(n):
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
return sum(dist[i][j] for i, j in shipments if dist[i][j] != INF)
# Test with multiple shipments
ob = Solution()
ports = [[1, 2], [2], [0], [1]]
shipments = [[0, 2], [1, 0], [3, 2]]
result = ob.solve(ports, shipments)
print(f"Total cost for multiple shipments: {result}")
Total cost for multiple shipments: 4
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(n³) | Floyd-Warshall triple nested loop |
| Space | O(n²) | Distance matrix storage |
Conclusion
The Floyd-Warshall algorithm efficiently finds all shortest paths in the port network. We calculate the total shipment cost by summing the shortest distances between required port pairs.
