Exemplo n.º 1
0
  public static String run(DefaultDirectedGraph<CFGHandle, DefaultEdge> graph) {
    StringBuilder result = new StringBuilder();

    result.append("digraph stronglyConnected {\n");
    for (final CFGHandle cfgh : graph.vertexSet())
      result.append('"').append(cfgh.toString()).append("\"\n");
    for (DefaultEdge edge : graph.edgeSet())
      result
          .append('"')
          .append(graph.getEdgeSource(edge))
          .append("\" -> \"")
          .append(graph.getEdgeTarget(edge))
          .append("\";\n");
    result.append('}');

    return result.toString();
  }
Exemplo n.º 2
0
  /** Rigourous Test :-) */
  public void testBFS() {

    DefaultDirectedGraph<Vertex, Double> graph =
        new DefaultDirectedGraph<Vertex, Double>(Double.class);
    Vertex v = null;
    for (int i = 0; i < this.numOfVertices; i++) {
      v = new Vertex("v" + i);
      graph.addVertex(v);
    }

    // use last vertex as start point of search
    Vertex lastVertex = v;

    Set<Vertex> vertices = graph.vertexSet();

    // add edges to graph
    Random rand = new Random();
    Double nextEdge = 0.0;
    for (Vertex start : vertices)
      for (Vertex end : vertices)
        if (!start.equals(end) && !graph.containsEdge(start, end)) {
          nextEdge = rand.nextDouble();
          if (nextEdge > 0.9) graph.addEdge(start, end, nextEdge);
        }

    System.out.println("Vertices: " + vertices.size());
    System.out.println("Edges: " + graph.edgeSet().size());

    BreadthFirstIterator<Vertex, Double> bfsIter =
        new BreadthFirstIterator<Vertex, Double>(graph, lastVertex);

    BFS<Double> bfs = new BFS<Double>(graph);
    Vertex[] bfsResult = bfs.breadthFirstTree(lastVertex);

    for (int i = 0; i < this.numOfVertices && bfsIter.hasNext(); i++) {
      System.out.println(bfsResult[i] + " " + bfsIter.next());
    }
  }