0

I wrote a java program to calculate the minimum spanning tree with randomly generated 100 vertices and randomly generated 800 edges. I will like to plot the graph that this program generates whenever I run it. Does anybody know of any tool that can help with this ?? My Java code below:

public static void main (String [] args)
{
    Random random = new Random();
    Edge[] edges = new Edge[800];
    for(int i = 0; i < edges.length; i++) {
       edges[i] = new Edge(
       Integer.toString(random.nextInt(100)),
       Integer.toString(random.nextInt(100)),
       random.nextInt(100) //weights from 0 to 99
       );
    }

    System.out.println("Graph");
    KEdges vv = new KEdges();

    for (Edge edge : edges) {
        System.out.println(edge);
        vv.insertEdge(edge);
    }
     System.out.println("Implementing 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<Edge>
 {
    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 < o.weight)? -1 : 1;
    }

}

static class KEdges 
{
    Vector<HashSet<String>>  vertexGroups = new Vector<HashSet<String>>();
    TreeSet<Edge> kruskalEdges = new TreeSet<Edge>();

    public TreeSet<Edge> getEdges()
    {
        return kruskalEdges;
    }

    public HashSet<String> getVertexGroup(String vertex)
    {
        for (HashSet<String> vertexGroup : vertexGroups)
        {
            if (vertexGroup.contains(vertex))
            {
                return vertexGroup;
            }
        }
        return null;
    }

  public void insertEdge(Edge edge)
  {
    String vertexA = edge.getVertexA();
    String vertexB = edge.getVertexB();

    HashSet<String> vertexGroupA = getVertexGroup(vertexA);
    HashSet<String> vertexGroupB = getVertexGroup(vertexB);

    if (vertexGroupA == null)
    {
        kruskalEdges.add(edge);
        if (vertexGroupB == null){
            HashSet<String> htNewVertexGroup = new HashSet<String>();
            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);
         }
    }
  }

}
saopayne
  • 179
  • 3
  • 15

2 Answers2

2

Take a look at JUNG or GraphViz and its java port Grappa.

I normally use JUNG

GrahamA
  • 5,875
  • 29
  • 39
1

I have had good experience with Prefuse. In particular, the integration of foreign data models.

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
  • Can I produce this network visualization in some hours?? How hard can it get to integrate use with my code above? – saopayne Dec 19 '13 at 09:49
  • Possible. It depend on your skills. Try to look around for some simple examples [1](https://docs.google.com/document/d/1w-8dF6GtYnlL_IftWA0epgTsaIsuufCFTkSPsSFY-30/edit) [2](http://www.cs.mun.ca/~hoeber/teaching/cs4767/notes/04-prefuse/) [3](http://stackoverflow.com/a/7600982/1725096). – Jens Piegsa Dec 19 '13 at 23:10