二进制搜索期间的堆栈溢出

时间:2018-01-10 01:21:03

标签: java stack-overflow bisection

我正在使用二分搜索来找到行星之间的平衡点。方法binaryBalance采用行星的Arraylist,它是具有位移和质量属性的对象。它还考虑了两个行星的位移,在这两个行星之间我试图找到一个平衡点。双x是搜索的初始起点,我在这里设置p1和p2的平均位移。代码运行顺利,但它的答案是一分钟。我尝试通过将错误间隔设置为大于1e-10来提高精度,但我不断收到Stack Overflow错误。如何以更高的精度解决这个问题?

import java.util.*;
import java.lang.*;

public class Solution {
public static void main(String[] arg) {
    Scanner sc = new Scanner(System.in);
    int numCase = sc.nextInt();

    for (int k = 1; k <= numCase; k++) {

        //Initializing Space...
        int numPlanets = sc.nextInt();
        ArrayList<Planet> space = new ArrayList<>();
        int[] weights = new int[numPlanets];
        int[] displacements = new int[numPlanets];

        for (int i = 0; i < numPlanets; i++) {
            displacements[i] = sc.nextInt();
        }
        for (int i = 0; i < numPlanets;i++) {
            weights[i] = sc.nextInt();
        }
        for (int i = 0; i < numPlanets;i++) {
            Planet p = new Planet(displacements[i],weights[i]);
            space.add(p);
        }

        System.out.print("#" + k + " ");
        for (int i = 0; i < numPlanets-1; i++) {
            double init = (double) (space.get(i).getDisplacement() + space.get(i+1).getDisplacement()) /2;
            binaryBalance(space,space.get(i).getDisplacement(),space.get(i+1).getDisplacement(),init);
        }
        System.out.println();
    }
}

public static class Planet {
    private int d;
    private int m;

    public Planet(int d,int m) {
        this.d = d;
        this.m = m;
    }

    public void setDisplacement(int d) {
        this.d = d;
    }

    public void setMass(int m) {
        this.m = m;
    }

    public double getG(double dPlanet) {
        double betweenDistance = this.d - dPlanet;
        return this.m/(betweenDistance*betweenDistance);
    }

    public int getDisplacement() {
        return d;
    }
    public int getMass() {
        return m;
    }
}

public static void binaryBalance(ArrayList<Planet> space, double p1, double p2, double x) {
    double leftg = 0;
    double rightg = 0;
    for (int i = 0; i < space.size(); i++) {
        if (space.get(i).getDisplacement() < x) {
            leftg = leftg + space.get(i).getG(x);
        } else {
            rightg = rightg + space.get(i).getG(x);
        }
    }

    if (Math.abs(leftg - rightg) < 1e-10) {
        System.out.print(String.format("%.10f",x) + " ");
        return;
    }
    if (leftg < rightg) {
        binaryBalance(space, p1, x, (p1 + x) / 2);
    } else {
        binaryBalance(space, x, p2, (p2 + x) / 2);
    }
}

测试案例是:

10
2
1 2 1 1
2
1 2 1 1000
2
457 468 333 321
3
1 2 3 1 2 1
4
2 3 5 7 3 2 7 5
5
3 11 12 19 29 542 661 450 521 366   
6
42 75 88 94 113 144 669 551 355 344 294 155
7
62 86 279 323 363 516 579 810 749 736 297 136 107 52
8
10 34 64 73 93 97 101 122 466 463 441 373 315 292 225 83
10
9 14 38 39 48 73 179 190 207 302 560 497 640 722 437 259 449 470 709 520

预期答案是:

#1 1.5000000000
#2 1.0306534300
#3 462.5504629633
#4 1.4060952085 2.5939047915
#5 2.5328594461 3.7271944335 6.0999536409
#6 6.3428568767 11.5477377494 15.9641592998 24.9267991615
#7 57.8805685415 81.8651598883 91.0573691382 105.0835650491 133.2934094881
#8 74.2211477711 190.6837563313 305.8269181686 348.3304429927 470.2694219293 555.4943093854
#9 21.5171374463 47.9890597763 68.6536668433 82.9131954023 95.0052272762 99.1999097770 116.4978330953
#10 11.5573600056 24.0238341337 38.4847676134 44.6137453708 64.7500445424 126.9908128982 184.3221650927 197.9760596291 266.0574653677

2 个答案:

答案 0 :(得分:0)

尝试使用迭代而不是递归。我还在每次迭代中添加了记录数据的行。

public static void binaryBalance(ArrayList<Planet> space, double p1, double p2, double x) {
    while(true) {

        //You can use this line to log evolution of your data
        System.out.println(String.format("p1=%s p2=%s x=%s", p1, p2, x));

        double leftg = 0;
        double rightg = 0;
        for (int i = 0; i < space.size(); i++) {
            if (space.get(i).getDisplacement() < x) {
                leftg = leftg + space.get(i).getG(x);
            } else {
                rightg = rightg + space.get(i).getG(x);
            }
        }

        if (Math.abs(leftg - rightg) < 1e-10) {
            System.out.print(String.format("%.10f",x) + " ");
            return;
        }

        double p1Tmp = p1;
        double p2Tmp = p2;
        double xTmp = x;
        if (leftg < rightg) {
            p1 = p1Tmp;
            p2 = xTmp;
            x = (p1Tmp + xTmp) / 2;
        } else {
            p1 = xTmp;
            p2 = p2Tmp;
            x = (p2Tmp + xTmp) / 2;
        }
    }
}

答案 1 :(得分:0)

{1}}容差为{1} - 10,最大迭代次数为47次,第二种情况下质量差异很大。这不会溢出任何堆栈,但当然你问的是提高准确性。不幸的是,在案例6中甚至无法达到1e-11,因为所涉及的数字的规模(正如我在评论中提到的)。因此,如果更改公差指数,则会得到无限递归。

但也许固定的平衡容差不是你期望为这个练习做的事情!如果我改进直到间隔为“零”宽度,我得到完全预期的答案(达到给定的精度)。 (leftg-rightgp1不需要相等,但它们之间没有浮点数。通过注意到p2; {{1将以二进制中的0结尾。)对于这些情况,这最多需要53个二等分:任何数值分析师应该熟悉的数字,因为它是{{1}的有效位数中的有效位数。 }。我没有检查更大的x==p1 || x==p2容差是否可以给出正确的答案。

由于获得(高于)53级以上是无用的,因此这里的递归选择是无害的(虽然使用x函数的尾递归看起来很奇怪),并且改为迭代将无济于事。无论哪种方式,你必须确保它终止!