[英]How can I create multiple edges with the same weight and same source vertex?
I'm using jgrapht and I'm having a problem here. 我正在使用jgrapht,在这里遇到问题。 As soon as I create two edges with the same weight from one source vertex, I end up having just one edge:
从一个源顶点创建具有相同权重的两个边后,我最终只有一个边:
DirectedWeightedPseudograph<Object, Object> Grph1 = new DirectedWeightedPseudograph<>(Object.class);
Grph1.addVertex("a");
Grph1.addVertex("b");
Grph1.addVertex("c");
Grph1.addEdge("a", "b", "55");
Grph1.addEdge("a", "c", "55");
I expect the output: 我期望输出:
a--55-->b
a--55-->c
But the actual output: 但是实际输出:
a--55-->b
c
You are not creating two edges. 您没有创建两个边缘。 You are only creating one edge, and this edge is
"55"
. 您仅创建了一条边,该边为
"55"
。 Right: The string "55"
is the edge in your example. 右:字符串
"55"
是示例中的边缘。
In order to create multiple edges with real weights (pun intended), you should declare your graph to have the right type, namely one that uses, for example, DefaultWeightedEdge
as the edge type. 为了创建具有真实权重的多个边缘(预期为双关语),应声明图形具有正确的类型,即使用例如
DefaultWeightedEdge
作为边缘类型的图形。
Then, when you add a new edge by calling addEdge
, you receive the DefaultWeightedEdge
instance. 然后,当通过调用
addEdge
添加新边缘时,将收到DefaultWeightedEdge
实例。 Using this instance, you can assign the weight to this edge, by calling setEdgeWeight
. 使用此实例,可以通过调用
setEdgeWeight
将权重分配给该边缘。
Graph<String, DefaultWeightedEdge> g = new DirectedWeightedPseudograph<>(DefaultWeightedEdge.class);
g.addVertex("a");
g.addVertex("b");
g.addVertex("c");
DefaultWeightedEdge e0 = g.addEdge("a", "b");
g.setEdgeWeight(e0, 55.0);
DefaultWeightedEdge e1 = g.addEdge("a", "c");
g.setEdgeWeight(e1, 55.0);
System.out.println(g.edgeSet());
From the documentation: 从文档中:
adds the specified edge e to this graph if this graph contains no edge e2 such that e2.equals(e)
如果此图不包含边e2,则将指定的边e添加到该图,使得e2.equals(e)
Object o1=Grph1.addEdge("a", "b", "55");
Object o2=Grph1.addEdge("a", "c", "55");
boolean test=o1.equals(o2); //this will return true
Instead of creating your graph with <Object,Object>
, use something different, eg <String,DefaultWeightedEdge>
as is done in the examples: 不用使用
<Object,Object>
创建图形,而是使用不同的东西,例如<String,DefaultWeightedEdge>
如示例中所示:
Graph<String,DefaultWeightedEdge> g=new DirectedWeightedPseudograph<>(DefaultWeightedEdge.class);
g.addVertex("a");
g.addVertex("b");
g.addVertex("c");
g.addEdge("a", "b", "55");
g.addEdge("a", "c", "55");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.