Skip to content
Advertisement

Custom Edge with jgrapht

I want to define a custom edge

JavaScript

But when I try to use the super()is that possible? What should I use for EdgeFactory when creating a graph?

JavaScript

Would this be sufficient? Or should I create a new class who extends EdgeFactory as well?

Advertisement

Answer

You do not require to subclass DefaultWeightedEdge to create your custom edge, the following works also. I’m currently using version 0.9.0 of JGraphT.

Class definition :

JavaScript

Graph creation :

JavaScript

Adding custom edge and setting weight :

JavaScript

Also consider to implement equals and hashCode methods of your Connection class, as JGraphT relies on those methods since version 0.8.4, see EqualsAndHashCode article. This article also provides an example where DefaultWeightedEdge is subclassed.

Advertisement