# Matrix input
cost <- matrix(c(4, 2, 5, 3, 3, 6, 7, 5, 4), nrow = 3)
lap_solve(cost)
# Data frame input
library(dplyr)
df <- tibble(
source = rep(1:3, each = 3),
target = rep(1:3, times = 3),
cost = c(4, 2, 5, 3, 3, 6, 7, 5, 4)
)
lap_solve(df, source, target, cost)
# With NA masking (forbidden assignments)
cost[1, 3] <- NA
lap_solve(cost)
# Grouped data frames
df <- tibble(
sim = rep(1:2, each = 9),
source = rep(1:3, times = 6),
target = rep(1:3, each = 3, times = 2),
cost = runif(18, 1, 10)
)
df |> group_by(sim) |> lap_solve(source, target, cost)
Run the code above in your browser using DataLab