Beispiel #1
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));
      }
    }
  }
Beispiel #2
0
  /*
   * (non-Javadoc)
   *
   * @see org.graphstream.algorithm.Algorithm#compute()
   */
  @SuppressWarnings("unchecked")
  public void compute() {
    Node source = graph.getNode(this.source_id);

    // Step 1: Initialize graph

    for (Node n : graph) {
      if (n == source) n.addAttribute(identifier + ".distance", 0.0);
      else n.addAttribute(identifier + ".distance", Double.POSITIVE_INFINITY);

      // n.addAttribute(identifier+".predecessors",(Object)null);
    }

    // Step 2: relax edges repeatedly

    for (int i = 0; i < graph.getNodeCount(); i++) {
      for (Edge e : graph.getEachEdge()) {
        Node n0 = e.getNode0();
        Node n1 = e.getNode1();
        Double d0 = (Double) n0.getAttribute(identifier + ".distance");
        Double d1 = (Double) n1.getAttribute(identifier + ".distance");

        Double we = (Double) e.getAttribute(weightAttribute);
        if (we == null)
          throw new NumberFormatException(
              "org.graphstream.algorithm.BellmanFord: Problem with attribute \""
                  + weightAttribute
                  + "\" on edge "
                  + e);

        if (d0 != null) {
          if (d1 == null || d1 >= d0 + we) {
            n1.addAttribute(identifier + ".distance", d0 + we);
            ArrayList<Edge> predecessors =
                (ArrayList<Edge>) n1.getAttribute(identifier + ".predecessors");

            if (d1 != null && d1 == d0 + we) {
              if (predecessors == null) {
                predecessors = new ArrayList<Edge>();
              }
            } else {
              predecessors = new ArrayList<Edge>();
            }
            if (!predecessors.contains(e)) {
              predecessors.add(e);
            }

            n1.addAttribute(identifier + ".predecessors", predecessors);
          }
        }
      }
    }

    // Step 3: check for negative-weight cycles

    for (Edge e : graph.getEachEdge()) {
      Node n0 = e.getNode0();
      Node n1 = e.getNode1();
      Double d0 = (Double) n0.getAttribute(identifier + ".distance");
      Double d1 = (Double) n1.getAttribute(identifier + ".distance");

      Double we = (Double) e.getAttribute(weightAttribute);

      if (we == null) {
        throw new NumberFormatException(
            String.format(
                "%s: Problem with attribute \"%s\" on edge \"%s\"",
                BellmanFord.class.getName(), weightAttribute, e.getId()));
      }

      if (d1 > d0 + we) {
        throw new NumberFormatException(
            String.format(
                "%s: Problem: negative weight, cycle detected on edge \"%s\"",
                BellmanFord.class.getName(), e.getId()));
      }
    }
  }