0

我正在寻找在java中模拟一个名为x的poset,其大小为2n,形式为a_1 < a_2 < ... < a_n,并且b_1 < b_2 < ... < b_n。

我想运行一次迭代以获得随机线性扩展,从而比较每个对象的“大小”,如果可以切换两个相邻的位置,我会这样做,如果不能,那么我坚持,以新的顺序结束。例如 x[i] = a_k 和 x[i+1] = b_k 我切换它们,但是如果 x[i] = a_k 和 x[i+1] = a_(k+1) 我不会。(本质上这是 Karzanov Khachiyan 链)。

我最初想使用一个数组数组,其中 x[][] = {a_1[], ..., b_1[], ... },比如 a_1 = {a,1},我可以在其中比较值并轻松进行切换。现在我正在尝试考虑其他方法来做到这一点,因为从我之前的问题中我可以看出这不会是一种特别有效或优雅的方式。有没有人有什么建议?

4

1 回答 1

3

Well first, I like your idea of storing the whole chain in an array. I think that will work fine.

But I agree with you that the "nested" array,

{ {'a', 1}, {'a', 2}, ... }

will probably be a little annoying. Have you thought of making a class like

class Elem {
    String symbol;
    int subscript;
}

You could then write a comparator to say whether one Elem is less than another:

Comparator<Elem> comp = new Comparator<Elem>() {
    public int compareTo(Elem e1, Elem e2) {
        // return -1 if e1<e2, +1 if e2<e1, 0 otherwise
    }
    public boolean equals(Elem e1, Elem e2) {
        // ...
    }
};

I think that might make your life easier because then one element feels more like a single object.

于 2011-06-25T16:27:22.363 回答