35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
from typing import Any, Dict, List
|
|
from common.base_problem import BaseProblem
|
|
from problems.min_path_sum.generator import generate_min_path_sum_case
|
|
from problems.min_path_sum.solvers import solve_dp, solve_recursion
|
|
from common.timer_utils import time_execution
|
|
|
|
class MinPathSumProblem(BaseProblem):
|
|
def generate_case(self, rows: int = 5, cols: int = 5, min_val: int = 1, max_val: int = 20) -> Dict[str, Any]:
|
|
grid = generate_min_path_sum_case(rows, cols, min_val, max_val)
|
|
return {"grid": grid}
|
|
|
|
def solve(self, input_data: Dict[str, Any], algorithm: str) -> Dict[str, Any]:
|
|
grid = input_data.get("grid", [])
|
|
|
|
result = None
|
|
duration = 0.0
|
|
|
|
if algorithm == "dp":
|
|
result, duration = time_execution(solve_dp)(grid)
|
|
elif algorithm == "recursion":
|
|
result, duration = time_execution(solve_recursion)(grid)
|
|
else:
|
|
raise ValueError(f"Unknown algorithm: {algorithm}")
|
|
|
|
return {
|
|
"algorithm": algorithm,
|
|
"min_path_sum": result,
|
|
"time_seconds": duration
|
|
}
|
|
|
|
def verify(self, input_data: Any, result: Any) -> bool:
|
|
grid = input_data.get("grid", [])
|
|
truth, _ = time_execution(solve_dp)(grid)
|
|
return result.get("min_path_sum") == truth
|