番石榴ValueGraph的简单示例

RV.*_*RV. 1 graph guava

我正在寻找番石榴ValueGraph的简单示例。就像是:

class GraphNode {
  String name;
  String value;
  // Do I need to override equals & hashcode methods here??

}

class GraphUser {
  public ValueGraph<GraphNode,Double> createGraph(){
    ValueGraph<GraphNode,Double> graph = ValueGraphBuilder.directed.build();
    // How do I add the nodes to graph??
    // How do I add the edges to graph?
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 如何创建以自定义对象为节点的图形?
  2. 如何在图形中添加节点和边?
  3. 我是否必须在自定义节点类中重写equals和hashCode方法?

一个简单的例子将非常有帮助。

jbd*_*can 6

番石榴维基给出使用下面的例子ValueGraph

MutableValueGraph<Integer, Double> weightedGraph = ValueGraphBuilder.directed().build();
weightedGraph.addNode(1);
weightedGraph.putEdgeValue(2, 3, 1.5);  // also adds nodes 2 and 3 if not already present
weightedGraph.putEdgeValue(3, 5, 1.5);  // edge values (like Map values) need not be unique
...
weightedGraph.putEdgeValue(2, 3, 2.0);  // updates the value for (2,3) to 2.0
Run Code Online (Sandbox Code Playgroud)

我会尽力按照您询问其他问题的顺序回答您:

  1. 如何创建以自定义对象为节点的图形?

    public final class GraphNode {
      private final String name;
      private final int age;
    
      GraphNode(String name, int age) {
        this.name = Objects.requireNonNull(name, "name");
        this.age = age;
      }
    
      public String name() {
        return name;
      }
    
      public int age() {
        return age;
      }
    
      @Override
      public boolean equals(Object other) {
        if (that instanceof GraphNode) {
          GraphNode that = (GraphNode) other;
          return this.name.equals(that.name)
              && this.age == that.age;
        }
        return false;
      }
    
      @Override
      public int hashCode() {
        return Objects.hash(name, age);
      }
    
      @Override
      public String toString() {
        return "(" + name + ", " + age + ")";
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    稍后会更多有关使用此类的对象创建值图的信息。

  2. 如何在图形中添加节点和边?

    MutableValueGraph<GraphNode, Double> weightedGraph = ValueGraphBuilder.directed().build();
    GraphNode a = new GraphNode("Jonathan", 20);
    GraphNode b = new GraphNode("Nicolas", 40);
    GraphNode c = new GraphNode("Georgia", 30);
    weightedGraph.putEdgeValue(a, b, 2.0);
    weightedGraph.putEdgeValue(a, c, 4.5);
    
    Run Code Online (Sandbox Code Playgroud)

    这将产生如下图(箭头向下):

           (Jonathan, 20)
                 / \
              2.0   4.5
               /     \
    (Nicolas, 40)   (Georgia, 30)
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我是否必须在自定义节点类中重写equals和hashCode方法?

    这不是严格必要的,但强烈建议这样做,因为否则,在下面的代码示例中,图形可能看起来与您期望的不同。

    MutableValueGraph<GraphNode, Double> weightedGraph = ValueGraphBuilder.directed().build();
    GraphNode a = new GraphNode("Jonathan", 20);
    GraphNode b = new GraphNode("Nicolas", 40);
    GraphNode c = new GraphNode("Georgia", 30);
    weightedGraph.putEdgeValue(a, b, 2.0);
    weightedGraph.putEdgeValue(a, c, 4.5);
    weightedGraph.putEdgeValue(b, new GraphNode("Luke", 10), 6.0);
    weightedGraph.putEdgeValue(c, new GraphNode("Luke", 10), 1.5);
    
    Run Code Online (Sandbox Code Playgroud)

    使用中的自定义equals()hashCode()实现GraphNode,图形将产生以下预期形状:

            (Jonathan, 20)
                 / \
              2.0   4.5
               /     \
    (Nicolas, 40)   (Georgia, 30)
               \     /
              6.0   1.5
                 \ /
             (Luke, 10)
    
    Run Code Online (Sandbox Code Playgroud)

    但是,如果没有equals()hashCode(),则值图将无法确定两个new GraphNode("Luke", 10)s在逻辑上是同一节点,因此将产生以下错误形状:

            (Jonathan, 20)
                 / \
              2.0   4.5
               /     \
    (Nicolas, 40)   (Georgia, 30)
              |       |
             6.0     1.5
              |       |
       (Luke, 10)   (Luke, 10)
    
    Run Code Online (Sandbox Code Playgroud)

我希望这有帮助!