(graph: nx.Graph)
return max_node_ks
Reference:
        Burkhardt, Paul & Faber, Vance & G. Harris, David. (2018).
        Bounds and algorithms for $k$-truss.
        https://arxiv.org/abs/1806.05523v1
Reference:
        Burkhardt, Paul & Faber, Vance & G. Harris, David. (2018).
        Bounds and algorithms for $k$-truss.
        https://arxiv.org/abs/1806.05523v1
Reference : Burkhardt Paul & Faber Vance & G . Harris David . ( 2018 ) . Bounds and algorithms for $k$ - truss . https : // arxiv . org / abs / 1806 . 05523v1
def _compute_node_truss_levels(graph: nx.Graph) -> Dict[str, int]:
    """
    Reference:
        Burkhardt, Paul & Faber, Vance & G. Harris, David. (2018).
        Bounds and algorithms for $k$-truss.
        https://arxiv.org/abs/1806.05523v1
    """
    max_edge_ks = {}
    is_removed = collections.defaultdict(int)
    triangle_counts = {
        edge: len(set(graph.neighbors(edge[0])) & set(graph.neighbors(edge[1])))
        for edge in graph.edges()
    }
    # rather than iterating over all theoretical values of k
    # let's break out early once all edges have been removed
    # max_edge_k = math.ceil(math.sqrt(len(triangle_counts)))
    # for k in range(1, max_edge_k):
    k = 1
    while True:
        to_remove = collections.deque(
            edge
            for edge, tcount in triangle_counts.items()
            if tcount < k and not is_removed[edge]
        )
        while to_remove:
            edge = to_remove.popleft()
            is_removed[edge] = 1
            for nbr in set(graph.neighbors(edge[0])) & set(graph.neighbors(edge[1])):
                for node in edge:
                    nbr_edge = (node, nbr)
                    try:
                        triangle_counts[nbr_edge] -= 1
                    except KeyError:
                        # oops, gotta reverse the node ordering on this edge
                        nbr_edge = (nbr, node)
                        triangle_counts[nbr_edge] -= 1
                    if triangle_counts[nbr_edge] == k - 1:
                        to_remove.append(nbr_edge)
                        is_removed[nbr_edge] = 1
            max_edge_ks[edge] = k - 1
        # here's where we break out early, if possible
        if len(is_removed) == len(triangle_counts):
            break
        else:
            k += 1
    max_node_ks = {
        node: max(k for edge, k in max_edge_ks.items() if node in edge)
        for node in graph.nodes()
    }
    return max_node_ks
def _compute_node_truss_levels ( graph : nx . Graph ) -> Dict [ str , int ] : max_edge_ks = { } is_removed = collections . defaultdict ( int ) triangle_counts = { edge : len ( set ( graph . neighbors ( edge [ 0 ] ) ) & set ( graph . neighbors ( edge [ 1 ] ) ) ) for edge in graph . edges ( ) } # rather than iterating over all theoretical values of k # let's break out early once all edges have been removed # max_edge_k = math.ceil(math.sqrt(len(triangle_counts))) # for k in range(1, max_edge_k): k = 1 while True : to_remove = collections . deque ( edge for edge , tcount in triangle_counts . items ( ) if tcount < k and not is_removed [ edge ] ) while to_remove : edge = to_remove . popleft ( ) is_removed [ edge ] = 1 for nbr in set ( graph . neighbors ( edge [ 0 ] ) ) & set ( graph . neighbors ( edge [ 1 ] ) ) : for node in edge : nbr_edge = ( node , nbr ) try : triangle_counts [ nbr_edge ] -= 1 except KeyError : # oops, gotta reverse the node ordering on this edge nbr_edge = ( nbr , node ) triangle_counts [ nbr_edge ] -= 1 if triangle_counts [ nbr_edge ] == k - 1 : to_remove . append ( nbr_edge ) is_removed [ nbr_edge ] = 1 max_edge_ks [ edge ] = k - 1 # here's where we break out early, if possible if len ( is_removed ) == len ( triangle_counts ) : break else : k += 1 max_node_ks = { node : max ( k for edge , k in max_edge_ks . items ( ) if node in edge ) for node in graph . nodes ( ) } return max_node_ks