4

我想定义一个自定义边缘

public class Connection extends DefaultWeightedEdge

但是当我尝试使用时,super()这可能吗?创建图表时我应该为 EdgeFactory 使用什么?

new SimpleWeightedGraph<Sensor, Connection>(EdgeFactory.class);

这足够了吗?或者我应该创建一个也扩展的新类EdgeFactory

4

2 回答 2

7

您不需要子类DefaultWeightedEdge来创建您的自定义边缘,以下工作也可以。我目前正在使用 0.9.0 版的 JGraphT。

类定义:

public class Connection {
    // class implementation
    // define all constructors and methods you need
}

图表创建:

SimpleWeightedGraph<Sensor, Connection> graph;
graph = new SimpleWeightedGraph<Sensor, Connection>(Connection.class);

添加自定义边缘和设置权重:

Sensor v1 = // sensor instance
Sensor v2 = // another sensor instance
graph.addVertex(v1);
graph.addVertex(v2);

Connection connection = new Connection(/* ... */);
graph.addEdge(v1, v2, connection);
graph.setEdgeWeight(connection, 10.0);

还要考虑实现类的方法equalshashCode方法Connection,因为 JGraphT 自 0.8.4 版起就依赖于这些方法,请参阅EqualsAndHashCode文章。本文还提供了一个DefaultWeightedEdge子类化的示例。

于 2014-10-31T07:49:44.893 回答
1

这是否有助于您创建自定义边缘?

这个特定的例子取自 JGrapht 内部文档。在代码库中搜索 StoerWagnerMinimumCut 算法。

开始了 。

这里的想法是为图的边缘赋予一些权重,因此需要操纵默认权重。

final WeightedGraph<Set<V>, DefaultWeightedEdge> workingGraph;
....

workingGraph =
            new SimpleWeightedGraph<Set<V>, DefaultWeightedEdge>(
                DefaultWeightedEdge.class);
list = ....
workingGraph.addVertex(list);
.... 

// 现在让我们操作边缘权重

DefaultWeightedEdge eNew = workingGraph.getEdge(someSourcEdge, someTargetEdge);
        if (eNew == null) {
            eNew = workingGraph.addEdge(someSourcEdge, someTargetEdge);
            workingGraph.setEdgeWeight(eNew, graph.getEdgeWeight(e));
        } else {
            workingGraph.setEdgeWeight(
                eNew,
                workingGraph.getEdgeWeight(eNew) + graph.getEdgeWeight(e));

我希望您可以使用该示例并使用它来创建自定义边缘。

PS :我的回答不是 100% 正确,但我的想法是在文档中为您指出正确的方向 :-)

于 2014-07-02T06:14:08.590 回答