Esempio n. 1
0
  private boolean applyAfterDirectedEdgeAddition(Update u) {
    DirectedEdge e = (DirectedEdge) ((EdgeUpdate) u).getEdge();
    DirectedNode src = e.getSrc();
    DirectedNode dst = e.getDst();
    int srcDegree = src.getOutDegree();
    int dstDegree = dst.getOutDegree();

    if (richClub.containsKey(srcDegree - 1) && richClub.get(srcDegree - 1).contains(src)) {

      if (richClub.containsKey(dstDegree) && richClub.get(dstDegree).contains(dst)) {
        this.edgesBetweenRichClub++;
      }
      removeFromRichclub(src);
      addToRichClub(src);
    } else if (this.richClub.containsKey(srcDegree - 1)
        && this.nodesSortedByDegree.containsKey(srcDegree - 1)
        && this.nodesSortedByDegree.get(srcDegree - 1).contains(src)) {

      // changes for lastNode of Richclub
      DirectedNode lastNode = (DirectedNode) this.richClub.get(srcDegree - 1).removeLast();
      if (this.richClub.get(lastNode.getOutDegree()).isEmpty()) {
        this.richClub.remove(lastNode.getOutDegree());
      }
      addNodeToRest(lastNode);

      // Changes for src node for richclub
      this.nodesSortedByDegree.get(srcDegree - 1).remove(src);
      if (this.nodesSortedByDegree.get(src.getOutDegree() - 1).isEmpty()) {
        this.nodesSortedByDegree.remove(src.getOutDegree() - 1);
      }
      addToRichClub(src);

      // calculate changes for richclub connectivity
      for (IElement ie : src.getEdges()) {
        DirectedEdge edge = (DirectedEdge) ie;
        DirectedNode node = (DirectedNode) edge.getDifferingNode(src);
        if (this.richClub.containsKey(node.getOutDegree())
            && this.richClub.get(node.getOutDegree()).contains(node)) {
          this.edgesBetweenRichClub++;
        }
      }

      for (IElement ie : lastNode.getEdges()) {
        DirectedEdge edge = (DirectedEdge) ie;
        DirectedNode node = (DirectedNode) edge.getDifferingNode(lastNode);
        if (this.richClub.containsKey(node.getOutDegree())
            && this.richClub.get(node.getOutDegree()).contains(node)) {
          this.edgesBetweenRichClub--;
        }
      }

    } else {
      this.nodesSortedByDegree.get(src.getOutDegree() - 1).remove(src);
      if (this.nodesSortedByDegree.get(src.getOutDegree() - 1).isEmpty()) {
        this.nodesSortedByDegree.remove(src.getOutDegree() - 1);
      }
      addNodeToRest(src);
    }
    return true;
  }
Esempio n. 2
0
  private boolean directedCompute() {

    SortedSet<Integer> degrees = new TreeSet<Integer>();
    for (IElement iE : g.getNodes()) {
      DirectedNode n = (DirectedNode) iE;
      int degree = n.getOutDegree();

      degrees.add(degree);
      if (nodesSortedByDegree.containsKey(degree)) {
        this.nodesSortedByDegree.get(degree).add(n);
      } else {
        LinkedList<Node> temp = new LinkedList<>();
        temp.add(n);
        this.nodesSortedByDegree.put(degree, temp);
      }
    }

    HashSet<Node> currentRichClub = new HashSet<Node>();
    int currentRichClubSize = 0;
    int size = degrees.size();
    for (int i = 0; i < size; i++) {
      int currentDegree = degrees.last();
      degrees.remove(currentDegree);
      LinkedList<Node> current = this.nodesSortedByDegree.get(currentDegree);
      currentRichClubSize += current.size();
      this.nodesSortedByDegree.remove(currentDegree);

      if (currentRichClubSize >= this.richClubSize) {
        int seperateAT = current.size() - (currentRichClubSize - this.richClubSize);
        LinkedList<Node> temp = new LinkedList<>();
        temp.addAll(current.subList(0, seperateAT));
        this.richClub.put(currentDegree, temp);
        currentRichClub.addAll(temp);
        LinkedList<Node> temp2 = new LinkedList<>();
        temp2.addAll(current.subList(seperateAT, current.size()));
        if (!temp2.isEmpty()) this.nodesSortedByDegree.put(currentDegree, (LinkedList<Node>) temp2);
        break;
      } else {
        richClub.put(currentDegree, current);
        currentRichClub.addAll(current);
      }
    }

    for (Node n : currentRichClub) {
      DirectedNode ne = (DirectedNode) n;
      for (IElement iE : ne.getOutgoingEdges()) {
        DirectedEdge e = (DirectedEdge) iE;
        if (currentRichClub.contains(e.getDst())) {
          edgesBetweenRichClub++;
        }
      }
    }
    return true;
  }
 private boolean applyAfterUpdateDirected(Update u) {
   if (u instanceof EdgeRemoval) {
     DirectedEdge e = (DirectedEdge) ((EdgeRemoval) u).getEdge();
     DirectedNode a = e.getSrc();
     DirectedNode b = e.getDst();
     if (a.hasEdge(new DirectedEdge(b, a))) {
       // remove triangles
       for (IElement cUncasted : a.getNeighbors()) {
         DirectedNode c = (DirectedNode) cUncasted;
         if (b.hasNeighbor(c)) {
           this.removeTriangle(a);
           this.removeTriangle(b);
           this.removeTriangle(c);
         }
       }
       // remove potentials
       this.removePotentials(a, a.getNeighborCount());
       this.removePotentials(b, b.getNeighborCount());
     }
   }
   return true;
 }
  private boolean applyBeforeUpdateDirected(Update u) {
    if (u instanceof NodeAddition) {
      Node n = (Node) ((NodeAddition) u).getNode();
      this.localCC.setValue(n.getIndex(), 0);
      this.nodePotentialCount =
          ArrayUtils.set(this.nodePotentialCount, n.getIndex(), 0, Long.MIN_VALUE);
      this.nodeTriangleCount =
          ArrayUtils.set(this.nodeTriangleCount, n.getIndex(), 0, Long.MIN_VALUE);
      this.averageCC = ArrayUtils.avgIgnoreNaN(this.localCC.getValues());
    } else if (u instanceof NodeRemoval) {

      DirectedNode n = (DirectedNode) ((NodeRemoval) u).getNode();

      DirectedNode[] neighbors = new DirectedNode[n.getNeighborCount()];
      int index = 0;
      for (IElement neighbor : n.getNeighbors()) {
        neighbors[index++] = (DirectedNode) neighbor;
      }

      for (int i = 0; i < neighbors.length; i++) {
        for (int j = i + 1; j < neighbors.length; j++) {
          if (neighbors[i].hasEdge(new DirectedEdge(neighbors[i], neighbors[j]))
              && neighbors[i].hasEdge(new DirectedEdge(neighbors[j], neighbors[i]))) {
            this.removeTriangle(n);
            this.removeTriangle(neighbors[i]);
            this.removeTriangle(neighbors[j]);
          }
        }
        this.removePotentials(neighbors[i], neighbors[i].getNeighborCount() - 1);
      }

      this.removePotentials(n, n.getNeighborCount() * (n.getNeighborCount() - 1) / 2);

      this.localCC.setValue(n.getIndex(), NodeValueList.emptyValue);
      this.nodePotentialCount[n.getIndex()] = Long.MIN_VALUE;
      this.nodeTriangleCount[n.getIndex()] = Long.MIN_VALUE;
      this.localCC.truncate();
      this.nodePotentialCount = ArrayUtils.truncate(this.nodePotentialCount, Long.MIN_VALUE);
      this.nodeTriangleCount = ArrayUtils.truncate(this.nodeTriangleCount, Long.MIN_VALUE);

      this.averageCC = ArrayUtils.avgIgnoreNaN(this.localCC.getValues());

    } else if (u instanceof EdgeAddition) {
      DirectedEdge e = (DirectedEdge) ((EdgeAddition) u).getEdge();
      DirectedNode a = e.getSrc();
      DirectedNode b = e.getDst();
      if (a.hasEdge(new DirectedEdge(b, a))) {
        // new triangles
        for (IElement cUncasted : a.getNeighbors()) {
          DirectedNode c = (DirectedNode) cUncasted;
          if (b.hasNeighbor(c)) {
            this.addTriangle(a);
            this.addTriangle(b);
            this.addTriangle(c);
          }
        }
        // new potentials
        this.addPotentials(a, a.getNeighborCount());
        this.addPotentials(b, b.getNeighborCount());
      }
    }
    return true;
  }
  private void directedDelete(DirectedNode r, DirectedEdge e) {
    HashMap<Node, Node> parent = this.parents.get(r);
    HashMap<Node, Integer> height = this.heights.get(r);
    DirectedNode src = e.getSrc();
    DirectedNode dst = e.getDst();

    // if the source or dst or edge is not in tree do nothing
    if (height.get(src) == Integer.MAX_VALUE
        || height.get(dst) == Integer.MAX_VALUE
        || dst.equals(r)
        || !parent.get(dst).equals(src)) {
      return;
    }

    // Queues and data structure for tree change
    HashSet<DirectedNode> uncertain = new HashSet<DirectedNode>();
    HashSet<DirectedNode> changed = new HashSet<DirectedNode>();

    PriorityQueue<QueueElement<DirectedNode>> q = new PriorityQueue<QueueElement<DirectedNode>>();

    q.add(new QueueElement<DirectedNode>(dst, height.get(dst)));

    uncertain.add(dst);
    parent.remove(dst);

    while (!q.isEmpty()) {
      QueueElement<DirectedNode> qE = q.poll();
      DirectedNode w = qE.e;

      int key = qE.distance;

      // find the new shortest path
      int dist = Integer.MAX_VALUE;

      ArrayList<DirectedNode> minSettled = new ArrayList<DirectedNode>();
      ArrayList<DirectedNode> min = new ArrayList<DirectedNode>();
      for (IElement iEgde : w.getIncomingEdges()) {
        DirectedWeightedEdge edge = (DirectedWeightedEdge) iEgde;
        IntWeight eWeight = (IntWeight) edge.getWeight();
        DirectedNode z = edge.getSrc();

        if (changed.contains(z) || height.get(z) == Integer.MAX_VALUE) {
          continue;
        }
        if (height.get(z) + (int) eWeight.getWeight() < dist) {
          min.clear();
          minSettled.clear();
          min.add(z);
          if (!uncertain.contains(z)) minSettled.add(z);
          dist = height.get(z) + (int) eWeight.getWeight();
          continue;
        }
        if (height.get(z) + (int) eWeight.getWeight() == dist) {
          min.add(z);
          if (!uncertain.contains(z)) minSettled.add(z);
          continue;
        }
      }

      boolean noPossibleNeighbour =
          (key >= breakLoop && dist > breakLoop)
              || (min.isEmpty() && (!uncertain.contains(w) || (key == dist)));

      // no neighbour found
      if (noPossibleNeighbour) {
        if (height.get(w) != Integer.MAX_VALUE) apsp.decr(height.get(w));
        height.put(w, Integer.MAX_VALUE);
        parent.remove(w);
        continue;
      }
      if (uncertain.contains(w)) {
        if (key == dist) {
          if (minSettled.isEmpty()) {
            parent.put(w, min.get(0));
          } else {
            parent.put(w, minSettled.get(0));
          }
          if (height.get(w) != Integer.MAX_VALUE) apsp.decr(height.get(w));
          apsp.incr(dist);
          height.put(w, dist);
          for (IElement iEgde : w.getOutgoingEdges()) {
            DirectedWeightedEdge edge = (DirectedWeightedEdge) iEgde;
            IntWeight iEdgeWeight = (IntWeight) edge.getWeight();

            DirectedNode z = edge.getDst();

            if (height.get(z) > dist + (int) iEdgeWeight.getWeight()) {
              q.remove(new QueueElement<DirectedNode>(z, dist + (int) iEdgeWeight.getWeight()));
              q.add(new QueueElement<DirectedNode>(z, dist + (int) iEdgeWeight.getWeight()));
            }
          }
        } else {
          changed.add(w);

          q.add(new QueueElement<DirectedNode>(w, dist));
          uncertain.remove(w);
          for (IElement iEgde : w.getOutgoingEdges()) {
            DirectedEdge edge = (DirectedEdge) iEgde;

            DirectedNode z = edge.getDst();

            if (parent.get(z) == w) {
              parent.remove(z);
              uncertain.add(z);
              q.add(new QueueElement<DirectedNode>(z, height.get(z)));
            }
          }
        }
        continue;
      }
      if (dist > key) {
        q.add(new QueueElement<DirectedNode>(w, dist));

        continue;
      }
      if (minSettled.isEmpty()) {
        parent.put(w, min.get(0));
      } else {
        parent.put(w, minSettled.get(0));
      }

      changed.remove(w);
      if (height.get(w) != Integer.MAX_VALUE) apsp.decr(height.get(w));
      apsp.incr(dist);
      height.put(w, dist);
      for (IElement iEgde : w.getOutgoingEdges()) {
        DirectedWeightedEdge edge = (DirectedWeightedEdge) iEgde;
        IntWeight eWeight = (IntWeight) edge.getWeight();

        DirectedNode z = edge.getDst();

        if (height.get(z) > dist + (int) eWeight.getWeight()) {
          q.remove(new QueueElement<DirectedNode>(z, dist + (int) eWeight.getWeight()));
          q.add(new QueueElement<DirectedNode>(z, dist + (int) eWeight.getWeight()));
        }
      }
    }
  }