Results 1 to 12 of 12
  1. #1
    fractal is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default problem in comparator interface implementing

    hello, i have writen the following class

    Java Code:
    class vDegreeComparator implements Comparator<Vertex>  {
        
      @Override
        public int compare(Vertex v1, Vertex v2) {
            int v1D=0;
            int v2D=0;
        
            try{
            v1D=getVertexDegree(v1);
            v2D=getVertexDegree(v2);
            
            
            }   
            
            catch(ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException){
               System.err.printf("\narrayindexOutOfBoundsException: %s\n", arrayIndexOutOfBoundsException);
            }
            catch(Exception exception){
            System.err.printf("\nException: %s\n", exception);
            }
            if (v1D>v2D) return 1;
            else
              if (v1D<v2D) return -1;
            return 0;
            
           
            }
      }
    then i have writen this method to sort a array of Vertex objects
    Java Code:
    public void sort() throws Exception {
    
          Arrays.sort(vertices, new vDegreeComparator());
          ........................................
    }
    the code is rynning without any problem but the output is wrong, i cant understand why.

    (sorry for bad english)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: problem in comparator interface implementing

    the output is wrong
    Can you show the output and explain what is wrong?

    Add some debug code to the compare method to print out the values of v1 & v1D and v2 & v2D.

    The class may need a toString() method to get usable print outs.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    fractal is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem in comparator interface implementing

    the input (before sorting) is
    Java Code:
    vertex std a -> degree 3
    vertex std b -> degree 2
    vertex std c -> degree 4
    vertex std d -> degree 2
    vertex std e -> degree 3
    vertex std f -> degree 2
    vertex std g -> degree 2
    the expected output is (sorting by ascending order)
    Java Code:
    vertex std b -> degree 2
    vertex std d -> degree 2
    vertex std f -> degree 2
    vertex std g -> degree 2
    vertex std e -> degree 3
    vertex std c -> degree 4
    but the outpout that i get is
    Java Code:
    vertex std b -> degree 2
    vertex std a -> degree 3
    vertex std f -> degree 2
    vertex std e -> degree 3
    vertex std d -> degree 2
    vertex std c -> degree 4
    vertex std g -> degree 2

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: problem in comparator interface implementing

    What does the debug print out look like?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    fractal is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem in comparator interface implementing

    for debugging pruposes i use the following method

    Java Code:
     public void printVertexDegree()throws Exception{
            for(int i=0; i<vertices.length; i++ ) {
               int deg=getVertexDegree(vertices[i]);
               System.out.println("vertex "+vertices[i]+" -> degree "+deg);
            }
        }
    the getVertexDegree method is working fine.

    and here is the excecution code

    Java Code:
    public static void main(String[] args) throws Exception {
            Lib.initiateProject();
            Graph g=new Graph("tst",900);
            //g.shuffle();
            g.print();
            //g.save("tst", 7);
            //g.save("tst",3);//
            g.printVertexDegree();
            g.sort();
            g.print();
            g.printVertexDegree();
    
        }
    }
    and the output

    Java Code:
    run:
    //input graph- g.print()
    std a
    std b
    std c
    std d
    std e
    std f
    std g
    -- end of vertices --
    std a std b 1.0
    std a std c 1.0
    std a std d 1.0
    std b std c 1.0
    std c std d 1.0
    std c std e 1.0
    std e std f 1.0
    std e std g 1.0
    std f std g 1.0
    
    // g.printVertexDegree();
    vertex std a -> degree 3
    vertex std b -> degree 2
    vertex std c -> degree 4
    vertex std d -> degree 2
    vertex std e -> degree 3
    vertex std f -> degree 2
    vertex std g -> degree 2
    
    // g.print(); after sorting - this is wrong sequence
    std b
    std a
    std f
    std e
    std d
    std c
    std g
    -- end of vertices --
    std b std a 1.0
    std b std c 1.0
    std a std d 1.0
    std a std c 1.0
    std f std e 1.0
    std f std g 1.0
    std e std c 1.0
    std e std g 1.0
    std d std c 1.0
    
    //g.printVertexDegree(); - this is wrong
    vertex std b -> degree 2
    vertex std a -> degree 3
    vertex std f -> degree 2
    vertex std e -> degree 3
    vertex std d -> degree 2
    vertex std c -> degree 4
    vertex std g -> degree 2
    BUILD SUCCESSFUL (total time: 3 seconds)

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: problem in comparator interface implementing

    Where is the debug output from the compare() method showing the values of v1 & v1D and v2 & v2D?
    Be sure to add id Strings so the output can be recognized: "v1D=" + v1D
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    fractal is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem in comparator interface implementing

    ok, i understand what you mean. I will check this out, and i will back

    I apreciate your help, thank you so mutch.

  8. #8
    fractal is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem in comparator interface implementing

    ok, this is the debugging code i wrote

    Java Code:
    class vDegreeComparator implements Comparator<Vertex>  {
        
      @Override
        public int compare(Vertex v1, Vertex v2) {
            int v1D=0;
            int v2D=0;
        
            try{
            v1D=getVertexDegree(v1);
            System.out.println("Vertex "+v1+" has degree v1D="+v1D);
            v2D=getVertexDegree(v2);
            System.out.println("Vertex "+v2+" has degree v2D="+v2D);
            
            
            }   
            
            catch(ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException){
               System.err.printf("\narrayindexOutOfBoundsException: %s\n", arrayIndexOutOfBoundsException);
            }
            catch(Exception exception){
            System.err.printf("\nException: %s\n", exception);
            }
            if (v1D>v2D) return 1;
            else
              if (v1D<v2D) return -1;
            return 0;
            
           // return
            }
    and the output (with red the wrong results)
    Java Code:
    run:
    std a
    std b
    std c
    std d
    std e
    std f
    std g
    -- end of vertices --
    std a std b 1.0
    std a std c 1.0
    std a std d 1.0
    std b std c 1.0
    std c std d 1.0
    std c std e 1.0
    std e std f 1.0
    std e std g 1.0
    std f std g 1.0
    vertex std a -> degree 3
    vertex std b -> degree 2
    vertex std c -> degree 4
    vertex std d -> degree 2
    vertex std e -> degree 3
    vertex std f -> degree 2
    vertex std g -> degree 2
    Vertex std b has degree v1D=2
    Vertex std a has degree v2D=3
    Vertex std c has degree v1D=4
    Vertex std b has degree v2D=2
    Vertex std c has degree v1D=4
    Vertex std a has degree v2D=2//wrong
    Vertex std d has degree v1D=2
    Vertex std a has degree v2D=2//wrong
    Vertex std d has degree v1D=2
    Vertex std c has degree v2D=4
    Vertex std e has degree v1D=3
    Vertex std d has degree v2D=4//wrong
    Vertex std e has degree v1D=3
    Vertex std a has degree v2D=2//wrong
    Vertex std f has degree v1D=2
    Vertex std e has degree v2D=4//wrong
    Vertex std f has degree v1D=2
    Vertex std a has degree v2D=2//wrong
    Vertex std g has degree v1D=2
    Vertex std e has degree v2D=2//wrong
    Vertex std g has degree v1D=2
    Vertex std c has degree v2D=2//wrong
    std b
    std a
    std f
    std e
    std d
    std c
    std g
    -- end of vertices --
    std b std a 1.0
    std b std c 1.0
    std a std d 1.0
    std a std c 1.0
    std f std e 1.0
    std f std g 1.0
    std e std c 1.0
    std e std g 1.0
    std d std c 1.0
    vertex std b -> degree 2
    vertex std a -> degree 3
    vertex std f -> degree 2
    vertex std e -> degree 3
    vertex std d -> degree 2
    vertex std c -> degree 4
    vertex std g -> degree 2
    BUILD SUCCESSFUL (total time: 0 seconds)
    i will study the results, and i hope i find out what is going on here
    Last edited by fractal; 06-11-2012 at 10:46 PM.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: problem in comparator interface implementing

    The print out would be easier to understand if you used: "v1" & "v2" instead of "Vertex "
    for example: "V1="+v1+

    All the lines are labelled: "Vertex ", you don't know if its v1 or v2


    How does getVertexDegree() work? Shouldn't it be a method of the Vertex class?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    fractal is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem in comparator interface implementing

    the getVertexDegree method is a method of the current public class (class Graph). The public class Vertex is an other public class of the project, the whole project contains about 20 public classes.

    here is the getVertex method
    Java Code:
    /**
     * Calculates the degree of the specified vertex v, i.e. the degree of a vertex
     * is equal to the number of edges (links) connected to the vertex. This method
     * do not support graphs containing loops.
     * @param v the specified vertex.
     * @return the degree of the vertex.
     * @throws Exception
     */
        public int getVertexDegree(Vertex v) throws Exception{
           int idx=indexOf(v);
           int degree=0;
              for(int i=0; i<order()-1; i++)
                  for (int j=i+1; j<order(); j++) {
                     double rVal=adjMatrix.get(i,j);
                     if (rVal!=0)
                         if (idx==i || idx==j)
                             degree++;
                  }
           return degree;
    
        }
    the funny thing is that i have implemented correctly the Comparable class to solve my problem, but the professor preffers the Comparator implemetation, so here i am!

    and the new version
    Java Code:
        class vDegreeComparator implements Comparator<Vertex>  {
        
      @Override
        public int compare(Vertex v1, Vertex v2) {
            int v1D=0;
            int v2D=0;
        
            try{
            v1D=getVertexDegree(v1);
            System.out.println("v1="+v1+"  v1D="+v1D);
            v2D=getVertexDegree(v2);
            System.out.println("v2="+v2+"  v2D="+v2D);
            
            
            }   
            
            catch(ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException){
               System.err.printf("\narrayindexOutOfBoundsException: %s\n", arrayIndexOutOfBoundsException);
            }
            catch(Exception exception){
            System.err.printf("\nException: %s\n", exception);
            }
            if (v1D>v2D) return 1;
            else
              if (v1D<v2D) return -1;
            return 0;
            
           // return
            }
      }
    and the output

    Java Code:
    run:
    std a
    std b
    std c
    std d
    std e
    std f
    std g
    -- end of vertices --
    std a std b 1.0
    std a std c 1.0
    std a std d 1.0
    std b std c 1.0
    std c std d 1.0
    std c std e 1.0
    std e std f 1.0
    std e std g 1.0
    std f std g 1.0
    vertex std a -> degree 3
    vertex std b -> degree 2
    vertex std c -> degree 4
    vertex std d -> degree 2
    vertex std e -> degree 3
    vertex std f -> degree 2
    vertex std g -> degree 2
    v1=std b  v1D=2
    v2=std a  v2D=3
    v1=std c  v1D=4
    v2=std b  v2D=2
    v1=std c  v1D=4
    v2=std a  v2D=2
    v1=std d  v1D=2
    v2=std a  v2D=2
    v1=std d  v1D=2
    v2=std c  v2D=4
    v1=std e  v1D=3
    v2=std d  v2D=4
    v1=std e  v1D=3
    v2=std a  v2D=2
    v1=std f  v1D=2
    v2=std e  v2D=4
    v1=std f  v1D=2
    v2=std a  v2D=2
    v1=std g  v1D=2
    v2=std e  v2D=2
    v1=std g  v1D=2
    v2=std c  v2D=2
    std b
    std a
    std f
    std e
    std d
    std c
    std g
    -- end of vertices --
    std b std a 1.0
    std b std c 1.0
    std a std d 1.0
    std a std c 1.0
    std f std e 1.0
    std f std g 1.0
    std e std c 1.0
    std e std g 1.0
    std d std c 1.0
    vertex std b -> degree 2
    vertex std a -> degree 3
    vertex std f -> degree 2
    vertex std e -> degree 3
    vertex std d -> degree 2
    vertex std c -> degree 4
    vertex std g -> degree 2
    BUILD SUCCESSFUL (total time: 1 second)
    it is obvius that the getVertexDegree method provides wrong results in compare method.
    The getVertexDegree method provides correct results in printVertexDegree method.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: problem in comparator interface implementing

    Does the degree value for a Vortex change? If not, it should be inside the Vortex class. Computing it every time you want it is not a good technique.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    fractal is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem in comparator interface implementing

    no, it is constant for each Vertex object, it is a new element in the project. I guess you are right, i will discuss with the proffesor

Similar Threads

  1. NEED HELP in IMPLEMENTING A GIVEN INTERFACE!
    By wasiqjaved in forum New To Java
    Replies: 0
    Last Post: 10-03-2011, 01:40 PM
  2. Implementing Interface
    By mew in forum New To Java
    Replies: 4
    Last Post: 02-16-2010, 04:33 PM
  3. help! implementing an interface
    By manda147 in forum New To Java
    Replies: 28
    Last Post: 11-17-2008, 05:27 AM
  4. Implementing an interface
    By bugger in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2008, 02:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •