1

我正在寻找一个类,jgrapht它允许我动态地构造一个图,并在运行时根据算法的分支在给定的图中生成新的边。我需要实现我看到这个类的算法分支和界限ListenableDirectedWeightedGraph

是否可以通过使用带有 的循环将边添加到图形中ListenableDirectedWeightedGraph

4

1 回答 1

0

我使用以下代码构建图表:

private void fillGraph(Tree tree, Graph<Vertex, Edge> graph)
{
    Vertex root = tree.getRootVertex();
    for (Edge edge : root.getEdges())
        addEdge(edge, graph);
}

private void addEdge(Edge edge, Graph<Vertex, Edge> graph)
{
    Vertex source = edge.getSource();
    Vertex target = edge.getTarget();
    if (!graph.containsVertex(source))
        graph.addVertex(source);
    graph.addVertex(target);
    graph.addEdge(source, target, edge);
    if (!target.getEdges().isEmpty())
        for (Edge e: target.getEdges())
            addEdge(e, graph);
}
于 2012-02-23T11:06:06.940 回答