cost <- matrix(c(4, 2, 5, 3, 3, 6, 7, 5, 4), nrow = 3, byrow = TRUE)
result <- assignment_duals(cost)
# Check optimality: u + v should equal cost for assigned pairs
for (i in 1:3) {
j <- result$match[i]
cat(sprintf("Row %d -> Col %d: u + v = %.2f, cost = %.2f\n",
i, j, result$u[i] + result$v[j], cost[i, j]))
}
# Verify strong duality
cat("sum(u) + sum(v) =", sum(result$u) + sum(result$v), "\n")
cat("total_cost =", result$total_cost, "\n")
# Reduced costs (how much must cost decrease to enter solution)
reduced <- outer(result$u, result$v, "+")
reduced_cost <- cost - reduced
print(round(reduced_cost, 2))
Run the code above in your browser using DataLab