public DFSNode(DFSNode parent,double remainingWeight, double? plate, Dictionary<double, int> residualPlates) { this.parent = parent; this.depth = parent?.depth + 1 ?? 0; this.remainingWeight = remainingWeight - plate * 2 ?? remainingWeight; this.plate = plate; this.residualPlates = new Dictionary<double, int>(residualPlates); if (plate != null) this.residualPlates[plate.Value] -= 2; }
void Search() { var successors = Successors(); if (successors.Count() == 0) { // leaf if (this.remainingWeight < Optimum.remainingWeight || (this.remainingWeight == Optimum.remainingWeight && this.depth < Optimum.depth )) { Optimum = this; } else { // nothing to do, leaf is not a potential optimum solution } } else { foreach (var successor in successors) { successor.Search(); } } }
public static IEnumerable<double> InitiateDFS(double Weight, List<Plate> Plates) { AllPlates = new Dictionary<double, int>(); foreach (var plate in Plates) { AllPlates[plate.weight] = plate.unlimited? INFINITY : plate.amount; // for simplicity's sake if (plate.weight < 10) AllPlates[plate.weight] = Math.Min(AllPlates[plate.weight], SMALL_PLATE_BOUND); } var G = new DFSNode(null, Weight, null, AllPlates); Optimum = G; G.Search(); return Optimum.UsedPlates.OrderByDescending(w => w); }