如何修改递归分支和绑定树节点遍历以避免堆栈溢出?

时间:2017-01-19 07:25:28

标签: swift recursion

我使用深度优先分支和绑定创建了以下Swift 3程序来解决背包问题。不幸的是,它是递归的。它适用于多达20项。但是对于更多数量的项目(30或更高),它将导致堆栈溢出。 (~5400级深)。

如何将其更改为非递归版本?

public enum BBState {
case created
case active
case death
}

public enum Direction {
case left
case right
}

public class BBTreeNode {
    public var artifact: Artifact
    public var nodeState: BBState
    public var level: Int
    public var path = [Direction]()
    public var weightSoFar: Int = 0
    public var optimumTotalValue: Int = 0
    public var estimatedOptTotalValue: Double = 0
    public weak  var parentNode: BBTreeNode?
    public var leftNode: BBTreeNode?
    public var rightNode: BBTreeNode?
    static var currentCompletedOptimumValues: Int = 0
    static var currentCompletedEstimatedOptimumValues: Double = 0
    static var currentOptimumPath = [Direction]()

// Initialization
public convenience init(artifact: Artifact) {
    self.init(artifact: artifact, level: 0, path: [], parent: nil, left: nil, right: nil)
}

public init(artifact: Artifact, level: Int, path: [Direction], parent: BBTreeNode?, left: BBTreeNode?, right: BBTreeNode?) {
    self.artifact = artifact
    self.nodeState = .created
    self.level = level
    self.path = path
    self.parentNode = parent
    self.leftNode = left
    self.rightNode = right
}


// BBTree
private func createRootAndInitiateBB(artifacts: [Artifact]) {
    // return the optimum value from Depth-First branch and bound
    let maxLevel = artifacts.count - 1
    print("Max Level: \(maxLevel)")
    // create dummy Root to start
    let root = BBTreeNode(artifact: Artifact(value: 0, weight: 0))
    root.estimatedOptTotalValue = Knapsack.calculateEstimatedOptimumValue(availableArtifacts: artifacts)
    root.nodeState = .active
    root.weightSoFar = Knapsack.capacity
    // loop here for each artifact# - selected(left) / not(right) but going recursive to left until
    // we have death node, then backtrack

    func depthFirstTraversal(bbNode: BBTreeNode?, level: Int) {
        guard let bbNode = bbNode  // not nil
            else { return }

        guard level <= maxLevel
            else { return }

        print("Currently on path: \(bbNode.path)")

        // switch to active is last state was created, else ignore
        bbNode.nodeState =  bbNode.nodeState == .created ?  .active : bbNode.nodeState

        // stop traverse down and traceback if calculated optimumValue < currentCompletedOptimumValue,
        // or current weightSoFar is 0 or less than 0
        // move up to parent
        if bbNode.estimatedOptTotalValue < BBTreeNode.currentCompletedEstimatedOptimumValues ||
            bbNode.weightSoFar < 0 {
            print("Case for estimatedOptTotalValue: \(bbNode.estimatedOptTotalValue) < currentCompletedEstimatedOptimumValues: \(BBTreeNode.currentCompletedEstimatedOptimumValues)")
            print("Or weight: \(bbNode.weightSoFar) < 0" )
            bbNode.nodeState = .death
            // remove references to children
            bbNode.leftNode = nil
            bbNode.rightNode = nil
            depthFirstTraversal(bbNode: bbNode.parentNode, level: bbNode.level - 1)
        } else if (bbNode.leftNode?.nodeState == .death &&
                bbNode.rightNode?.nodeState == .death) || level ==  maxLevel  {

            print("Case for no more path available. Need to backtrack. ")
            print("Current level: \(level)")
            // stop, record and traceback if at maxLevel or when both children are death
            if level == maxLevel && bbNode.estimatedOptTotalValue > BBTreeNode.currentCompletedEstimatedOptimumValues {
                BBTreeNode.currentCompletedEstimatedOptimumValues = bbNode.estimatedOptTotalValue
                BBTreeNode.currentCompletedOptimumValues = bbNode.optimumTotalValue
                BBTreeNode.currentOptimumPath = bbNode.path
                print("blah...")
                print("Candidate for optimum: \(bbNode.path)")
                print("Completed optimum path: \(BBTreeNode.currentCompletedOptimumValues)")
                print("Estimated optimum value: \(BBTreeNode.currentCompletedEstimatedOptimumValues)")
            }
            bbNode.nodeState = .death
            // remove references to children
            bbNode.leftNode = nil
            bbNode.rightNode = nil
            let _ = path.popLast()
            depthFirstTraversal(bbNode: bbNode.parentNode, level: bbNode.level - 1)
        } else if bbNode.leftNode == nil {
            // create left child
            print("create left child node")
            let childLeftNode = createBBNode(leftChild: true, parent: bbNode, path: bbNode.path + [.left])
            bbNode.leftNode = childLeftNode
            depthFirstTraversal(bbNode: childLeftNode, level: childLeftNode.level)
        } else if bbNode.rightNode == nil {
            // create right child
            print("create right child node")
            let childRightNode = createBBNode(leftChild: false, parent: bbNode, path: bbNode.path + [.right])
            bbNode.rightNode = childRightNode
            depthFirstTraversal(bbNode: childRightNode, level: childRightNode.level)
        }

    }

    func createBBNode(leftChild: Bool, parent: BBTreeNode, path: [Direction]) -> BBTreeNode {
        let level = parent.level + 1
        let artifact = artifacts[level]
        let newBBNode = BBTreeNode(artifact: artifact, level: level, path: path, parent: parent, left: nil, right: nil )
        if leftChild {
            newBBNode.optimumTotalValue =  parent.optimumTotalValue + artifact.value
            newBBNode.estimatedOptTotalValue = parent.estimatedOptTotalValue
            newBBNode.weightSoFar = parent.weightSoFar - artifact.weight
        } else {
            // right direction, we don't pick this item
            // Artifact is a struct,  artifacts is array of Artifact, so we don't need to write deep copy 
            var artifactsWithItemsRemoved = artifacts

            print("Current artifacts before any removal: \(artifactsWithItemsRemoved)")
            print("for path \(newBBNode.path) we are going to remove artifacts...")
            // first remove the dummy artifact 
            artifactsWithItemsRemoved.remove(at: 0)
            // now the real artifacts
            var indexOfItemForRemoval = [Int]()
            for (index,direction) in path.enumerated() {
                if direction == .right {
                    indexOfItemForRemoval.append(index)
                }
            }
            // actual removal, need to reverse the array index to avoid out of range index
            for idx in indexOfItemForRemoval.reversed() {
                artifactsWithItemsRemoved.remove(at: idx)
            }

            print("Artifacts with items removed: \(artifactsWithItemsRemoved)")

            newBBNode.optimumTotalValue = parent.optimumTotalValue
            newBBNode.estimatedOptTotalValue = Knapsack.calculateEstimatedOptimumValue(availableArtifacts: artifactsWithItemsRemoved)
            print("New estimatedOptValue for \(newBBNode.path) is \(newBBNode.estimatedOptTotalValue)")
            // no weight deduction if we don't pick
            newBBNode.weightSoFar = parent.weightSoFar
        }
        return newBBNode
    }

    depthFirstTraversal(bbNode: root, level: 0)
}

public func findOptimumValueAndPath(artifacts: [Artifact]) -> (Int,[Direction], Double) {
    createRootAndInitiateBB(artifacts: artifacts)
    return (BBTreeNode.currentCompletedOptimumValues, BBTreeNode.currentOptimumPath, BBTreeNode.currentCompletedEstimatedOptimumValues)
}

}

====更新===

我设法将递归的深度限制为某个限制,如4000,一旦计数器达到限制,我返回调用者的currentNode信息。然后拨打&#39; depthFirstTraversal&#39;再次使用currentNode但使用新的新堆栈。

以下是我的表现方式:

  1. 将createRootAndInitiateBB和findOptimumValueAndPath移动到调用者。
  2. 更新depthFirstTraversal以获得如下函数签名:

    static func depthFirstTraversal(bbNode:BBTreeNode,level:Int,recursiveCount:Int,complete completeFlag:inout Bool,currentProcessedNode:inout BBTreeNode)

  3. 其他一些重构工作是为了保持计数器数量并修复一些错误。 (例如,parentNode不应该是弱的,否则在返回调用者之后,currentNode的父节点变为nil并且我们失去了回溯到更高节点的能力)。

1 个答案:

答案 0 :(得分:1)