Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7867047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:36:09+00:00 2026-06-03T00:36:09+00:00

I want to create a graph using Jgraph, and later use that graph to

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:36:11+00:00Added an answer on June 3, 2026 at 12:36 am

    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());
        }*/ 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a graph of items on a certain day using data
I want to create a Login through Facebook on a website using Graph API
I'm using packages subfigure and float to create figures and tables that I want
I have faced a problem when using PLY. I want to create a call
I want to create a program that creates a graph (to be specific a
I want to create a matrix representation for a graph algorithm using the least
I want to create events on behalf of the user using the Graph API.
Using the Graph Facebook API with this code [facebook requestWithGraphPath:@me/friends andDelegate:self]; Now I have
I want to create a graph using JFreeChart and display it in a java
I am using EASYGRAPH for to create graph in iphone app problem is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.