用 Java 实现图

发布于 2024-12-12 18:01:23 字数 1483 浏览 0 评论 0原文

我接到一个任务,用 java 实现一个图表。它将最终用于测试搜索方法(广度优先、深度优先和迭代加深)。要创建的三个类必须实现三个相应的接口:

public interface Node {
    public Node createNode(String name, int ID, float weight);
    public Node[] getNeighbours();
    public Edge[] getEdges();
    public void addEdge(Edge e);
    public void removeEdge(Edge e);
    public String getName();
    public int getID();
    public float getWeight();
    public String toString();

public interface Edge {
    public Edge createEdge(String name, int ID, float weight);
    public Node getStartNode();
    public void setStartNode(Node n);
    public Node getEndNode();
    public void setEndNode(Node n);
    public String getName();
    public int getID();
    public float getWeight();
    public String toString();

public interface Graph {
    public Graph createGraph(String name, int ID, Node[] nodes, Edge[] edges, Node root);
    public String getName();
    public Edge[] getEdges();
    public void addEdge(Edge e);
    public Edge getEdge(String name, int ID);
    public void removeEdge(Edge e);
    public Node[] getNodes();
    public void addNode(Node n);
    public Node getNode(String name, int ID);
    public void removeNode(Node n);
    public void setRoot(Node n);
    public Node getRoot();  
    public boolean isTree();        <= optional!
    public String toString();

main 方法将位于图类中。

我有点困惑为什么每个类都有创建方法而不是构造函数。

任何人都可以建议我是否应该使用邻接矩阵或邻接列表来存储边?

任何和所有的帮助将不胜感激。

谢谢

I've been given a task to implement a graph in java. It will eventually be used to test search methods (breadth first, depth first and iterative deepening). The three classes to be made have to implement three corresponding interfaces:

public interface Node {
    public Node createNode(String name, int ID, float weight);
    public Node[] getNeighbours();
    public Edge[] getEdges();
    public void addEdge(Edge e);
    public void removeEdge(Edge e);
    public String getName();
    public int getID();
    public float getWeight();
    public String toString();

public interface Edge {
    public Edge createEdge(String name, int ID, float weight);
    public Node getStartNode();
    public void setStartNode(Node n);
    public Node getEndNode();
    public void setEndNode(Node n);
    public String getName();
    public int getID();
    public float getWeight();
    public String toString();

public interface Graph {
    public Graph createGraph(String name, int ID, Node[] nodes, Edge[] edges, Node root);
    public String getName();
    public Edge[] getEdges();
    public void addEdge(Edge e);
    public Edge getEdge(String name, int ID);
    public void removeEdge(Edge e);
    public Node[] getNodes();
    public void addNode(Node n);
    public Node getNode(String name, int ID);
    public void removeNode(Node n);
    public void setRoot(Node n);
    public Node getRoot();  
    public boolean isTree();        <= optional!
    public String toString();

The main method will be in the graph class.

I'm a bit confused as to why there is create methods for each class rather than constructors.

Also can anyone advise on whether I should store edges using an adjacency matrix or an adjancency list?

Any and all help will be greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日裸衫吸 2024-12-19 18:01:24

我和你一起质疑在界面中放置创建方法是否明智。接口中的方法必须在实现类中实现作为实例方法,通常像这样的方法,当它们用来代替构造函数时,是方法,即静态工厂方法,不能出现在接口中。

在 Java 接口中拥有创建方法的唯一原因是进行克隆,但这里的情况似乎并非如此。当您实现这些接口时,创建方法将调用您的构造函数(您可以自由地以任何您想要的方式实现),但这些方法有点愚蠢,因为作为实例方法,您将需要有现有的对象来调用它们。

至于你的第二个问题,邻接矩阵、邻接表或关联表的选择完全取决于你。

I'm with you on questioning the wisdom of placing create methods in the interface. Methods in an interface have to be implemented as instance methods in the implementing classes, and normally methods like this, when they are used in place of constructors, are class methods, namely static factory methods, which cannot appear in interfaces.

The only reason to have a creation method in a Java interface is if one is doing cloning, which does not appear to be the case here. When you implement these interfaces the creation methods will call your constructor (which you are free to implement any way you want), but these methods are kind of silly because, as instance methods, you will need to have existing objects on which to call them.

As for your second question, the choice of adjacency matrix, adjacency list, or incidence list is totally up to you.

情栀口红 2024-12-19 18:01:24

接口不能定义构造函数。有关一些讨论,请参阅此 wiki 页面

请参阅有关邻接表权衡的维基百科页面,了解可能引导您进行的简短讨论正确的方向。

Interfaces can't define constructors. For some discussion, see this wiki page.

See the wikipedia page on adjacency list tradeoffs for a brief discussion that might lead you in the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文