private Node root; //start node public BranchAndBoundSover(List<Item> items, int capacity) : base(items, capacity) { Node.Items = Items; Node.Capacity = Capacity; items.Sort(delegate (Item i1, Item i2) { return (i1.Value / i1.Weight).CompareTo(i2.Value / i2.Weight); }); items.Reverse(); addedItems = new int[this.Items.Count]; for (int i = 0; i < addedItems.Length; i++) { addedItems[i] = 1; } root = new Node(0, 0, 0, addedItems); branchingNodes = new List<Node>() { root }; }
private void Branch(Node node) { branchingNodes.Remove(node); Node nodeLeft = new Node(node.Index + 1, node.Value + Items[node.Index].Value, node.Weight + Items[node.Index].Weight, (int[])node.AddedItems.Clone()); int[] addedItemsClone = (int[])node.AddedItems.Clone(); addedItemsClone[node.Index] = 0; Node nodeRight = new Node(node.Index + 1, node.Value, node.Weight, addedItemsClone); if (nodeLeft.Weight <= this.Capacity) { branchingNodes.Add(nodeLeft); } if (nodeRight.Weight<=this.Capacity) { branchingNodes.Add(nodeRight); } }