Java Poset simulation

橙三吉。 提交于 2020-01-07 02:01:55

问题


I'm looking to simulate a poset, called x say, of size 2n in java of the form a_1 < a_2 < ... < a_n, and b_1 < b_2 < ... < b_n.

I want to run an iteration to get a random linear extension whereby I compare the 'size' of each object and if two adjacent positions can be switched I do so, if not then I stick, to end with a new order. For example x[i] = a_k and x[i+1] = b_k I switch them, however if x[i] = a_k and x[i+1] = a_(k+1) I would not. (In essence this is the Karzanov Khachiyan chain).

I initially thought of using an array of arrays where x[][] = {a_1[], ..., b_1[], ... }, where say a_1 = {a,1}, where I could compare the values and easily make a switch. Now I am trying to think of other ways to do this, as from my previous quesiton I can see this isn't going to be a partiularly efficient or elegant way of doing this. Does anyone have any suggestions?


回答1:


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.



来源:https://stackoverflow.com/questions/6478384/java-poset-simulation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!