Exemplo n.º 1
0
  private boolean applyAfterDirectedNodeRemoval(Update u) {
    DirectedNode node = (DirectedNode) ((NodeRemoval) u).getNode();
    int min = Integer.MAX_VALUE;
    int max = 0;
    HashMap<Integer, LinkedList<DirectedNode>> order = new HashMap<>();
    for (IElement ie : node.getIncomingEdges()) {
      DirectedEdge e = (DirectedEdge) ie;
      DirectedNode n = e.getSrc();

      if (richClub.containsKey(n.getOutDegree() + 1)
          && richClub.get(n.getOutDegree() + 1).contains(n)) {
        this.edgesBetweenRichClub--;
        min = Math.min(min, n.getOutDegree());
        max = Math.max(max, n.getOutDegree());
        richClub.get(n.getOutDegree() + 1).remove(n);
        if (richClub.get(n.getOutDegree() + 1).isEmpty()) {
          richClub.remove(n.getOutDegree() + 1);
        }
        if (richClub.containsKey(n.getOutDegree())) {
          richClub.get(n.getOutDegree()).add(n);
        } else {
          richClub.put(n.getOutDegree(), new LinkedList<Node>());
          richClub.get(n.getOutDegree()).add(n);
        }
        if (order.containsKey(n.getOutDegree())) {
          order.get(n.getOutDegree()).add(n);
        } else {
          order.put(n.getOutDegree(), new LinkedList<DirectedNode>());
          order.get(n.getOutDegree()).add(n);
        }
      } else if (nodesSortedByDegree.containsKey(n.getOutDegree() + 1)
          && nodesSortedByDegree.get(n.getOutDegree() + 1).contains(n)) {
        nodesSortedByDegree.get(n.getOutDegree() + 1).remove(n);
        if (nodesSortedByDegree.get(n.getOutDegree() + 1).isEmpty()) {
          nodesSortedByDegree.remove(n.getOutDegree() + 1);
        }
        if (nodesSortedByDegree.containsKey(n.getOutDegree())) {
          nodesSortedByDegree.get(n.getOutDegree()).add(n);
        } else {
          nodesSortedByDegree.put(n.getOutDegree(), new LinkedList<Node>());
          nodesSortedByDegree.get(n.getOutDegree()).add(n);
        }
      } else {
        System.out.println("f**k");
      }
    }

    for (int i = min; i <= max; i++) {
      boolean f = false;
      if (f) {
        break;
      }
      if (!order.containsKey(i)) {
        continue;
      }
      for (DirectedNode n1 : order.get(i)) {
        int big = getBiggest();
        if (i >= big) {
          f = true;
          break;
        }

        // changes for firstNode of Rest
        DirectedNode firstNode = (DirectedNode) this.nodesSortedByDegree.get(big).removeFirst();
        if (this.nodesSortedByDegree.get(firstNode.getOutDegree()).isEmpty()) {
          this.nodesSortedByDegree.remove(firstNode.getOutDegree());
        }
        addToRichClub(firstNode);

        // Changes for src node for richclub
        this.richClub.get(i).remove(n1);
        if (this.richClub.get(i).isEmpty()) {
          this.richClub.remove(i);
        }
        addNodeToRest(n1);

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

        for (IElement ie : firstNode.getEdges()) {
          DirectedEdge edge = (DirectedEdge) ie;
          DirectedNode n2 = (DirectedNode) edge.getDifferingNode(firstNode);
          if (this.richClub.containsKey(n2.getOutDegree())
              && this.richClub.get(n2.getOutDegree()).contains(n2)) {
            this.edgesBetweenRichClub++;
          }
        }
      }
    }
    if (this.richClub.containsKey(node.getOutDegree())
        && this.richClub.get(node.getOutDegree()).contains(node)) {
      int degree = ((DirectedNode) node).getOutDegree();

      // changes for firstNode of Rest
      DirectedNode firstNode =
          (DirectedNode) this.nodesSortedByDegree.get(getBiggest()).removeFirst();
      if (this.nodesSortedByDegree.get(firstNode.getOutDegree()).isEmpty()) {
        this.nodesSortedByDegree.remove(firstNode.getOutDegree());
      }
      addToRichClub(firstNode);

      // Changes for src node for richclub
      this.richClub.get(degree).remove(node);
      if (this.richClub.get(degree).isEmpty()) {
        this.richClub.remove(degree);
      }

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

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

    } else {
      int degree = ((DirectedNode) node).getOutDegree();

      this.nodesSortedByDegree.get(degree).remove(node);
      if (this.nodesSortedByDegree.get(degree).isEmpty()) {
        this.nodesSortedByDegree.remove(degree);
      }
    }

    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()));
        }
      }
    }
  }