简体   繁体   中英

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. My Node class looks like this:

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. I cannot, however, add to the graph using my add method which is as follows:

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?

**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. 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. 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. I did that in my Node class and nothing happened, so why am I getting errors in my Edge class? Any help would be greatly appreciated.

Also the class that both Node and Edge are declared in is defined as

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. 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. 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). 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. 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.

In general your code so far looks OK btw. I mean you seem to be on the right track.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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