javascript单线程使得一些计算变得不可能

时间:2014-03-21 08:37:02

标签: javascript multithreading

考虑到我正在尝试找algorithm找到least expansive path。 每个当前路径的计算都是async。 我的算法只会杀死堆栈。还有其他选择吗?

我编码的代码是:

function DFS(unvisitedNodes, currentVertex, currentRouteFromStart, depth, minimalPath,     finalVertex, maxDepth, doneCallback) {

// Add the final destination to the top of the list.
var copiedUnvisitedVertices = unvisitedNodes.slice(0);
copiedUnvisitedVertices.splice(0, 0, finalVertex);

var i = 0;
var singleIteration = function (i) {
    if (i == copiedUnvisitedVertices.length) {
        doneCallback(minimalPath);
        return;
    }

    if (i > 0) {
        // Putting the check here makes sure we compute the edge 
        // for the last depth for the destination vertex only.
        if (depth == maxDepth) {
            currentRouteFromStart.pop();
            doneCallback(minimalPath);
            return;
        }
    }

    var dest = copiedUnvisitedVertices[i];
    computeEdge(currentVertex, dest, function (edgeInfo) {
        currentRouteFromStart.push(edgeInfo);
        var currentCost = edgeCost(currentRouteFromStart);
        var afterBFSDone = function (_minimalPath) {
            currentRouteFromStart.pop();
            singleIteration(i + 1);
            return;
        }
        if (dest == finalVertex) {

            var minPrice = edgeCost(minimalPath);
            if (currentCost < minPrice) {
                minimalPath = currentRouteFromStart.slice(0);
            }
        }
        else {
            DFS(unvisitedNodes.slice(0).splice(i - 1), dest, currentRouteFromStart, depth + 1, minimalPath, finalVertex, maxDepth, afterBFSDone);
        }

        afterBFSDone(minimalPath);


    });
};
singleIteration(0);

}

0 个答案:

没有答案