Java:JGraphT 的最小生成树?

发布于 2024-08-11 20:23:47 字数 86 浏览 6 评论 0原文

我有一个问题,本质上可以看作是一个图表。我正在考虑使用 JGraphT 来实现它,而不是自己动手。使用 JGraphT 从图中获取最小生成树的最佳方法是什么?

I have a problem that can essentially be viewed as a graph. I'm considering using JGraphT to implement it instead of rolling my own. What would be the best way to get a minimum spanning tree out of a graph using JGraphT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱要勇敢去追 2024-08-18 20:23:48

不幸的是,我不知道足够的图论来给你一个完整、直接的答案,但我在一些项目中使用过 jgrapht,所以也许这会有所帮助。

jgrapht 包含的算法列表如下: http:// www.jgrapht.org/javadoc/org/jgrapht/alg/package-summary.html,您还可以在这里找到作为迭代器实现的图遍历(如果有帮助的话):http://www.jgrapht.org/javadoc/org/jgrapht/traverse/package-summary .html

我很确定这些算法都不能让你开箱即用,所以你必须自己编码,但我可以根据经验告诉你,在 jgrapht 之上编码为与从头开始相比要容易得多。还有一个 FibonacciHeap 类可能有助于实现普里姆算法。

如果您需要算法本身的帮助,维基百科条目中有许多算法,其中包含详细的描述和伪代码。 最小生成树文章链接到它们。

Unfortunately, I don't know enough graph theory to give you a complete, direct answer, but I have used jgrapht in a few projects, so maybe this will help.

The list of algorithms included with jgrapht is here: http://www.jgrapht.org/javadoc/org/jgrapht/alg/package-summary.html, and you can also find graph traversals implemented as iterators (if that is any help) here: http://www.jgrapht.org/javadoc/org/jgrapht/traverse/package-summary.html

I'm pretty sure none of those algorithms will get you want you want out of the box, so you'd have to code it yourself, but I can tell you from experience that coding on top of jgrapht as opposed to starting from scratch is A LOT easier. There is also a FibonacciHeap class that would presumably help with implementing Prim's algorithm.

If you need help with the algorithm itself, there are a number of algorithms with Wikipedia entries, with detailed descriptions and pseudocode. The Minimum spanning tree article links to them.

花开浅夏 2024-08-18 20:23:48

Jung 对此有一个实现。

Jung has an implementation of this.

前事休说 2024-08-18 20:23:48

ClosestFirstIterator 可能会帮助你。它使用 FibonacciHeap 构建生成树,同时迭代顶点。

ClosestFirstIterator 提供了 getShortestPathLength() 和 getSpanningTreeEdge() 方法来获取最小生成树的部分内容。

// instantiate the iterator
ClosestFirstIterator<V,E> it = new ClosestFirstIterator<V,E>(graph, start_vertex);

// iterate to build the tree
while(it.hasNext())
  it.next();

// query
double distance = getShortestPathLength(vertex);
E next_edge = getSpanningTreeEdge(vertex);

ClosestFirstIterator may help you out. It builds a spanning tree using FibonacciHeap while iterating over the vertices.

ClosestFirstIterator provides the methods getShortestPathLength() and getSpanningTreeEdge() to get parts of the minimum spanning tree.

// instantiate the iterator
ClosestFirstIterator<V,E> it = new ClosestFirstIterator<V,E>(graph, start_vertex);

// iterate to build the tree
while(it.hasNext())
  it.next();

// query
double distance = getShortestPathLength(vertex);
E next_edge = getSpanningTreeEdge(vertex);
浅沫记忆 2024-08-18 20:23:48

最新版本的 JGraphT 库对 MST 算法有多种选择,无需从头开始实现。

以下代码适用于 Java 8 和 JGraphT 版本 1.3.0。它使用 (a) Prim、(b) Kruskal 和 (c) Borůvka 的算法计算小图的最小生成树。有关不同算法的详细信息,请参阅有关 mst 问题的维基百科文章。

package org.myorg.mstdemo;

import org.jgrapht.Graph;
import org.jgrapht.alg.spanning.BoruvkaMinimumSpanningTree;
import org.jgrapht.alg.spanning.KruskalMinimumSpanningTree;
import org.jgrapht.alg.spanning.PrimMinimumSpanningTree;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.builder.GraphTypeBuilder;
import org.jgrapht.util.SupplierUtil;

/**
 * MST demo
 */
public class App {

    public static void main(String[] args) {

        Graph<String, DefaultWeightedEdge> graph = GraphTypeBuilder
                .undirected()
                .weighted(true)
                .allowingMultipleEdges(false)
                .allowingSelfLoops(false)
                .vertexSupplier(SupplierUtil.createStringSupplier())
                .edgeSupplier(SupplierUtil.createDefaultWeightedEdgeSupplier())
                .buildGraph();

        String v0 = graph.addVertex();
        String v1 = graph.addVertex();
        String v2 = graph.addVertex();

        DefaultWeightedEdge e1 = graph.addEdge(v0, v1);
        DefaultWeightedEdge e2 = graph.addEdge(v1, v2);
        DefaultWeightedEdge e3 = graph.addEdge(v0, v2);

        graph.setEdgeWeight(e1, 100d);
        graph.setEdgeWeight(e2, 5d);
        graph.setEdgeWeight(e3, 1d);

        System.out.println("Prim:");
        for(DefaultWeightedEdge e: new PrimMinimumSpanningTree<>(graph).getSpanningTree()) { 
            System.out.println(e);
        }

        System.out.println("Kruskal:");
        for(DefaultWeightedEdge e: new KruskalMinimumSpanningTree<>(graph).getSpanningTree()) { 
            System.out.println(e);
        }

        System.out.println("Borůvka:");
        for(DefaultWeightedEdge e: new BoruvkaMinimumSpanningTree<>(graph).getSpanningTree()) { 
            System.out.println(e);
        }

    }

}

Latest versions of the JGraphT library have various choices for an MST algorithm, no need to implement one from scratch.

The following code works with Java 8 and JGraphT version 1.3.0. It computes the minimum spanning tree of a small graph using the algorithms by (a) Prim, (b) Kruskal and (c) Borůvka. Details about the different algorithms can be found in the wikipedia article about the mst problem.

package org.myorg.mstdemo;

import org.jgrapht.Graph;
import org.jgrapht.alg.spanning.BoruvkaMinimumSpanningTree;
import org.jgrapht.alg.spanning.KruskalMinimumSpanningTree;
import org.jgrapht.alg.spanning.PrimMinimumSpanningTree;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.builder.GraphTypeBuilder;
import org.jgrapht.util.SupplierUtil;

/**
 * MST demo
 */
public class App {

    public static void main(String[] args) {

        Graph<String, DefaultWeightedEdge> graph = GraphTypeBuilder
                .undirected()
                .weighted(true)
                .allowingMultipleEdges(false)
                .allowingSelfLoops(false)
                .vertexSupplier(SupplierUtil.createStringSupplier())
                .edgeSupplier(SupplierUtil.createDefaultWeightedEdgeSupplier())
                .buildGraph();

        String v0 = graph.addVertex();
        String v1 = graph.addVertex();
        String v2 = graph.addVertex();

        DefaultWeightedEdge e1 = graph.addEdge(v0, v1);
        DefaultWeightedEdge e2 = graph.addEdge(v1, v2);
        DefaultWeightedEdge e3 = graph.addEdge(v0, v2);

        graph.setEdgeWeight(e1, 100d);
        graph.setEdgeWeight(e2, 5d);
        graph.setEdgeWeight(e3, 1d);

        System.out.println("Prim:");
        for(DefaultWeightedEdge e: new PrimMinimumSpanningTree<>(graph).getSpanningTree()) { 
            System.out.println(e);
        }

        System.out.println("Kruskal:");
        for(DefaultWeightedEdge e: new KruskalMinimumSpanningTree<>(graph).getSpanningTree()) { 
            System.out.println(e);
        }

        System.out.println("Borůvka:");
        for(DefaultWeightedEdge e: new BoruvkaMinimumSpanningTree<>(graph).getSpanningTree()) { 
            System.out.println(e);
        }

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文