简体   繁体   English

如何创建具有相同权重和相同源顶点的多个边?

[英]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.

相关问题 如何添加一些具有相同权重的边缘? - How to add some edges with same weight? 如何为每个jar从相同的源但不同的主类创建多个jar? - How can I create multiple jars from the same source but different main class for each jar? 如何在单个遍历中从一个顶点创建多个边 - How to create multiple edges from one vertex in single traversal 如何从包含顶点和边的文本文件创建图形? - How can I create a graph from Text File containing the vertex and edges? Gremlin的边共享相同的顶点 - Gremlin get edges sharing the same vertex 如何使用 OpenAPI 3.0 创建同一请求的多个版本? - How can I create multiple versions of the same request with OpenAPI 3.0? 为什么要在循环中创建多个具有相同名称的实例? - Why can I create multiple instances with the same name in a loop? 使用Spring Data JPA,如何创建具有多个条件或在同一字段上或的查询? - Using Spring Data JPA, how can I create a query with multiple conditions ORed on same field? JUNG-MouseOver如何更改顶点和边的厚度 - JUNG - How can MouseOver change thicknes of Vertex and Edges 如何使用Configuration Admin为同一类创建多个配置? - How do I create multiple configurations for the same class with Configuration Admin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM