11import  networkx  as  nx 
2- import  os 
3- import  json 
2+ 
43
54def  get_top_n_central_nodes (centrality_dict , n ):
65    """Sort nodes based on centrality measure and return top N nodes. 
7-      
6+ 
87    Args: 
98        centrality_dict: Dictionary of nodes with their centrality values. 
109        n: Number of top nodes to return. 
11-      
10+ 
1211    Returns: 
1312        Sorted list of top N nodes with their centrality values. 
1413    """ 
1514    # sorted_nodes = sorted(centrality_dict.items(), key=lambda item: item[1], reverse=True) 
1615    # return sorted_nodes[:n] 
17-     sorted_nodes  =  sorted (centrality_dict .items (), key = lambda  item : item [1 ], reverse = True )
16+     sorted_nodes  =  sorted (
17+         centrality_dict .items (), key = lambda  item : item [1 ], reverse = True 
18+     )
1819    return  [node  for  node , _  in  sorted_nodes [:n ]]
1920
21+ 
2022def  analyze_graph_structure (G ):
2123    """Analyzes the structure of a knowledge graph and provides hopefully useful information. 
2224    Currently, I am not sure how to use most of the information, but we may find a way to use it 
@@ -32,7 +34,7 @@ def analyze_graph_structure(G):
3234    num_nodes  =  G .number_of_nodes ()  # Total number of nodes 
3335    num_edges  =  G .number_of_edges ()  # Total number of edges 
3436
35- # Degree Distribution 
37+      # Degree Distribution 
3638    degree_distribution  =  dict (G .degree ())
3739    # Degree distribution can indicate the presence of hubs or important nodes 
3840
@@ -51,7 +53,6 @@ def analyze_graph_structure(G):
5153    - Degree Centrality: node1 = 0.33(1/3), node2 = 0.66(2/3), node3 = 0.33(1/3) 
5254    """ 
5355
54- 
5556    # Betweenness Centrality: Measures node's control over information flow 
5657    betweenness_centrality  =  nx .betweenness_centrality (G )
5758    """ 
@@ -70,7 +71,7 @@ def analyze_graph_structure(G):
7071    - Betweenness Centrality show the dependency of the network on a node 
7172
7273    """ 
73-      
74+ 
7475    # eigenvector centrality measures the influence of a node in a network 
7576    eigenvector_centrality  =  nx .eigenvector_centrality (G )
7677
@@ -115,7 +116,12 @@ def analyze_graph_structure(G):
115116    closeness_centrality  =  get_top_n_central_nodes (nx .closeness_centrality (G ), n )
116117
117118    # Find intersection of top nodes from all measures (set intersection) 
118-     all_centrality_nodes  =  set (degree_centrality ) &  set (betweenness_centrality ) &  set (eigenvector_centrality ) &  set (closeness_centrality )
119+     all_centrality_nodes  =  (
120+         set (degree_centrality )
121+         &  set (betweenness_centrality )
122+         &  set (eigenvector_centrality )
123+         &  set (closeness_centrality )
124+     )
119125
120126    top_nodes  =  list (all_centrality_nodes )[:6 ]
121127
0 commit comments