使用Jgraph创建图形

时间:2011-11-20 07:06:06

标签: java graph

我想使用Jgraph创建一个图形,然后使用该图形来查找最小生成树。

如何使用Jgraph创建图形?


这是我实施的。你能告诉我如何从包中使用kruskals算法。我用谷歌搜索,但找不到任何相关信息。

import org.jgrapht.*;
import org.jgrapht.graph.*;

public class MyGraph {
    UndirectedGraph<String, DefaultEdge> g = new SimpleGraph<String, DefaultEdge>     (DefaultEdge.class);

    public void addVertex(String name)  {
        // name=new String();
        g.addVertex(name);
    }

    public void addEdge(String v1,String v2) {
        g.addEdge(v1, v2);
    }

    public UndirectedGraph<String, DefaultEdge> getGraph() {
        return g;
    }
}

1 个答案:

答案 0 :(得分:2)

这是用户输入(如没有边和顶点)的主类,用于创建图形到创建的图形的生成树。以下是这个问题的完整答案。

import java.util.Scanner;

public class Main {

    public static void main(String args[]) {

        int x;
        Scanner sc=new Scanner(System.in);
        MyGraph my=new MyGraph();
        System.out.println("Enter the no of vertices");
        int no_of_ver=sc.nextInt();

        for(int i=1;i<=no_of_ver;i++) {
            System.out.println("Enter vertex"+i);
            my.addVertex(sc.next());
        }

        do {
            System.out.println("Enter the edges");
            String e1=sc.next();
            String e2=sc.next();
            my.addEdge(e1, e2);
            // my.setEdgeWeight();
            System.out.println("Continue... Yes:1 ********** No:0");
            x=sc.nextInt();
        } while(x==1);

        System.out.println("Graph\n"+my.getGraph().toString());
        System.out.println("\n\n**********Spanning Tree*********");
        my.getSpanningTree();
        // System.out.println("\nSpanning Tree Cost");
        //my.getSpanningTreeCost();
    }
}

MyGraph类下面通过获取边缘和计算生成树来完成创建图形的所有工作。我使用jgrapht库来创建图形

import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.alg.KruskalMinimumSpanningTree;

public class MyGraph {

    private UndirectedGraph<String, DefaultEdge> g = new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);
    static final double DEFAULT_EDGE_WEIGHT=19;
    DefaultWeightedEdge>(DefaultWeightedEdge.class); 
    private DefaultWeightedEdge e1;

    public void addVertex(String name) {
        g.addVertex(name);
        //graph.addVertex(name);
    }

    public void addEdge(String v1,String v2) {
        g.addEdge(v1, v2);
        // e1=graph.addEdge(v1, v2);
    }

    /*public void setEdgeWeight() {
        graph.setEdgeWeight(e1, DEFAULT_EDGE_WEIGHT);          
    }*/

    public UndirectedGraph<String, DefaultEdge> getGraph() {
        return g;
    }

    /*public SimpleWeightedGraph<String,DefaultWeightedEdge> getGraph() {
        return graph;
    }*/

    public void getSpanningTree() {
        KruskalMinimumSpanningTree k=new KruskalMinimumSpanningTree(g);
        System.out.println(k.getEdgeSet().toString());
        //KruskalMinimumSpanningTree k1=new KruskalMinimumSpanningTree(graph);
        //System.out.println(k1.getEdgeSet().toString());   
    }

    /*public void getSpanningTreeCost() {
        KruskalMinimumSpanningTree k=new KruskalMinimumSpanningTree(graph);
        System.out.println(k.getSpanningTreeCost());
    }*/ 
}