Ejemplo n.º 1
0
  /**
   * Constructs the shortest path array for the given graph.
   *
   * @param g input graph
   * @param <V> a V object.
   * @param <E> a E object.
   */
  public FloydWarshall(Graph<V, E> g) {
    int sz = g.vertexSet().size();
    d = new double[sz][sz];
    indices = new HashMap<V, Integer>();

    // Initialise distance to infinity, or the neighbours weight, or 0 if
    // same
    for (V v1 : g.vertexSet()) {
      for (V v2 : g.vertexSet()) {
        if (v1 == v2) {
          d[index(v1)][index(v2)] = 0;
        } else {
          E e = g.getEdge(v1, v2);

          if (e == null) {
            d[index(v1)][index(v2)] = Double.POSITIVE_INFINITY;
          } else {
            d[index(v1)][index(v2)] = g.getEdgeWeight(e);
          }
        }
      }
    }

    // now iterate k times
    for (int k = 0; k < sz; k++) {
      for (V v1 : g.vertexSet()) {
        for (V v2 : g.vertexSet()) {
          d[index(v1)][index(v2)] =
              Math.min(d[index(v1)][index(v2)], d[index(v1)][k] + d[k][index(v2)]);
          if (Double.POSITIVE_INFINITY != d[index(v1)][index(v2)])
            diameter = Math.max(diameter, d[index(v1)][index(v2)]);
        }
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * @param graph the graph to be ordered
   * @param orderByDegree should the vertices be ordered by their degree. This speeds up the VF2
   *     algorithm.
   * @param cacheEdges if true, the class creates a adjacency matrix and two arrays for incoming and
   *     outgoing edges for fast access.
   */
  public GraphOrdering(Graph<V, E> graph, boolean orderByDegree, boolean cacheEdges) {
    this.graph = graph;
    this.cacheEdges = cacheEdges;

    List<V> vertexSet = new ArrayList<>(graph.vertexSet());
    if (orderByDegree) {
      java.util.Collections.sort(vertexSet, new GeneralVertexDegreeComparator<>(graph));
    }

    vertexCount = vertexSet.size();
    mapVertexToOrder = new HashMap<>();
    mapOrderToVertex = new ArrayList<>(vertexCount);

    if (cacheEdges) {
      outgoingEdges = new int[vertexCount][];
      incomingEdges = new int[vertexCount][];
      adjMatrix = new Boolean[vertexCount][vertexCount];
    }

    Integer i = 0;
    for (V vertex : vertexSet) {
      mapVertexToOrder.put(vertex, i++);
      mapOrderToVertex.add(vertex);
    }
  }
Ejemplo n.º 3
0
  /**
   * Creates a new iterator for the specified graph. Iteration will start at the specified start
   * vertex. If the specified start vertex is <code>
   * null</code>, Iteration will start at an arbitrary graph vertex.
   *
   * @param g the graph to be iterated.
   * @param startVertex the vertex iteration to be started.
   * @throws IllegalArgumentException if <code>g==null</code> or does not contain <code>startVertex
   *     </code>
   */
  public CrossComponentIterator(Graph<V, E> g, V startVertex) {
    super();

    if (g == null) {
      throw new IllegalArgumentException("graph must not be null");
    }
    graph = g;

    specifics = createGraphSpecifics(g);
    vertexIterator = g.vertexSet().iterator();
    setCrossComponentTraversal(startVertex == null);

    reusableEdgeEvent = new FlyweightEdgeEvent<V, E>(this, null);
    reusableVertexEvent = new FlyweightVertexEvent<V>(this, null);

    if (startVertex == null) {
      // pick a start vertex if graph not empty
      if (vertexIterator.hasNext()) {
        this.startVertex = vertexIterator.next();
      } else {
        this.startVertex = null;
      }
    } else if (g.containsVertex(startVertex)) {
      this.startVertex = startVertex;
    } else {
      throw new IllegalArgumentException("graph must contain the start vertex");
    }
  }
Ejemplo n.º 4
0
  /** {@inheritDoc} */
  public void generateGraph(
      Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap) {
    if (size < 1) {
      return;
    }

    // Add all the vertices to the set
    for (int i = 0; i < size; i++) {
      V newVertex = vertexFactory.createVertex();
      target.addVertex(newVertex);
    }

    /*
     * We want two iterators over the vertex set, one fast and one slow.
     * The slow one will move through the set once. For each vertex,
     * the fast iterator moves through the set, adding an edge to all
     * vertices we haven't connected to yet.
     *
     * If we have an undirected graph, the second addEdge call will return
     * nothing; it will not add a second edge.
     */
    Iterator<V> slowI = target.vertexSet().iterator();
    Iterator<V> fastI;

    while (slowI.hasNext()) { // While there are more vertices in the set

      V latestVertex = slowI.next();
      fastI = target.vertexSet().iterator();

      // Jump to the first vertex *past* latestVertex
      while (fastI.next() != latestVertex) {;
      }

      // And, add edges to all remaining vertices
      V temp;
      while (fastI.hasNext()) {
        temp = fastI.next();
        target.addEdge(latestVertex, temp);
        target.addEdge(temp, latestVertex);
      }
    }
  }
Ejemplo n.º 5
0
 /**
  * Creates a new labels graph according to the regular graph. After its creation they will no
  * longer be linked, thus changes to one will not affect the other.
  *
  * @param regularGraph
  */
 public GraphOrdering(Graph<V, E> regularGraph) {
   this(regularGraph, regularGraph.vertexSet(), regularGraph.edgeSet());
 }
 public FloydWarshallShortestPaths(Graph<V, E> graph) {
   this.graph = graph;
   this.vertices = new ArrayList<V>(graph.vertexSet());
 }
 /**
  * Creates an object to calculate shortest paths between the start vertex and others vertices
  * using the Bellman-Ford algorithm.
  *
  * @param graph
  * @param startVertex
  */
 public BellmanFordShortestPath(Graph<V, E> graph, V startVertex) {
   this(graph, startVertex, graph.vertexSet().size() - 1);
 }