开发者

Using a queue to solve TSP (Branch and Bound)

I'm working on a Branch and Bound algorithm for the Traveling Salesman Problem and I've run into a little hitch. I'm using a pretty standard Queue with Nodes representing subsets of vertices (paths). I'm pretty sure I have the whole thing worked out, but at the moment I have the public class Queue, and underneath that the private class Node, with all of its properties: the current path, the lower bound, etc.

However, in my main program, I initialize the Queue of nodes and create the two starting nodes, but I get the error "Node cannot be resolved to a type". I assumed this was because it was within the Queue class and not accessible to the main program, but when I move it out, I get errors everywhere else on items that are connected to the node.

I really hope this makes sense, I'm not sure how else to explain it, but everything else seems to be fine. Here's a snippet of my code for clarification:

`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

    }`

That is where I declare the Node class, and this is where I actually use i开发者_开发知识库t in my main program:

`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();`


In your main, change Node to Queue.Node :

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

I'd suggest putting the Node class in a seperate file from Queue since you apparently need to manipulate instances of it from outside the Queue class.


I think your Node class should be static, and not an inner class of Queue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜