Graph 番石榴价值图的简单示例
我正在寻找番石榴ValueGraph的简单示例。比如:Graph 番石榴价值图的简单示例,graph,guava,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,
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?
}
}
类图形节点{
字符串名;
字符串值;
//我需要在这里重写equals和hashcode方法吗??
}
类葡萄采摘器{
public ValueGraph createGraph(){
ValueGraph graph=ValueGraphBuilder.directed.build();
//如何将节点添加到图形??
//如何将边添加到图形中?
}
}
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
稍后将详细介绍如何使用此类对象创建值图
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()
,值图将无法判断两个新图形节点(“Luke”,10)
在逻辑上是相同的节点,因此它将产生以下错误形状:
(Jonathan, 20)
/ \
2.0 4.5
/ \
(Nicolas, 40) (Georgia, 30)
| |
6.0 1.5
| |
(Luke, 10) (Luke, 10)
我希望这有帮助 你读过吗?很好的例子!知道如何将其持久化到DB吗?@Serafins的答案是这样的(特别是,使用图形文件格式-尽可能避免Java序列化想法,因为Java序列化存在有效Java中描述的安全问题)。但是,如果你在阅读后感到困惑,那么可以随意创建一个带有标签[番石榴]的新问题;我一直盯着那个标签,这样我就能找到你的问题,我会尽我所能回答你的问题。:)@jbduncan有没有一种方法可以用上面显示的格式打印图表?@ArunGowda没有,好吧。最接近的方法是找到一个库,或者将番石榴图转换为GraphViz.dot文件,或者使用JGraphT和一起使用,然后使用GraphViz从.dot文件中删除。我希望这有帮助。我想我不清楚。我想在控制台中打印图形结构以进行调试:)。就像你上面所说的。
(Jonathan, 20)
/ \
2.0 4.5
/ \
(Nicolas, 40) (Georgia, 30)
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)
(Jonathan, 20)
/ \
2.0 4.5
/ \
(Nicolas, 40) (Georgia, 30)
| |
6.0 1.5
| |
(Luke, 10) (Luke, 10)