Results 1 to 12 of 12
- 06-11-2012, 08:08 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 7
- Rep Power
- 0
problem in comparator interface implementing
hello, i have writen the following class
then i have writen this method to sort a array of Vertex objectsJava 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; } }
the code is rynning without any problem but the output is wrong, i cant understand why.Java Code:public void sort() throws Exception { Arrays.sort(vertices, new vDegreeComparator()); ........................................ }
(sorry for bad english)
- 06-11-2012, 08:29 PM #2
Re: problem in comparator interface implementing
Can you show the output and explain what is wrong?the output 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.
- 06-11-2012, 08:49 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 7
- Rep Power
- 0
Re: problem in comparator interface implementing
the input (before sorting) is
the expected output is (sorting by ascending order)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
but the outpout that i get isJava 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
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
- 06-11-2012, 08:56 PM #4
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.
- 06-11-2012, 09:07 PM #5
Member
- Join Date
- Jun 2012
- Posts
- 7
- Rep Power
- 0
Re: problem in comparator interface implementing
for debugging pruposes i use the following method
the getVertexDegree method is working fine.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); } }
and here is the excecution code
and the outputJava 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(); } }
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)
- 06-11-2012, 09:15 PM #6
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=" + v1DIf you don't understand my response, don't ignore it, ask a question.
- 06-11-2012, 09:19 PM #7
Member
- Join Date
- Jun 2012
- Posts
- 7
- Rep Power
- 0
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.
- 06-11-2012, 09:39 PM #8
Member
- Join Date
- Jun 2012
- Posts
- 7
- Rep Power
- 0
Re: problem in comparator interface implementing
ok, this is the debugging code i wrote
and the output (with red the wrong results)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 }
i will study the results, and i hope i find out what is going on hereJava 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)
Last edited by fractal; 06-11-2012 at 09:46 PM.
- 06-11-2012, 09:54 PM #9
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.
- 06-11-2012, 10:18 PM #10
Member
- Join Date
- Jun 2012
- Posts
- 7
- Rep Power
- 0
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
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!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; }
and the new version
and the outputJava 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 } }
it is obvius that the getVertexDegree method provides wrong results in compare method.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)
The getVertexDegree method provides correct results in printVertexDegree method.
- 06-11-2012, 10:26 PM #11
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.
- 06-11-2012, 10:33 PM #12
Member
- Join Date
- Jun 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
NEED HELP in IMPLEMENTING A GIVEN INTERFACE!
By wasiqjaved in forum New To JavaReplies: 0Last Post: 10-03-2011, 12:40 PM -
Implementing Interface
By mew in forum New To JavaReplies: 4Last Post: 02-16-2010, 03:33 PM -
help! implementing an interface
By manda147 in forum New To JavaReplies: 28Last Post: 11-17-2008, 04:27 AM -
Implementing an interface
By bugger in forum Advanced JavaReplies: 1Last Post: 01-09-2008, 01:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks