Graphs OOP app
Hey there, I wanted to ask if the structure for my OOP-based graphs interface made sense or if there are some places I can change things. I want to emphasise beforehand that, althought I know i could simply represent a graph using the adjacency matrix, I want my interface to be more intuitive that hand-typing a random matrix, so I wanted to store it as a List of nodes and edges. (if there's a better way please let me know)
Anyways, the current structure for my program goes a little something like this:
- When it comes to edges, I have an abstract class for an arbitrary edge, from which 2 different classes will inherit: Directed edges (
Arc
) and undirected edges (UEdge
). I'm representing the type of edge using an EdgeType
enum. Also, for the endpoints, I'm simply representing it as a Tuple<Node, Node>
- The Node
class is one that im not sure what to put inside, for the moment all that I have in the node class is an integer for its index in the graph.
- The Graph
class is an abstract class from which 3 classes will inherit: DiGraph
(Directed Graph), UGraph
(Undirected Graph) and PGraph
(Ponderated graph, a graph in which each edge has a value i.e. the use case of Djikstra's algorithm). This class would have a List<Edge>
called Edges
for all the edges in the graph, and a List<Node>
called Nodes
for all the Nodes. Nodes
wouldn't be taken as an argument in the constructor, since I could just extract the nodes from the edges and then set the list equal to those in the constructor.
I don't wanna try and plan out everything beforehand because I feel like i'll get overwhelmed, so I would appreciate if you guys told me if this structure makes sense so far or if there are some things i could fix so they dont bite me in the ass later down the line. Thanks in advance!7 Replies
This isn't related to the problem, but please do not use
Tuple<Node, Node>
, use (Node, Node)
.what's the difference?
Tuple<T, T>
is a reference type, (T, T)
is a value type.Tuple<Node, Node>
is a class, but (Node, Node)
is a struct, and so, it will be extremely optimized by the compiler/JIT, also, you can name each part of the struct (Node left, Node right)
Yeah, value tuples are just generally better.
ah i see
thanks for the suggestion
alright so i made a little diagram to visualize how i want the project to be organized, does this look adequate? (i did make a few changes, like completely getting rid of the node class since all it was containing was an integer for the id