0

I am using DefaultDirectedGraph to create my directed graph, where each vertex is an object.

DefaultDirectedGraph g = new DefaultDirectedGraph(DefaultEdge.class);

I was wondering if it is possible to characterize the edges? For example, I would like to keep the information of the friendship between students.

Or should I have a map between edges and the objects of friendship?

Mina Nabi
  • 3
  • 3
  • Not sure what you are asking for. Graph friendshipGraph=new DefaultUndirectedGraph<>(DefaultEdge.class); Add some people: friendshipGraph.addVertex(new Person("Jeff")); ... Add relations: friendshipGraph.addEdge(p1, p2); To query whether 2 people have a relation: friendshipGraph.containsEdge(p1,p2); To query all relationships: friendshipGraph.edgeSet(); – Joris Kinable Mar 19 '18 at 14:52
  • I want to keep some information about the edge between vertices. (please note that I used student and friendship example just to clarify my question. Although friendship relation does not need to be directional, but in my real problem the edge needs to be directional. Lets say I want to keep the information of how initiated the friendship and number of years that the students are friend. ) I know in JgraphT, vertices can be a class such as student. Is there any possibility to define a class for edges as well, instead of using DefaultEdge class). Thank you in advance. – Mina Nabi Mar 19 '18 at 17:00

1 Answers1

1

You can certainly store information on the edges. Here is a usecase I recently used myself:

public class Intersection{
    public final long latitude;
    public final long longitude;
    public Intersection(long latitude, long longitude){ ...}
}

public class Street extends DefaultWeightedEdge{
    public final String streetName;
    public final int speedLimit;
    public final Street(...){...}
}

public class RoadNetwork{
    public final Graph<Intersection, Street> network=new DefaultDirectedGraph<>(Street.class);
    Intersection i1=new Intersection(..);
    Intersection i2=new Intersection(..);
    Street s=new Street(..);

    //Network with 1 street
    network.addVertex(i1);
    network.addVertex(i2);
    network.addEdge(i1,i2,s);
}

Notes:

  1. Street extends DefaultWeightedEdge: it is no longer necessary to extend DefaultEdge or DefaultWeightedEdge in recent versions of jgrapht
  2. In jgrapht, all edges are objects. When you use custom objects (such as Street) in the above example, you can only use the addEdge(vertex1, vertex2, edgeObject) method. You cannot use the addEdge(vertex1,vertex2) method unless you provide an EdgeFactory to the graph constructor.
  3. A common mistake is to store graph information on the edges. E.g. it would be wrong to store the source and target Intersection (the 2 endpoints of the street) on the street object itself. This information is stored in the graph.
  4. When implementing your own vertex or edge classes, you must implement the equals and hashCode methods.See https://github.com/jgrapht/jgrapht/wiki/Users:-EqualsAndHashCode for details.
Joris Kinable
  • 2,232
  • 18
  • 29