图形可视化

时间:2013-12-19 08:38:25

标签: java graph visualization minimum-spanning-tree

我写了一个java程序,用随机生成的100个顶点和随机生成的800个边来计算最小生成树。我想在运行它时绘制该程序生成的图形。 有没有人知道任何可以帮助这个的工具? 我的Java代码如下:

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);
         }
    }
  }

}

2 个答案:

答案 0 :(得分:2)

查看JUNG或GraphViz及其java端口Grappa

我通常使用JUNG

答案 1 :(得分:1)

我对Prefuse有很好的经验。特别是国外数据模型的整合。