nyfer
nyfer

Reputation: 95

Create graph using Jgraph

I want to create a graph using Jgraph, and later use that graph to find the minimum spanning Tree.

How to create a graph using Jgraph?.


this is what I have implemented. Can you please tell me how to use kruskals algorithm from the package. I googled it, but couldn't find any information on it.

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;
    }
}

Upvotes: 2

Views: 12220

Answers (1)

nyfer
nyfer

Reputation: 95

This the main class where user input such as no of edges and vertices is taken to create a graph to the Spanning tree of the graph created. Below is the complete answer to this question.

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();
    }
}

Below MyGraph class does all the work of creating the graph by taking edges and calculating the spanning tree. I have used jgrapht library to create graph

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());
    }*/ 
}

Upvotes: 2

Related Questions