-2

Since Java priority queue does not allow to update value of element in priority queue therefore I decided to use TreeSet in Java as an alternate of priority queue to implement Dijikstra shortest distance algorithm. Since I can find element in treeSet with O(log n) than I remove it and now I can insert new element in treeSet. TreeSet.first() always give me a lowest value in treeSet. But this idea is not working. I don't know why?

public class Dijikstra {
      public static void dik(int n,int adj[][]) {
      TreeSet<nod> p= new TreeSet(new Mycompare());
      boolean visit[] = new boolean[n];
      nod a[] = new nod[n];

      for(int i = 0; i < n; i++) {
          a[i] =new nod();
          a[i].dis = Integer.MAX_VALUE;
          a[i].id = i;
          if(i == 0) {
              a[i].dis = 0;
          }
              p.add(a[i]);
       }


       while(p.isEmpty() == false) {
           nod temp = p.first();
           p.remove(p.first());

           visit[temp.id] = true;

           for(int i = 0; i < n; i++) {
               if(visit[i] == false && adj[temp.id][i] != 0) {

               if(a[i].dis > temp.dis + adj[temp.id][i]) {

                   p.remove(a[i]);
                   a[i].dis = temp.dis + adj[temp.id][i];
                   p.add(a[i]);

               }

               }
           }

       }
       for(int i = 0; i < n; i++) {
           System.out.println(a[i].id+ "  " + a[i].dis);
       }
   }


}

above is my Dijikstra class

my nod class is

class nod {
    int dis;
    int id;
}

my Mycompare class is

class Mycompare implements Comparator<nod> {

    @Override
    public int compare(nod t, nod t1) {
        return t.dis - t1.dis;
    }

}
mkj
  • 2,761
  • 5
  • 24
  • 28
  • 2
    Surely you can be more specific than "not working"? – meriton Aug 01 '14 at 17:24
  • when i print my treeSet after inserting all elements of array of object(nod) in TreeSet it is showing me only two elements(one with dis = 0 and other with value Integer.Max_Value) in it not all elements i am inserting in it, in other words treeSet consider two object with same value of dis(in class nod) as one – user3430383 Aug 01 '14 at 17:31

2 Answers2

0

From the javadoc of TreeSet:

Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal.

The javadoc of Set writes:

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

That is, if the comparator returns 0, the element is considered a duplicate, and a set does not admit duplicates. Therefore, a TreeSet is not a priority queue.

meriton
  • 68,356
  • 14
  • 108
  • 175
  • you want to say that i have to use comparable instead of comparator to compare object but how how i maintain TreeSet according to the fields in those object – user3430383 Aug 01 '14 at 17:54
  • I didn't say that and neither do I "want" to, because it would be incorrect. – meriton Aug 01 '14 at 18:01
  • but a topcoder algorithm page say that java user can use TreeSet to implement dijikstra http://help.topcoder.com/data-science/competing-in-algorithm-challenges/algorithm-tutorials/introduction-to-graphs-and-their-data-structures-section-3/ – user3430383 Aug 01 '14 at 18:10
0

after some research i am able to implement treeset which serve as a priority queue instead of MyCompare class(which is useless) i have to implement compareTo method in class nod and declare treeset without any comparator TreeSet set = new TreeSet()

class nod implements Comparable{

    int dis;
    int id;

    @Override
    public int compareTo(Object o) {
      nod right = (nod)o;
      if (dis < right.dis) return -1;
      if (dis > right.dis) return 1;
      if (id < right.id) return -1;
      if (id > right.id) return 1;
          return 0;    
    }    
}
alterfox
  • 1,675
  • 3
  • 22
  • 37