Example #1
0
  public int testBfsDfs() {
    int foo = 0;

    // BFS from 1000 nodes
    start = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
      Iterator<Node> bfsIt = g.getNode(nodeIds.get(i)).getBreadthFirstIterator();
      while (bfsIt.hasNext()) {
        Node node = bfsIt.next();
        if (node.hasAttribute("foo")) foo++;
      }
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.BFS_IT, end - start);

    // DFS from 1000 nodes - tested only for new implementations
    // because of a bug in the old
    start = System.currentTimeMillis();
    if (g instanceof org.graphstream.graph.implementations.AbstractGraph) {
      for (int i = 0; i < 1000; i++) {
        Iterator<Node> dfsIt = g.getNode(nodeIds.get(i)).getDepthFirstIterator();
        while (dfsIt.hasNext()) {
          Node node = dfsIt.next();
          if (node.hasAttribute("foo")) foo++;
        }
      }
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.DFS_IT, end - start);

    return foo;
  }
Example #2
0
  public int testGraphIterators() {
    int foo = 0;

    // Iterating on all nodes
    start = System.currentTimeMillis();
    Iterator<Node> nodeIt = g.getNodeIterator();
    while (nodeIt.hasNext()) {
      Node n = nodeIt.next();
      if (n.hasAttribute("foo")) foo++;
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.GRAPH_NODE_IT, end - start);

    // iterating on all edges
    start = System.currentTimeMillis();
    Iterator<Edge> edgeIt = g.getEdgeIterator();
    while (edgeIt.hasNext()) {
      Edge e = edgeIt.next();
      if (e.hasAttribute("foo")) foo++;
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.GRAPH_EDGE_IT, end - start);

    return foo;
  }
  /**
   * Like {@link #nodePosition(Graph,String,Point3)} but use an existing node as argument.
   *
   * @param node The node to consider.
   * @param pos A point that will receive the node position.
   */
  public static void nodePosition(Node node, Point3 pos) {
    if (node.hasAttribute("xyz") || node.hasAttribute("xy")) {
      Object o = node.getAttribute("xyz");

      if (o == null) o = node.getAttribute("xy");

      if (o != null) {
        positionFromObject(o, pos);
      }
    } else if (node.hasAttribute("x")) {
      pos.x = (double) node.getNumber("x");

      if (node.hasAttribute("y")) pos.y = (double) node.getNumber("y");

      if (node.hasAttribute("z")) pos.z = (double) node.getNumber("z");
    }

    //		if (node.hasAttribute("xyz") || node.hasAttribute("xy")) {
    //			Object o = node.getAttribute("xyz");
    //
    //			if (o == null)
    //				o = node.getAttribute("xy");
    //
    //			if (o != null && o instanceof Object[]) {
    //				Object oo[] = (Object[]) o;
    //
    //				if (oo.length > 0 && oo[0] instanceof Number) {
    //					pos.x = ((Number) oo[0]).doubleValue();
    //
    //					if (oo.length > 1)
    //						pos.y = ((Number) oo[1]).doubleValue();
    //					if (oo.length > 2)
    //						pos.z = ((Number) oo[2]).doubleValue();
    //				}
    //			}
    //		} else if (node.hasAttribute("x")) {
    //			pos.x = (double) node.getNumber("x");
    //
    //			if (node.hasAttribute("y"))
    //				pos.y = (double) node.getNumber("y");
    //
    //			if (node.hasAttribute("z"))
    //				pos.z = (double) node.getNumber("z");
    //		}
  }
  /**
   * Like {@link #nodePosition(Graph,String,double[])} but use an existing node as argument.
   *
   * @param node The node to consider.
   * @param xyz An array of at least three cells.
   */
  public static void nodePosition(Node node, double xyz[]) {
    if (xyz.length < 3) return;

    if (node.hasAttribute("xyz") || node.hasAttribute("xy")) {
      Object o = node.getAttribute("xyz");

      if (o == null) o = node.getAttribute("xy");

      if (o != null) {
        positionFromObject(o, xyz);
      }

    } else if (node.hasAttribute("x")) {
      xyz[0] = (double) node.getNumber("x");

      if (node.hasAttribute("y")) xyz[1] = (double) node.getNumber("y");

      if (node.hasAttribute("z")) xyz[2] = (double) node.getNumber("z");
    }
  }
Example #5
0
  /**
   * Compute the scores for all relevant communities for the selected node using Leung algorithm.
   *
   * @param u The node for which the scores computation is performed
   * @complexity O(DELTA) where DELTA is is the average node degree in the network
   */
  @Override
  protected void communityScores(Node u) {
    /*
     * Reset the scores for each communities
     */
    communityScores = new HashMap<Object, Double>();

    /*
     * Iterate over the nodes that this node "hears"
     */
    for (Edge e : u.getEnteringEdgeSet()) {
      Node v = e.getOpposite(u);

      /*
       * Update the count for this community
       */
      if (v.hasAttribute(marker)) {

        // Compute the neighbor node current score
        Double score = (Double) v.getAttribute(marker + ".score") * Math.pow(v.getInDegree(), m);

        /*
         * The rest of the formula depends on the weighted status of the
         * network
         */
        Double weight;
        if (e.hasAttribute(weightMarker))
          if (e.isDirected()) {
            Edge e2 = v.getEdgeToward(u.getId());
            if (e2 != null && e2.hasAttribute(weightMarker))
              weight =
                  (Double) e.getAttribute(weightMarker) + (Double) e2.getAttribute(weightMarker);
            else weight = (Double) e.getAttribute(weightMarker);
          } else weight = (Double) e.getAttribute(weightMarker);
        else weight = 1.0;

        // Update the score of the according community
        if (communityScores.get(v.getAttribute(marker)) == null)
          communityScores.put(v.getAttribute(marker), score * weight);
        else
          communityScores.put(
              v.getAttribute(marker),
              communityScores.get(v.getAttribute(marker)) + (score * weight));
      }
    }
  }
Example #6
0
  @Override
  public void computeNode(Node node) {
    /*
     * Recall and update the node current community and previous score
     */
    Object previousCommunity = node.getAttribute(marker);
    Double previousScore = (Double) node.getAttribute(marker + ".score");
    super.computeNode(node);

    /*
     * Update the node label score
     */

    // Handle first iteration
    if (previousCommunity == null) {
      previousCommunity = node.getAttribute(marker);
      previousScore = (Double) node.getAttribute(marker + ".score");
    }

    /*
     * The node is the originator of the community and hasn't changed
     * community at this iteration (or we are at the first simulation step):
     * keep the maximum label score
     */
    if ((node.getAttribute(marker).equals(previousCommunity)) && (previousScore.equals(1.0)))
      node.setAttribute(marker + ".score", 1.0);

    /*
     * Otherwise search for the highest score amongst neighbors and reduce
     * it by decreasing factor
     */
    else {
      Double maxLabelScore = Double.NEGATIVE_INFINITY;
      for (Edge e : node.getEnteringEdgeSet()) {
        Node v = e.getOpposite(node);
        if (v.hasAttribute(marker) && v.getAttribute(marker).equals(node.getAttribute(marker))) {
          if ((Double) v.getAttribute(marker + ".score") > maxLabelScore)
            maxLabelScore = (Double) v.getAttribute(marker + ".score");
        }
      }
      node.setAttribute(marker + ".score", maxLabelScore - delta);
    }
  }
Example #7
0
  public int testAccessById() {
    int foo = 0;

    // access each node by id
    start = System.currentTimeMillis();
    for (String id : nodeIds) {
      Node n = g.getNode(id);
      if (n.hasAttribute("foo")) foo++;
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.NODE_BY_ID, end - start);

    // access each edge by id
    start = System.currentTimeMillis();
    for (String id : edgeIds) {
      Edge e = g.getEdge(id);
      if (e.hasAttribute("foo")) foo++;
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.EDGE_BY_ID, end - start);
    return foo;
  }
Example #8
0
  public int testNodeIterators() {
    int foo = 0;

    // For each node n, iterating on all edges of n
    start = System.currentTimeMillis();
    Iterator<Node> nodeIt = g.getNodeIterator();
    while (nodeIt.hasNext()) {
      Node n = nodeIt.next();
      Iterator<Edge> edgeIt = n.getEdgeIterator();
      while (edgeIt.hasNext()) {
        Edge e = edgeIt.next();
        if (e.hasAttribute("foo")) foo++;
      }
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.NODE_EDGE_IT, end - start);

    // For each node n, iterating on all entering edges of n
    start = System.currentTimeMillis();
    nodeIt = g.getNodeIterator();
    while (nodeIt.hasNext()) {
      Node n = nodeIt.next();
      Iterator<Edge> edgeIt = n.getEnteringEdgeIterator();
      while (edgeIt.hasNext()) {
        Edge e = edgeIt.next();
        if (e.hasAttribute("foo")) foo++;
      }
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.NODE_ENTERING_EDGE_IT, end - start);

    // For each node n, iterating on all leaving edges of n
    start = System.currentTimeMillis();
    nodeIt = g.getNodeIterator();
    while (nodeIt.hasNext()) {
      Node n = nodeIt.next();
      Iterator<Edge> edgeIt = n.getLeavingEdgeIterator();
      while (edgeIt.hasNext()) {
        Edge e = edgeIt.next();
        if (e.hasAttribute("foo")) foo++;
      }
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.NODE_LEAVING_EDGE_IT, end - start);

    // For each node n, iterating on all neighbors of n
    start = System.currentTimeMillis();
    nodeIt = g.getNodeIterator();
    while (nodeIt.hasNext()) {
      Node n = nodeIt.next();
      Iterator<Node> neighborIt = n.getNeighborNodeIterator();
      while (neighborIt.hasNext()) {
        Node neighbor = neighborIt.next();
        if (neighbor.hasAttribute("foo")) foo++;
      }
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.NODE_NEIGHBOR_IT, end - start);

    // For each node n, iterating on all edges of n using n.getEdge(i)
    start = System.currentTimeMillis();
    nodeIt = g.getNodeIterator();
    while (nodeIt.hasNext()) {
      Node n = nodeIt.next();
      for (int i = 0; i < n.getDegree(); i++) {
        Edge e = n.getEdge(i);
        if (e.hasAttribute("foo")) foo++;
      }
    }
    end = System.currentTimeMillis();
    measureValues.put(Measures.NODE_GET_EDGE, end - start);

    return foo;
  }