如何找到这个算法的Big O,如果它是多项式,P = NP

时间:2015-12-09 22:22:41

标签: javascript algorithm np

我想找到这个算法是多项式的。这是分区问题的解决方案。您可能知道,如果是多项式,则P = NP。

算法是:

///// Partition Problem Algorithm

var set = [3, 1, 1, 2, 2, 1];
var setCopy = set;
var i = 0;
var sum = 0;
var setSum = 0;
var found = 0;
var sumNumbers = [];
var setLength = set.length;
var storeNumber = [];

while (found < 1) {
    var random = Math.floor(Math.random() * set.length); /// generate a random number
    sum += set[random]; /// take a random number from set
    if (sumNumbers == 0) { /// if doesn’t have a number in array
        sumNumbers += set[random]; /// stores the number without a comma
    } else {
        sumNumbers += [", " + set[random]]; /// stores the number with a comma
    }
    console.log(sumNumbers); /// show the number
    set.splice(random, 1); /// takes the number of set
    for (j = 0; j < set.length; j++) {
        setSum = setSum + set[j]; /// sum all numbers of set
    }
    if (sum == setSum) { /// if the sum of the numbers of a set is equal to the other
        console.log("Found!");
        console.log("First Set: " + sum, "Second Set: " + setSum);
        console.log(sumNumbers);
        console.log(set); /// show the set
        found++; /// count a point to the found
    } else {
        console.log("Not Found");
        console.log("First Set: " + sum, "Second Set: " + setSum);
        if (sum > setSum) {
            sum = 0;
            var set = [3, 1, 1, 2, 2, 1];
            setSum = 0;
            sumNumbers = 0;
            console.log("New attempt");
        }
    }
    var setSum = 0 /// turn the sum of all number of the set to zero, because of the while loop
}

为了确定确定性算法的算法,建议使用cobarzan:

    ///// Partition Problem Algorithm

var set = [3, 1, 1, 2, 2, 1];
var setCopy = set;
var i = 0;
var sum = 0;
var setSum = 0;
var found = 0;
var sumNumbers = [];
var setLength = set.length;
var storeNumber = [];

while (found < 1) {
    var takeOne = 0; /// take the first number
    sum += set[takeOne]; /// take a the number from set
    if (sumNumbers == 0) { /// if doesn’t have a number in array
        sumNumbers += set[takeOne]; /// stores the number without a comma
    } else {
        sumNumbers += [", " + set[takeOne]]; /// stores the number with a comma
    }
    console.log(sumNumbers); /// show the number
    set.splice(takeOne, 1); /// takes the number of set
    for (j = 0; j < set.length; j++) {
        setSum = setSum + set[j]; /// sum all numbers of set
    }
    if (sum == setSum) { /// if the sum of the numbers of a set is equal to the other
        console.log("Found!");
        console.log("First Set: " + sum, "Second Set: " + setSum);
        console.log(sumNumbers);
        console.log(set); /// show the set
        found++; /// count a point to the found
    } else {
        console.log("Not Found");
        console.log("First Set: " + sum, "Second Set: " + setSum);
        if (sum > setSum) {
            sum = 0;
            var set = [3, 1, 1, 2, 2, 1];
            setSum = 0;
            sumNumbers = 0;
            takeOne++
            console.log("New attempt");
            if (takeOne == set.length) {
                var takeOne += 1;
            }
        }
    }
    var setSum = 0 /// turn the sum of all number of the set to zero, because of the while loop
}

1 个答案:

答案 0 :(得分:4)

如果只有一个可能的分区,那么这种方法在找到之前将平均花费指数量的时间。所以它会有指数运行时间。

如果没有可能的分区,这种方法永远不会产生答案,因此您永远不会做出决定。因此,您没有一个程序可以解决在有限时间内是否存在这样的答案的决策问题。

实际上,伪随机数生成器的局限性是,如果对于一个体面大小的集合的解决方案很少,你将永远找不到答案,因为在开始循环之前你可以尝试很多东西。< / p>

因此,这比天真的指数暴力搜索更严重。