title: Randomly generate edges and vertices
body: <p>I'm learning minimum spanning trees and I encountered this question and decided to give it a shot...</p>
  
  <blockquote>
    <p>Implement the minimum spanning tree algorithm 
     on a randomly generated directed graph network of 100 vertices and 800 edges</p>
  </blockquote>
  
  <pre><code>public static int[][] getRandomArray(int n){
      int[][] a = new int[n][n];
      Random r = new Random();
  
      for(int i = 0; i &lt; a.length; i++){
          for(int j = 0; j &lt; a[i].length; j++){
              a[i][j] = r.nextInt();
          }
      }
  
      return a;
  }
  
  
  public static void main (String [] args)
  {
     int x[];
       //TreeSet is used to sort the edges before passing to the algorithm
      TreeSet&lt;Edge&gt;  edges = new TreeSet&lt;Edge&gt;();
  
      Random random = new Random();
  
      edges.add(new Edge("0", "1", 2)); 
      edges.add(new Edge("0", "3", 1)); 
      edges.add(new Edge("1", "2", 3)); 
      edges.add(new Edge("2", "3", 5)); 
  
      System.out.println("Graph");
      KEdges vv = new KEdges();
  
      for (Edge edge : edges) {
          System.out.println(edge);
          vv.insertEdge(edge);
      }
  
      System.out.println("Kruskal algorithm");
      int total = 0;
      for (Edge edge : vv.getEdges()) {
          System.out.println(edge);
          total += edge.getEdgeWeight();
      }
      System.out.println("Total weight is " + total);
  }
  
  
  static class Edge implements Comparable&lt;Edge&gt;
  {
      String vertexA;
      String vertexB;
      int weight;
  
      public Edge(String vertexA, String vertexB, int weight)
      {
          this.vertexA = vertexA;
          this.vertexB = vertexB;
          this.weight = weight;
      }
  
      public String getVertexA()
      {
          return vertexA;
      }
  
      public String getVertexB()
      {
          return vertexB;
      }
  
      public int getEdgeWeight()
      {
          return weight;
      }
  
      public String toString()
      {
          return "("+ vertexA + ", " + vertexB + ") : weight "+ weight ;
      }
      @Override
      public int compareTo(Edge o) {
         return (this.weight &lt; o.weight)? -1 : 1;
      }
  
  }
  
  static class KEdges 
  {
      Vector&lt;HashSet&lt;String&gt;&gt;  vertexGroups = new Vector&lt;HashSet&lt;String&gt;&gt;();
      TreeSet&lt;Edge&gt; kruskalEdges = new TreeSet&lt;Edge&gt;();
  
      public TreeSet&lt;Edge&gt; getEdges()
      {
          return kruskalEdges;
      }
  
      public HashSet&lt;String&gt; getVertexGroup(String vertex)
      {
          for (HashSet&lt;String&gt; vertexGroup : vertexGroups)
          {
              if (vertexGroup.contains(vertex))
              {
                  return vertexGroup;
              }
          }
          return null;
      }
  
  
  
    public void insertEdge(Edge edge)
    {
      String vertexA = edge.getVertexA();
      String vertexB = edge.getVertexB();
  
      HashSet&lt;String&gt; vertexGroupA = getVertexGroup(vertexA);
      HashSet&lt;String&gt; vertexGroupB = getVertexGroup(vertexB);
  
      if (vertexGroupA == null)
      {
          kruskalEdges.add(edge);
          if (vertexGroupB == null){
              HashSet&lt;String&gt; htNewVertexGroup = new HashSet&lt;String&gt;();
              htNewVertexGroup.add(vertexA);
              htNewVertexGroup.add(vertexB);
              vertexGroups.add(htNewVertexGroup);
          }
      }
      else{
          if (vertexGroupB == null)
           {
               vertexGroupA.add(vertexB);
               kruskalEdges.add(edge);
           }
          else if (vertexGroupA != vertexGroupB)
           {
          vertexGroupA.addAll(vertexGroupB);
          vertexGroups.remove(vertexGroupB);
          kruskalEdges.add(edge);
           }
      }
    }
  
  }
  </code></pre>
  
  <p>I'm stuck on how to generate the random edges and vertices...
  Any suggestions and ideas...</p>