Commit 45756293 authored by Victor Zimmermann's avatar Victor Zimmermann
Browse files

Add local clustering coefficient to output.

parent fb132c2d
Loading
Loading
Loading
Loading
+30 −12
Original line number Diff line number Diff line
@@ -891,23 +891,20 @@ def disambiguate_mst(graph: nx.Graph, root_hub_list: list,
##############################


def global_clustering_coefficient(graph: nx.Graph) -> float:
    """Calculates global clustering coefficient from graph.
def local_clustering_coefficient(graph: nx.Graph, node: str) -> float:
    """Calculates local clustering coefficient from node.
    
    Iterates over every node and calculates the global coefficient as a mean
    of every local clustering coefficient. 
    Local Clustering Coefficient is defined as number of edges between
    neighbors of a node, divided by the number of possible nodes.
    
    Args:
        graph: Undirected graph.
        node: Node in graph.
        
    Returns:
        Global coefficient.
        Local coefficient.
    """
    
    local_coefficient_list = list()
    
    for node in graph.nodes:
        
    neighbor_list = graph.adj[node]
    
    neighbor_edge_list = [(x,y) for x in neighbor_list 
@@ -915,7 +912,7 @@ def global_clustering_coefficient(graph: nx.Graph) -> float:
    
    if len(neighbor_edge_list) == 0:
        
            local_coefficient_list.append(0)
        return 0
    
    else:
        
@@ -924,7 +921,28 @@ def global_clustering_coefficient(graph: nx.Graph) -> float:
            if graph.has_edge(x,y):
                edge_count += 1
        
            local_coefficient_list.append(edge_count/len(neighbor_edge_list))
        return edge_count/len(neighbor_edge_list)



def global_clustering_coefficient(graph: nx.Graph) -> float:
    """Calculates global clustering coefficient from graph.
    
    Iterates over every node and calculates the global coefficient as a mean
    of every local clustering coefficient. 
    
    Args:
        graph: Undirected graph.
        
    Returns:
        Global coefficient.
    """
    
    local_coefficient_list = list()
    
    for node in graph.nodes:
        
        local_coefficient_list.append(local_clustering_coefficient(graph,node))

    return np.mean(local_coefficient_list)

@@ -983,7 +1001,7 @@ def print_stats(stat_dict: dict, graph: nx.Graph) -> None:
    stat_string.append('Tuples gained through merging: {}.'.format(stat_dict['pipe_gain']))
    stat_string.append('Sense inventory:')
    for hub in stat_dict['hubs'].keys():
        stat_string.append(' -> {} ({}): {}.'.format(hub,graph.degree[hub], ", ".join(stat_dict['hubs'][hub])))
        stat_string.append(' -> {} ({}): {}.'.format(hub,local_clustering_coefficient(graph,hub), ", ".join(stat_dict['hubs'][hub])))
    
    print('\n[A] '+'\n[A] '.join(stat_string)+'\n')