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

  • Home
  • SEARCH
  • 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 7721327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:53:48+00:00 2026-06-01T03:53:48+00:00

I’m trying to write a program that would find the minimum spanning tree. But

  • 0

I’m trying to write a program that would find the minimum spanning tree. But one problem I am having with this algorithm, is testing for a circuit. What would be the best way to do this in java.

Ok here is my code

import java.io.*;
import java.util.*;

public class JungleRoads 
{
    public static int FindMinimumCost(ArrayList graph,int size)
    {
        int total = 0;
        int [] marked = new int[size];      //keeps track over integer in the mst

        //convert an arraylist to an array
        List<String> wrapper = graph;
        String[] arrayGraph = wrapper.toArray(new String[wrapper.size()]);
        String[] temp = new String[size];
        HashMap visited = new HashMap();


        for(int i = 0; i < size; i++)
        {
           // System.out.println(arrayGraph[i]);
            temp = arrayGraph[i].split(" ");

            //loop over connections of a current node
            for(int j =  2; j < Integer.parseInt(temp[1])*2+2; j++)
            {

                if(temp[j].matches("[0-9]+"))
                {
                    System.out.println(temp[j]);
                }
            }


        }


        graph.clear();
        return total;


    }


    public static void main(String[] args) throws IOException
    {

         FileReader fin = new FileReader("jungle.in");
        BufferedReader infile = new BufferedReader(fin);

        FileWriter fout = new FileWriter("jungle.out");
        BufferedWriter outfile = new BufferedWriter(fout);


        String line;
        line = infile.readLine();
        ArrayList graph = new ArrayList();

        do
        {

            int num = Integer.parseInt(line);
            if(num!= 0)
            {

                int size = Integer.parseInt(line)-1;

                for(int i=0; i < size; i++)
                {
                    line = infile.readLine(); 
                    graph.add(line);
                }

               outfile.write(FindMinimumCost(graph, size));
            }   


            line = infile.readLine();
        }while(!line.equals("0"));

    }
}
  • 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-01T03:53:49+00:00Added an answer on June 1, 2026 at 3:53 am

    Kruskall’s algorithm doesn’t searche for cycles, because it’s not performance efficient; instead creates disjoint trees, and then connects them. Since connecting two distinct subtrees with a single edge creates a new tree, there is no need to check for cycles.

    If you look at wiki page algorithm is as follow:

    1. create a forest **F** (a set of trees), where each vertex in the graph is a separate tree
    2. create a set S containing all the edges in the graph
    3. while S is nonempty and F is not yet spanning
        a. remove an edge with minimum weight from S
        b. if that edge connects two different trees, then add it to the forest, combining 
           two trees into a single tree
        c. otherwise discard that edge.
    

    You should use Disjoint Set Data Structure for this. again from wiki:

    first sort the edges by weight using a comparison sort in O(E log E)
    time; this allows the step "remove an edge with minimum weight from S"
    to operate in constant time. Next, we use a disjoint-set data
    structure (Union&Find) to keep track of which vertices are in which
    components. We need to perform O(E) operations, two ‘find’ operations
    and possibly one union for each edge. Even a simple disjoint-set data
    structure such as disjoint-set forests with union by rank can perform
    O(E) operations in O(E log V) time. Thus the total time is O(E log E)
    = O(E log V).


    #Creating Disjoint Forests
    Now you can take a look at Boost Graph Library-Incremental Components part.
    You should implement some methods: MakeSet, Find, Union, After that you can implement Kruskall’s algorithm. All you doing is working with sets, and simplest possible way to do so is using linked list.

    Each set has one element named as representative element which is the first element in the set.

    1- First implement MakeSet by linked lists:

    This prepares the disjoint-sets data structure for the incremental
    connected components algorithm by making each vertex in the graph a
    member of its own component (or set).

    Just initialize each vertex (element) as a representative element of new set, we can do this by setting them as themselves’ parent:

     function MakeSet(x)
       x.parent := x
    

    2- Implement Find method:

    Find representative element of a set that contains a vertex x:

     function Find(x)
     if x.parent == x
        return x
     else
        return Find(x.parent)
    

    The if part checks the element is representative element or not. we set all representative elements of sets as their first element by setting them as themselves parent.

    3- And finally when all previous steps are done, simple part is implementing the Union method:

    function Union(x, y)
     xRoot := Find(x) // find representative element of first element(set)
     yRoot := Find(y) // find representative element of second element(set)
     xRoot.parent := yRoot // set representative element of first set 
                           // as same as representative element of second set
    

    Now how you should run Kruskall?

    First put all nodes in n disjoint sets by MakeSet method. In each iteration after finding desired edge (not marked and minimal one), find related sets of its endpoint vertices by Find method (their representative elements), if they are the same, drop this edge out because this edge causes a cycle, but If they are in different sets, use Union method to merge these sets. Since each set is a tree their union is a tree.

    You can optimize this by choosing better data structure for disjoint sets, but for now I think this is enough. If you are interested in more advanced data structures, you can implement rank base method, there is a good documentation about it in wiki, it’s easy but I didn’t mention it to prevent from bewilderment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.