2017-11-16 26 views
0

私はGuava ValueGraphの簡単な例を探しています。以下のような何か: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. ノードをノードに追加するにはどうすればよいですか?&エッジをグラフに追加するにはどうすればよいですか?
  3. カスタムノードクラスのequals & hashCodeメソッドをオーバーライドする必要がありますか?

単純な例が非常に役に立ちます。

+1

[グラフ、Guava wikiで説明](https://github.com/google/guava/wiki/GraphsExplained)を読んだことがありますか? – Xaerxess

答えて

0

Guava wikiValueGraphを使用するため、次の例を示します:

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. 行う方法カスタムオブジェクトをノードとしてグラフを作成しますか?後で、このクラスのオブジェクトと値グラフを作成する

    class GraphNode { 
        String name; 
        int age; 
    
        GraphNode(String name, int age) { 
        this.name = Objects.requireNonNull(name, "name"); 
        this.age = 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. ノードを追加するにはどうすればよいですか?&グラフにエッジを追加するにはどうすればよいですか?

    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. 私は対等にカスタムノードクラスの& hashCodeメソッドをオーバーライドする必要がありますか?

    そうでなければ、次のコード例では、グラフが期待どおりに表示されない可能性があるため、非常にお勧めします。 GraphNodeでカスタム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); 
    

    、以下の期待される形状を生成しますグラフ:

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

    しかしequals()hashCode()せず、値グラフは語ることができませんその2 new GraphNode("Luke", 10) sは論理的に同じノードなので、次のような誤った形状が生成されます。

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

こちらがお役に立てば幸いです。

+0

詳細な説明をありがとうございます。本当に助けに感謝します。 –