使用队列解决TSP(分支和绑定)

时间:2011-05-11 14:10:02

标签: java algorithm traveling-salesman branch-and-bound

我正在研究旅行商问题的分支定界算法,但我遇到了一些麻烦。我正在使用一个非常标准的队列,节点代表顶点(路径)的子集。我很确定我已经完成了整个过程,但是目前我有公共类Queue,并且在私有类Node下面,它具有所有属性:当前路径,下限等等。

但是,在我的主程序中,我初始化节点的队列并创建两个起始节点,但是我收到错误“节点无法解析为类型”。我假设这是因为它在Queue类中并且主程序无法访问,但是当我将其移出时,我在连接到节点的项目上的其他地方都会出错。

我真的希望这是有道理的,我不确定如何解释它,但其他一切似乎都很好。以下是我的代码片段以供澄清:

`public class Queue<Item> implements Iterable<Item> {
    private int N;         // number of elements on queue
    private Node first;    // beginning of queue
    private Node last;     // end of queue

    // helper linked list class
    public class Node {
        public int level;       //level on the tree
        public int[] path;      //current path
        public int bound;       //lower bound
        public Item item;       
        public Node next;       //the next in the path
        public boolean inPath;  //checks to see if the vertex is in the current path
        public int missingV;    //the vertex that is missing from the path

    }`

这就是我声明Node类的地方,这是我在主程序中实际使用它的地方:

`public static void main(String args[]) {
    int n = 4;
    int[][] W = new int[][] {{0,2,4,7},{2,0,7,3},{4,7,0,5},{6,3,5,0}};
    int[] opttour;
    Queue<Node> PQ = new Queue<Node>();
    Node u = new Node();
    Node v = new Node();`

2 个答案:

答案 0 :(得分:0)

在您的主要内容中,将Node更改为Queue.Node

    Queue<Queue.Node> PQ = new Queue<Queue.Node>();
    Queue.Node u = PQ.new Node();
    Queue.Node v = PQ.new Node();

我建议将Node类放在Queue的单独文件中,因为您显然需要在Queue类之外操作它的实例。

答案 1 :(得分:0)

我认为你的Node类应该是静态的,而不是Queue的内部类。