I want to implement some graph algorithms that's why I am creating a kind of graph framework. Up to now I implemented directed graphs very easily with the following classes;
class Vertex {
String id;
String name;
}
class Edge {
String id;
Vertex source;
Vertex destination;
int weight;
}
class Graph {
List<Vertex> vertexes;
List<Edge> edges; }
When testing it I create:
Edge edge = new Edge(id, source_node, destination_node, weight)
This is perfectly fine in directed graphs. However in undirected graphs; I have to write like this; Let's say we have 2 nodes which are A, B and the weight between them is say 10. So because of the structure of undirected graphs I have to put two edges;
Edge e1 = new Edge(id1, A, B, 10)
Edge e2 = new Edge(id2, B, A, 10)
This type of edge creation is both inefficient and exhaustive.
Therefore how can I modify my code so that I don't have to put two edges between two nodes for undirected graphs. What is best way to integrate undirected graph type to my code as well?
Thanks for your time.
Edgeclass into two:UndirectedEdgeandDirectedEdge, they can alsoextendfrom a base class calledEdge- ogzd