Quantcast

Unexpected behavior for addVertex

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Unexpected behavior for addVertex

Ajay L
Hi all,

Please refer to code snippet below.

SimpleDirectedWeightedGraph<CustomNode, CustomWeightedEdge> graph = new SimpleDirectedWeightedGraph<CustomNode, CustomWeightedEdge>(
CustomWeightedEdge.class);

CustomNode node1 = new CustomNode("node1");
graph.addVertex(node1);
CustomNode node1Copy = new CustomNode("node1");
System.out.println(node1.equals(node1Copy));
System.out.println(graph.addVertex(node1));
System.out.println(graph.addVertex(node1Copy));

When run, this produces true, false, true

As per addVertex documentation - "Adds the specified vertex to this graph if not already present. More formally, adds the specified vertex, v, to this graph if this graph contains no vertex u such that u.equals(v). If this graph already contains such vertex, the call leaves this graph unchanged and returns false. In combination with the restriction on constructors, this ensures that graphs never contain duplicate vertices"

So why is addVertex call on node1Copy returning true. What am I missing?

Using latest jgrapht release 0.9.1

Thanks
Ajay

------------------------------------------------------------------------------

_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Unexpected behavior for addVertex

Szabolcs Besenyei
Hi Ajay,

Did you override the equals and hashCode methods in your costum node and edge classes?

Üdvözlettel,

Besenyei Szabolcs

2015-10-22 19:05 GMT+02:00 Ajay L <[hidden email]>:
Hi all,

Please refer to code snippet below.

SimpleDirectedWeightedGraph<CustomNode, CustomWeightedEdge> graph = new SimpleDirectedWeightedGraph<CustomNode, CustomWeightedEdge>(
CustomWeightedEdge.class);

CustomNode node1 = new CustomNode("node1");
graph.addVertex(node1);
CustomNode node1Copy = new CustomNode("node1");
System.out.println(node1.equals(node1Copy));
System.out.println(graph.addVertex(node1));
System.out.println(graph.addVertex(node1Copy));

When run, this produces true, false, true

As per addVertex documentation - "Adds the specified vertex to this graph if not already present. More formally, adds the specified vertex, v, to this graph if this graph contains no vertex u such that u.equals(v). If this graph already contains such vertex, the call leaves this graph unchanged and returns false. In combination with the restriction on constructors, this ensures that graphs never contain duplicate vertices"

So why is addVertex call on node1Copy returning true. What am I missing?

Using latest jgrapht release 0.9.1

Thanks
Ajay

------------------------------------------------------------------------------

_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users



------------------------------------------------------------------------------

_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Unexpected behavior for addVertex

Ajay L
Thx for your quick reply, Besenyei. I had overridden equals() but had missed doing hashCode(). Did that and it works fine now. thx

On Thu, Oct 22, 2015 at 1:01 PM, Szabolcs Besenyei <[hidden email]> wrote:
Hi Ajay,

Did you override the equals and hashCode methods in your costum node and edge classes?

Üdvözlettel,

Besenyei Szabolcs

2015-10-22 19:05 GMT+02:00 Ajay L <[hidden email]>:
Hi all,

Please refer to code snippet below.

SimpleDirectedWeightedGraph<CustomNode, CustomWeightedEdge> graph = new SimpleDirectedWeightedGraph<CustomNode, CustomWeightedEdge>(
CustomWeightedEdge.class);

CustomNode node1 = new CustomNode("node1");
graph.addVertex(node1);
CustomNode node1Copy = new CustomNode("node1");
System.out.println(node1.equals(node1Copy));
System.out.println(graph.addVertex(node1));
System.out.println(graph.addVertex(node1Copy));

When run, this produces true, false, true

As per addVertex documentation - "Adds the specified vertex to this graph if not already present. More formally, adds the specified vertex, v, to this graph if this graph contains no vertex u such that u.equals(v). If this graph already contains such vertex, the call leaves this graph unchanged and returns false. In combination with the restriction on constructors, this ensures that graphs never contain duplicate vertices"

So why is addVertex call on node1Copy returning true. What am I missing?

Using latest jgrapht release 0.9.1

Thanks
Ajay

------------------------------------------------------------------------------

_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users




------------------------------------------------------------------------------

_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Loading...