title: implementing kruskals algorithm in java
body: <p>im able to run this code for some input.
  but in some cases i get the wrong spanning tree.
  for eg:
  if i give the input as follows while executing the program :</p>
  
  <p>Enter no.of vertices:
      5
       Enter no.of edges:
      8</p>
  
  <pre><code>    Enter the vertices and the weight of edge 1:
      1
      3
      10
  
   Enter the vertices and the weight of edge 2:
  1
  4
  100
   Enter the vertices and the weight of edge 3:
  3
  5
  64
   Enter the vertices and the weight of edge 4:
  1
  2
  13
   Enter the vertices and the weight of edge 5:
  3
  2
  20
   Enter the vertices and the weight of edge 6:
  2
  5
  5
   Enter the vertices and the weight of edge 7:
  4
  3
  80
   Enter the vertices and the weight of edge 8:
  4
  5
  40
  MINIMUM SPANNING TREE :
  2-5
  1-3
  4-5
  MINIMUM COST = 55
  
  expected o/p :
  
   MINIMUM SPANNING TREE :
      2-5
      1-3
      1-2
      4-5
      MINIMUM COST = 68
  </code></pre>
  
  <p>kindly help me out to rectify my mistake...
  pls tel me wat changes shud i make in the code.. plssss</p>
  
  <p>the code is as follows :</p>
  
  <pre><code>import java.io.*;  
  class Edge
  {
   int v1,v2,wt;   // wt is the weight of the edge
  }
  class kruskalsalgo
  {
  public static void main(String args[])throws IOException 
  { 
  int i,j,mincost=0;
  BufferedReader br=new BufferedReader( new InputStreamReader(System.in));
  System.out.println(" Enter no.of vertices:");
  int v=Integer.parseInt(br.readLine());
  System.out.println(" Enter no.of edges:");
  int e=Integer.parseInt(br.readLine());
   Edge ed[]=new Edge[e+1];
  for(i=1;i&lt;=e;i++)
  {
   ed[i]=new Edge();
   System.out.println(" Enter the vertices and the weight of edge "+(i)+ ":"); 
   ed[i].v1=Integer.parseInt(br.readLine());
   ed[i].v2=Integer.parseInt(br.readLine());
   ed[i].wt=Integer.parseInt(br.readLine());
  }
  for(i=1;i&lt;=e;i++)      // sorting the edges in ascending order
   for(j=1;j&lt;=e-1;j++)
  {
   if(ed[j].wt&gt;ed[j+1].wt)
   {
     Edge t=new Edge();
      t=ed[j];
      ed[j]=ed[j+1];
      ed[j+1]=t;
  }
  }
  
  int visited[]=new int[v+1];       // array to check whether the vertex is visited or not
  for(i=1;i&lt;=v;i++)
   visited[i]=0;
  System.out.println("MINIMUM SPANNING TREE :");
  
  
  for(i=1;i&lt;=e;i++)
  { 
   if(i&gt;v)
    break;
  else if( visited[ed[i].v1]==0 || visited[ed[i].v2]==0)
   {
      System.out.println(ed[i].v1+ "-"+ ed[i].v2);
      visited[ed[i].v1]=visited[ed[i].v2]=1;
      mincost+=ed[i].wt;
   }
  }
  System.out.println("MINIMUM COST = " +mincost);
  }
  }
  </code></pre>