src/main/java/thinkd/ThinkDFast.java
/**
     * Counts triangles with the given edge and update estimates
     * @param src the source node of the given edge
     * @param dst the destination node of the given edge
     * @param add true for addition and false for deletion
     */
Counts triangles with the given edge and update estimates
@param src the source node of the given edge
@param dst the destination node of the given edge
@param add true for addition and false for deletion
Counts triangles with the given edge and update estimates @param src the source node of the given edge @param dst the destination node of the given edge @param add true for addition and false for deletion
private void count(final int src, final int dst, boolean add) {
        if(!srcToDsts.containsKey(src) || !srcToDsts.containsKey(dst)) {
            return;
        }
        IntOpenHashSet srcSet = srcToDsts.get(src);
        IntOpenHashSet dstSet = srcToDsts.get(dst);
        if(srcSet.size() > dstSet.size()) {
            IntOpenHashSet temp = srcSet;
            srcSet = dstSet;
            dstSet = temp;
        }
        if(add) { 
            int count = 0; 
            for (int neighbor : srcSet) {
                if (dstSet.contains(neighbor)) {
                    count++;
                    nodeToTriangles.addTo(neighbor, inc);
                }
            }
            if (count > 0) {
                double countSum = inc * count;
                nodeToTriangles.addTo(src, countSum);
                nodeToTriangles.addTo(dst, countSum);
                globalTriangle += countSum;
            }
        }
        else if(lowerBound){ 
            int count = 0; 
            for (int neighbor : srcSet) {
                if (dstSet.contains(neighbor)) {
                    count ++;
                    double value = nodeToTriangles.addTo(neighbor, - inc);
                    if(value < inc) {
                        nodeToTriangles.put(neighbor, 0); 
                    }
                }
            }
            if (count > 0) {
                final double countSum = inc * count;
                double value = nodeToTriangles.addTo(src, - countSum);
                if(value < countSum) {
                    nodeToTriangles.put(src, 0); 
                }
                value = nodeToTriangles.addTo(dst, - countSum);
                if(value < countSum) {
                    nodeToTriangles.put(dst, 0); 
                }
                globalTriangle -= countSum;
                globalTriangle = Math.max(0, globalTriangle); 
            }
        }
        else { 
            int count = 0; 
            for (int neighbor : srcSet) {
                if (dstSet.contains(neighbor)) {
                    count ++;
                    nodeToTriangles.addTo(neighbor, - inc);
                }
            }
            if (count > 0) {
                double countSum = inc * count;
                nodeToTriangles.addTo(src, - countSum);
                nodeToTriangles.addTo(dst, - countSum);
                globalTriangle -= countSum;
            }
        }
    }
private void count ( final int src , final int dst , boolean add ) { if ( ! srcToDsts . containsKey ( src ) || ! srcToDsts . containsKey ( dst ) ) { return ; } IntOpenHashSet srcSet = srcToDsts . get ( src ) ; IntOpenHashSet dstSet = srcToDsts . get ( dst ) ; if ( srcSet . size ( ) > dstSet . size ( ) ) { IntOpenHashSet temp = srcSet ; srcSet = dstSet ; dstSet = temp ; } if ( add ) { int count = 0 ; for ( int neighbor : srcSet ) { if ( dstSet . contains ( neighbor ) ) { count ++ ; nodeToTriangles . addTo ( neighbor , inc ) ; } } if ( count > 0 ) { double countSum = inc * count ; nodeToTriangles . addTo ( src , countSum ) ; nodeToTriangles . addTo ( dst , countSum ) ; globalTriangle += countSum ; } } else if ( lowerBound ) { int count = 0 ; for ( int neighbor : srcSet ) { if ( dstSet . contains ( neighbor ) ) { count ++ ; double value = nodeToTriangles . addTo ( neighbor , - inc ) ; if ( value < inc ) { nodeToTriangles . put ( neighbor , 0 ) ; } } } if ( count > 0 ) { final double countSum = inc * count ; double value = nodeToTriangles . addTo ( src , - countSum ) ; if ( value < countSum ) { nodeToTriangles . put ( src , 0 ) ; } value = nodeToTriangles . addTo ( dst , - countSum ) ; if ( value < countSum ) { nodeToTriangles . put ( dst , 0 ) ; } globalTriangle -= countSum ; globalTriangle = Math . max ( 0 , globalTriangle ) ; } } else { int count = 0 ; for ( int neighbor : srcSet ) { if ( dstSet . contains ( neighbor ) ) { count ++ ; nodeToTriangles . addTo ( neighbor , - inc ) ; } } if ( count > 0 ) { double countSum = inc * count ; nodeToTriangles . addTo ( src , - countSum ) ; nodeToTriangles . addTo ( dst , - countSum ) ; globalTriangle -= countSum ; } } }
Counts triangles with the given edge and update estimates
 @param src the source node of the given edge
 @param dst the destination node of the given edge
 @param add true for addition and false for deletion
Counts triangles with the given edge and update estimates @param src the source node of the given edge @param dst the destination node of the given edge @param add true for addition and false for deletion
// if this edge has a new node, there cannot be any triangles // process the addition // number of added triangles // process the deletion with lower bounding // number of deleted triangles //lower bounding //lower bounding //lower bounding //lower bounding // process the deletion without lower bounding // number of deleted triangles