Example #1
0
  /**
   * Clone a given graph with same node/edge structure and same attributes.
   *
   * @param g the graph to clone
   * @return a copy of g
   */
  public static Graph clone(Graph g) {
    Graph copy;

    try {
      Class<? extends Graph> cls = g.getClass();
      copy = cls.getConstructor(String.class).newInstance(g.getId());
    } catch (Exception e) {
      logger.warning(String.format("Cannot create a graph of %s.", g.getClass().getName()));
      copy = new AdjacencyListGraph(g.getId());
    }

    copyAttributes(g, copy);

    for (int i = 0; i < g.getNodeCount(); i++) {
      Node source = g.getNode(i);
      Node target = copy.addNode(source.getId());

      copyAttributes(source, target);
    }

    for (int i = 0; i < g.getEdgeCount(); i++) {
      Edge source = g.getEdge(i);
      Edge target =
          copy.addEdge(
              source.getId(),
              source.getSourceNode().getId(),
              source.getTargetNode().getId(),
              source.isDirected());

      copyAttributes(source, target);
    }

    return copy;
  }
Example #2
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));
      }
    }
  }