简体   繁体   English

图实现可比节点Java

[英]Graph implementing a comparable node java

For one of my projects in my computer science class, I have to implement a directed, weighted graph. 对于我在计算机科学课上的一个项目,我必须实现一个有向加权图。 Since we are only allowed to use an adjacency list and not an adjacency matrix, I decided to create a Node class that will contain my data, as well as a treeset to hold all of its edges, since the project stipulates that the edges must be sorted using the natural ordering of whatever data my graph is instantiated with. 由于只允许使用邻接表,而不允许使用邻接矩阵,因此我决定创建一个Node类,该类将包含我的数据以及一个树形集以容纳其所有边缘,因为该项目规定边缘必须为使用实例化我的图形的任何数据的自然顺序进行排序。 My Node class looks like this: 我的Node类如下所示:

private class Node<V extends Comparable<V>> {
  private V data;
  private TreeSet<Edge> edges = new TreeSet<Edge>();

  public Node(V data) {
      this.data = data;
  }
}

My graph itself is also a treeset that contains objects of type Node. 我的图本身也是一个包含Node类型对象的树集。 I cannot, however, add to the graph using my add method which is as follows: 但是,我不能使用如下所示的add方法将其添加到图形中:

private boolean addVertex(V vertex) {
    Iterator iter = graph.iterator();
    Node check;

    while (iter.hasNext()) {
        check = (Node) iter.next();
        if (check.data.compareTo(vertex) == 0)
            return false;
    }

    Node n = new Node(vertex);
    graph.add(n);
    return true;
}

How can I make it so that my code adds Node s to my graph class using the natural ordering of the data that the Node class is instantiated with? 如何做到这一点,以便我的代码使用实例化Node类的数据的自然顺序将Node添加到我的图形类中?

**Edit So based on what Peter said, I was able to come up with a solution (sort of) to my problem. **编辑因此,根据彼得所说的,我能够提出解决问题的方法。 Since my edges, per project stipulation, must be iterated over in the natural order of the data held in the nodes, I created a compareTo method in my edge class that works by using the data's compareTo method. 由于必须按照项目规定的自然顺序来遍历我的边缘,因此我在边缘类中创建了compareTo方法,该方法通过使用数据的compareTo方法来工作。 It looks something like this: 看起来像这样:

private class Edge<V extends Comparable<V>> implements Comparable<V> {
  private int weight;
  private boolean visited;
  //This is the data held in the node the edge ends at
  private V endNode;

  public Edge(V dest, int weight) {
      visited = false;
      endNode = dest;
      this.weight = weight;
  }

public int compareTo(Edge<V> e) {
    if (endNode.compareTo((V) e.endNode) < 0)
        return -1;
    else if (endNode.compareTo((V) e.endNode) == 0)
        return 0;
    else
        return 1;
}
}

Unfortunately, when I try and do this, I get two errors. 不幸的是,当我尝试执行此操作时,出现两个错误。 one of them says "the type parameter V is hiding the type V" and the other says I must implement the Comparable.compareTo(V) method even though I explicitly do it in my code. 他们中的一个说“类型参数V隐藏了类型V”,另一个说我必须实现Comparable.compareTo(V)方法,即使我在代码中明确地做到了。 I was able to find some information on the first error, which said that I could be getting that error because I used V as a concrete type somewhere in my code, however, that does not really help me much since I don't fully understand it. 我能够找到有关第一个错误的一些信息,该错误表示我可能会收到该错误,因为我在代码中的某处使用了V作为具体类型,但是,由于我不完全了解,这并没有太大帮助它。 I did that in my Node class and nothing happened, so why am I getting errors in my Edge class? 我是在Node类中完成此操作的,但没有任何反应,那么为什么在Edge类中却出现错误? Any help would be greatly appreciated. 任何帮助将不胜感激。

Also the class that both Node and Edge are declared in is defined as 同时声明了Node和Edge的类也定义为

public class Graph<V extends Comparable<V>>

if that helps anyone 如果这可以帮助任何人

The wikipedia page http://en.wikipedia.org/wiki/Adjacency_list is a good starting point. Wikipedia页面http://en.wikipedia.org/wiki/Adjacency_list是一个很好的起点。 Also, google for this "Goodrich and Tamassia adjacency". 此外,谷歌为此“古德里奇和塔玛西娅邻接”。 This is a good starting point too. 这也是一个很好的起点。

Because your Graph is weighted and directed, it means you can/should associate with each Vertex the list of its outgoing (or alternatively incoming) Edge s. 因为您的图是加权和有向的,所以这意味着您可以/应该将每个Vertex的传出(或传入) Edge列表与关联。 An Edge is then an ordered couple of its two vertices (start vertex and end vertex) and whatever additional information you may want to store in it (eg its weight). 然后, Edge是其两个顶点(起始顶点和终止顶点)及其中可能要存储的任何其他信息(例如,其权重)的有序对。 That's what you need here. 这就是您的需要。

Seems to me the natural ordering you're referring to is about the Edges, not about the Vertices. 在我看来,您所指的自然顺序是关于边缘的,而不是关于顶点的。 But you can have some ordering the vertices too. 但是您也可以对顶点进行排序。

You can also have a Graph class. 您也可以有一个Graph类。 The Graph can be just your top-level container class which gives you fast access to eg: 1) a Vertex given its id/name (or whatever piece of data you use to identify it), 2) an Edge given eg its start and end vertices, other methods, etc. Graph可以只是您的顶级容器类,它使您可以快速访问例如:1)给定Vertex的ID /名称(或用于标识它的任何数据)的Vertex ,2)给定其起点和Edge顶点,其他方法等

In general your code so far looks OK btw. 一般来说,到目前为止,您的代码看起来还不错。 I mean you seem to be on the right track. 我的意思是您似乎处在正确的轨道上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM