ノードを追加するにはどうすればよいですか?&グラフにエッジを追加するにはどうすればよいですか?
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)
私は対等にカスタムノードクラスの& 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)
[グラフ、Guava wikiで説明](https://github.com/google/guava/wiki/GraphsExplained)を読んだことがありますか? – Xaerxess