in Education by
I want to implement in Java a class for handling graph data structures. I have a Node class and an Edge class. The Graph class maintains two list: a list of nodes and a list of edges. Each node must have an unique name. How do I guard against a situation like this: Graph g = new Graph(); Node n1 = new Node("#1"); Node n2 = new Node("#2"); Edge e1 = new Edge("e#1", "#1", "#2"); // Each node is added like a reference g.addNode(n1); g.addNode(n2); g.addEdge(e1); // This will break the internal integrity of the graph n1.setName("#3"); g.getNode("#2").setName("#4"); I believe I should clone the nodes and the edges when adding them to the graph and return a NodeEnvelope class that will maintain the graph structural integrity. Is this the right way of doing this or the design is broken from the beginning ? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I work with graph structures in Java a lot, and my advice would be to make any data member of the Node and Edge class that the Graph depends on for maintaining its structure final, with no setters. In fact, if you can, I would make Node and Edge completely immutable, which has many benefits. So, for example: public final class Node { private final String name; public Node(String name) { this.name = name; } public String getName() { return name; } // note: no setter for name } You would then do your uniqueness check in the Graph object: public class Graph { Set nodes = new HashSet(); public void addNode(Node n) { // note: this assumes you've properly overridden // equals and hashCode in Node to make Nodes with the // same name .equal() and hash to the same value. if(nodes.contains(n)) { throw new IllegalArgumentException("Already in graph: " + node); } nodes.add(n); } } If you need to modify a name of a node, remove the old node and add a new one. This might sound like extra work, but it saves a lot of effort keeping everything straight. Really, though, creating your own Graph structure from the ground up is probably unnecessary -- this issue is only the first of many you are likely to run into if you build your own. I would recommend finding a good open source Java graph library, and using that instead. Depending on what you are doing, there are a few options out there. I have used JUNG in the past, and would recommend it as a good starting point.

Related questions

0 votes
    To add a new element to a list we use which Python command? a) list1.addEnd(5) b) list1.addLast(5) c) list1.append(5) d) list1.add(5)...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    How can I clone or deep copy an object so that the cloned object can be modified without any changes being reflected in the original object?...
asked Jan 15, 2021 in Technology by JackTerrance
0 votes
    which of the following tab should be used to add and delete element such as chapter number hyperlink etc a) ... style d) background Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    which of the following tab should be used to add and delete element such as chapter number ,hyperlink etc Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    ________ modify a ggplot or theme object by adding on new components. (a) +.gg (b) -.gg (c) /.gg ... Data Analysis of R Programming Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    What is the use of git clone command?...
asked Nov 4, 2020 in Technology by JackTerrance
0 votes
    Delphi 8 introduced Class Helpers for the purposes of mapping the VCL/RTL to the .NET object hierarchy. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    Where does a new element be inserted in linked list implementation of a queue? (a) Head of list (b) ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Where does a new element be inserted in linked list implementation of a queue? (a) Head of list (b ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    I've been reading the legacy code,which invloves in the customized memory pooling system, then I found that ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    When should I use SQL Azure and when should I use table Storage? I was thinking, use table storage for transaction ... do you think? Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    To solve any problem when we can use genetic algorithms as opposed to neural networks & vice versa. I saw some ... in some cases. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    When should I use a struct rather than a class in C#?...
asked Jan 16, 2021 in Technology by JackTerrance
...