protected Graph<String, DefaultWeightedEdge> createWithBias(boolean negate) {
    Graph<String, DefaultWeightedEdge> g;
    double bias = 1;
    if (negate) {
      // negative-weight edges are being tested, so only a directed graph
      // makes sense
      g = new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
      bias = -1;
    } else {
      // by default, use an undirected graph
      g = new SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
    }

    g.addVertex(V1);
    g.addVertex(V2);
    g.addVertex(V3);
    g.addVertex(V4);
    g.addVertex(V5);

    e12 = Graphs.addEdge(g, V1, V2, bias * 2);

    e13 = Graphs.addEdge(g, V1, V3, bias * 3);

    e24 = Graphs.addEdge(g, V2, V4, bias * 5);

    e34 = Graphs.addEdge(g, V3, V4, bias * 20);

    e45 = Graphs.addEdge(g, V4, V5, bias * 5);

    e15 = Graphs.addEdge(g, V1, V5, bias * 100);

    return g;
  }
  @Override
  public <N extends Node, E extends Edge> Graph<N, E> createGraph(
      VertexFactory<N> vfac, EdgeFactory<N, E> efac) {
    Integer inp =
        InputDialog.getIntegerInput("Game Tree Factory", "No. of levels?", 2, Integer.MAX_VALUE);
    if (inp == null) {
      return null;
    }

    int levels = inp;
    final double levelSep = ((2 << levels - 1) * (5.0 + 5.0)) / (Math.PI * levels);
    final double shift = levels * levelSep;
    Graph<N, E> graph = new SimpleGraph<>(efac);
    List<N> parents = new LinkedList<>();
    List<N> children = new LinkedList<>();
    Node.PropChanger npr = Node.PropChanger.create();

    N n = vfac.createVertex();
    npr.setPosition(n, polarToCartesian(0, 0, shift));
    graph.addVertex(n);
    parents.add(n);

    for (int i = 1; i <= levels; i++) {
      final double curRadius = i * levelSep;
      double th = 0;
      final double thDelta = Math.PI / parents.size();

      for (N p : parents) {
        N c1 = vfac.createVertex();
        N c2 = vfac.createVertex();
        npr.setPosition(c1, polarToCartesian(curRadius, th + 0.5 * thDelta, shift));
        npr.setPosition(c2, polarToCartesian(curRadius, th + 1.5 * thDelta, shift));
        th += 2 * thDelta;
        graph.addVertex(c1);
        graph.addVertex(c2);
        graph.addEdge(p, c1);
        graph.addEdge(p, c2);
        children.add(c1);
        children.add(c2);
      }
      parents = children;
      children = new LinkedList<>();
    }

    return graph;
  }
  /**
   * Returns a graph, each vertex corresponding with an vector in R, and edges connecting vertices
   * (vectors) if they are obtuse or at right angles.
   *
   * @param TOL > 0 decides how close to zero is considered zero.
   */
  public static Graph<Matrix, DefaultEdge> obtuseConnectedGraph(Set<Matrix> R, double TOL) {
    Graph G = new SimpleGraph<>(DefaultEdge.class);
    for (Matrix v : R) G.addVertex(v); // graph contains the relevant vectors
    int N = R.size();
    Matrix[] b = new Matrix[N];
    R.toArray(b); // build an array with pointers to vectors in R (also vertices in G)

    // add edges between obtuse relevant vectors.
    for (int i = 0; i < N; i++)
      for (int j = i + 1; j < N; j++) if (dot(b[i], b[j]) < Math.abs(TOL)) G.addEdge(b[i], b[j]);

    return G;
  }
  /** {@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);
      }
    }
  }
Esempio n. 5
0
  /** {@inheritDoc} */
  public void generateGraph(
      Graph<V, E> target, final VertexFactory<V> vertexFactory, Map<String, V> resultMap) {
    if (size < 1) {
      return;
    }

    // A little trickery to intercept the rim generation.  This is
    // necessary since target may be initially non-empty, meaning we can't
    // rely on its vertex set after the rim is generated.
    final Collection<V> rim = new ArrayList<V>();
    VertexFactory<V> rimVertexFactory =
        new VertexFactory<V>() {
          public V createVertex() {
            V vertex = vertexFactory.createVertex();
            rim.add(vertex);

            return vertex;
          }
        };

    RingGraphGenerator<V, E> ringGenerator = new RingGraphGenerator<V, E>(size - 1);
    ringGenerator.generateGraph(target, rimVertexFactory, resultMap);

    V hubVertex = vertexFactory.createVertex();
    target.addVertex(hubVertex);

    if (resultMap != null) {
      resultMap.put(HUB_VERTEX, hubVertex);
    }

    for (V rimVertex : rim) {
      if (inwardSpokes) {
        target.addEdge(rimVertex, hubVertex);
      } else {
        target.addEdge(hubVertex, rimVertex);
      }
    }
  }
Esempio n. 6
0
  @Override
  public void generateGraph(
      Graph<V, E> veGraph, VertexFactory<V> vVertexFactory, Map<String, V> stringVMap) {

    Map<Integer, V> intToNodeMap = new HashMap<Integer, V>();

    for (Integer nodeID : selected.vertices.keySet()) {
      V node = vVertexFactory.createVertex();
      if (!veGraph.containsVertex(node)) veGraph.addVertex(node);
      intToNodeMap.put(nodeID, node);
    }

    for (PajekArc arc : selected.arcs) {
      V source = intToNodeMap.get(arc.source);
      V target = intToNodeMap.get(arc.target);
      E edge = null;
      if (!veGraph.containsEdge(source, target)) edge = veGraph.addEdge(source, target);
      else edge = veGraph.getEdge(source, target);

      if (veGraph instanceof WeightedGraph)
        ((WeightedGraph) veGraph).setEdgeWeight(edge, arc.weight);
    }
  }
  /** {@inheritDoc} */
  public void generateGraph(
      Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap) {
    V lastVertex = null;

    for (int i = 0; i < size; ++i) {
      V newVertex = vertexFactory.createVertex();
      target.addVertex(newVertex);

      if (lastVertex == null) {
        if (resultMap != null) {
          resultMap.put(START_VERTEX, newVertex);
        }
      } else {
        target.addEdge(lastVertex, newVertex);
      }

      lastVertex = newVertex;
    }

    if ((resultMap != null) && (lastVertex != null)) {
      resultMap.put(END_VERTEX, lastVertex);
    }
  }
Esempio n. 8
0
 void addVertex(V jtVertex) {
   jtElementsBeingAdded.add(jtVertex);
   graph.addVertex(jtVertex);
   jtElementsBeingAdded.remove(jtVertex);
 }
  /**
   * Create OSM graph for routing
   *
   * @return
   */
  public void createGraph() {

    logger.debug("Creating Graph...");
    graph = new DirectedWeightedMultigraph<>(OsmEdge.class);
    rgDelegator = new RoutingGraphDelegator(graph);
    rgDelegator.setRouteType(this.routeType);
    // iterate all ways and segments for all nodes:
    for (Way way : data.getWays()) {

      // skip way if not suitable for routing.
      if (way == null || way.isDeleted() || !this.isvalidWay(way) || way.getNodes().size() < 1)
        continue;

      // INIT
      Node from = null;
      Node to = null;
      List<Node> nodes = way.getNodes();
      int nodes_count = nodes.size();

      /*
       * Assume node is A B C D E. The procedure should be
       *
       *  case 1 - bidirectional ways:
       *  1) Add vertex A B C D E
       *  2) Link A<->B, B<->C, C<->D, D<->E as Edges
       *
       *  case 2 - oneway reverse:
       *  1) Add vertex A B C D E
       *  2) Link B->A,C->B,D->C,E->D as Edges. result: A<-B<-C<-D<-E
       *
       *  case 3 - oneway normal:
       *  1) Add vertex A B C D E
       *  2) Link A->B, B->C, C->D, D->E as Edges. result: A->B->C->D->E
       *
       *
       */

      String oneway_val = way.get("oneway"); /*   get (oneway=?) tag for this way.   */
      String junction_val = way.get("junction"); /*   get (junction=?) tag for this way.   */

      from = nodes.get(0); /*   1st node A  */
      graph.addVertex(from); /*   add vertex A */

      for (int i = 1; i < nodes_count; i++) {
        /*   loop from B until E */

        to = nodes.get(i); /*   2nd node B   */

        if (to != null && !to.isDeleted()) {
          graph.addVertex(to); /*   add vertex B */

          // this is where we link the vertices
          if (!routingProfile.isOnewayUsed()) {
            // "Ignore oneways" is selected
            addEdgeBidirectional(way, from, to);

          } else if (oneway_val == null && junction_val == "roundabout") {
            // Case (roundabout): oneway=implicit yes
            addEdgeNormalOneway(way, from, to);

          } else if (oneway_val == null
              || oneway_val == "false"
              || oneway_val == "no"
              || oneway_val == "0") {
            // Case (bi-way): oneway=false OR oneway=unset OR oneway=0 OR oneway=no
            addEdgeBidirectional(way, from, to);

          } else if (oneway_val == "-1") {
            // Case (oneway reverse): oneway=-1
            addEdgeReverseOneway(way, from, to);

          } else if (oneway_val == "1" || oneway_val == "yes" || oneway_val == "true") {
            // Case (oneway normal): oneway=yes OR 1 OR true
            addEdgeNormalOneway(way, from, to);
          }

          from = to; /*   we did A<->B, next loop we will do B<->C, so from=B,to=C for next loop. */
        }
      } // end of looping thru nodes
    } // end of looping thru ways

    logger.debug("End Create Graph");
    logger.debug("Vertex: " + graph.vertexSet().size());
    logger.debug("Edges: " + graph.edgeSet().size());
  }
Esempio n. 10
0
 public void addNode(Node toAdd) {
   nodes.add(toAdd);
   myGraph.addVertex(toAdd);
 }
Esempio n. 11
0
  /**
   * @param connectedOnly if true, the result will be a connected graph
   * @return
   */
  public Graph<V, E> toGraph() {
    if (subgraph != null) return subgraph;
    if (directed) {
      subgraph = new DirectedMultigraph<V, E>(g2.getEdgeFactory());
    } else {
      subgraph = new Multigraph<V, E>(g2.getEdgeFactory());
    }

    E edge;
    V source;
    V target;
    for (int x = 0; x < dimx; x++) {
      for (int y = 0; y < dimy; y++) {
        if (matrix[x][y]) {
          edge = edgeList2.get(y);
          source = g2.getEdgeSource(edge);
          target = g2.getEdgeTarget(edge);
          if (mappedVerticesFromG2.contains(source) && mappedVerticesFromG2.contains(target)) {
            // make sure the source and target vertices have been added, then add the edge
            subgraph.addVertex(source);
            subgraph.addVertex(target);
            subgraph.addEdge(source, target, edge);
          }
        }
      }
    }

    if (connectedOnly) {
      // make sure this subgraph is connected, if it is not return the largest connected part
      List<Set<V>> connectedVertices = new ArrayList<Set<V>>();
      for (V v : subgraph.vertexSet()) {
        if (!SharedStaticMethods.containsV(connectedVertices, v)) {
          connectedVertices.add(SharedStaticMethods.getConnectedVertices(subgraph, v));
        }
      }
      // ConnectedVertices now contains Sets of connected vertices every vertex of the subgraph is
      // contained exactly once in the list
      // if there is more then 1 set, then this method should return the largest connected part of
      // the graph
      if (connectedVertices.size() > 1) {
        Graph<V, E> largestResult = null;
        Graph<V, E> currentGraph;
        int largestSize = -1;
        Set<V> currentSet;
        for (int i = 0; i < connectedVertices.size(); i++) {
          currentSet = connectedVertices.get(i);
          /*note that 'subgraph' is the result from the Mcgregor algorithm, 'currentGraph' is an
           * induced subgraph of 'subgraph'. 'currentGraph' is connected, because the vertices in
           * 'currentSet' are connected with edges in 'subgraph'
           */
          currentGraph = new Subgraph<V, E, Graph<V, E>>(subgraph, currentSet);
          if (currentGraph.edgeSet().size() > largestSize) {
            largestResult = currentGraph;
          }
        }

        return largestResult;
      }
    }

    return subgraph;
  }