Guava ValueGraph的简单示例

时间:2017-11-16 18:53:29

标签: graph guava

我正在寻找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?
  }
}
  1. 如何使用自定义对象创建图表作为节点?
  2. 如何添加节点&amp;边缘图?
  3. 我是否必须覆盖等于&amp;自定义节点类中的hashCode方法?
  4. 一个简单的例子非常有帮助。

1 个答案:

答案 0 :(得分:5)

Guava wiki给出了以下使用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

我会按照您提出的顺序回答您的其他问题:

  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 + ")";
      }
    }
    

    稍后将详细介绍如何使用此类的对象创建值图。

  2. 如何添加节点&amp;边缘图表?

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

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

           (Jonathan, 20)
                 / \
              2.0   4.5
               /     \
    (Nicolas, 40)   (Georgia, 30)
    
  3. 我是否必须覆盖等于&amp;自定义节点类中的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);
    

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

            (Jonathan, 20)
                 / \
              2.0   4.5
               /     \
    (Nicolas, 40)   (Georgia, 30)
               \     /
              6.0   1.5
                 \ /
             (Luke, 10)
    

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

            (Jonathan, 20)
                 / \
              2.0   4.5
               /     \
    (Nicolas, 40)   (Georgia, 30)
              |       |
             6.0     1.5
              |       |
       (Luke, 10)   (Luke, 10)
    
  4. 我希望这有帮助!