<jupyter_start><jupyter_text>In this notebook, we are analyzing a few physical properties of a graph to better understand the graph structure<jupyter_code>import csv
import pandas as pd
from pandas import DataFrame
from rdflib import Graph as RDFGraph
from rdflib.extras.external_graph_libs import rdflib_to_networkx_graph
import networkx as nx
from networkx import Graph as NXGraph
import matplotlib.pyplot as plt
import statistics
import collections
import os
def build_graph(filepath):
    
    df_graph = pd.read_csv(filepath, sep='\t')
    graph = nx.Graph()

    for i in range (len(df_graph)):
        row = df_graph.iloc[i, :]
        graph.add_edge(row[1], row[3])
        graph[row[1]][row[3]]['predicate'] = row[2]
        
    print('Graph loaded successfully.')
    return graph
        
        
def draw_graph(graph):
    
    options = {
        'node_color': 'green',
        'edge_color': 'black',
        'node_size': 10,
        'width': 0.1,
        'alpha': 1,
    }
    plt.figure(figsize=(10,10))
    nx.draw(graph, **options, with_labels=False)
    

def calculate_clustering_coef(graph):

    clustering_coefficients = []
    global_clustering_coefficients = nx.clustering(graph)
    for node, cc in global_clustering_coefficients.items():
        clustering_coefficients.append({'node': node,
                                        'clustering coefficient': cc})

    for row in clustering_coefficients:
        print('{}: {}'.format(row['node'], row['clustering coefficient']))
        
        
def average_degree(E,V):
    return 2*E/V


def calculate_subgraphs(graph):
    
    subgraph_list = 0

    for conn_component in nx.connected_components(graph):
        subgraph_list += 1

    return subgraph_list<jupyter_output><empty_output><jupyter_text>Loading 'yagoWikipediaInfo_fa.tsv', a subgraph of YAGO graph<jupyter_code>yagoGraph = build_graph('yagoWikipediaInfo_fa.tsv')
edge_count = yagoGraph.number_of_edges()
node_count = yagoGraph.number_of_nodes()

print('================= GRAPH PROPERTIES =================\n')
print(f'Number of edges: {edge_count}')
print(f'Number of nodes: {node_count}')
print(f'Graph density: {nx.classes.function.density(yagoGraph)}')
print(f'Average degree: {average_degree(edge_count, node_count)}')
print(f'Number of subgraphs: {calculate_subgraphs(yagoGraph)}')
def plot_rand_freq_plot():
    
    rf_table = []
    degree = []
    frequency = []
    degree_distribution = collections.Counter(degrees)
    degree_distribution_list = degree_distribution.most_common(len(degree_distribution))
    top_frequency = degree_distribution_list[0][1]
    for index, item in enumerate(degree_distribution_list, start=1):
        rf_table.append({'rank': index,
                         'degree': item[0],
                         'frequency': item[1],
                         'relative frequency':'1/{}'.format(index)})

    freq_list = list(degree_distribution.values())

    rank = [row['rank'] for row in rf_table]
    freq = [row['frequency'] for row in rf_table]

    plt.xlabel("Rank")
    plt.ylabel("Frequency")
    plt.loglog(rank, freq, basex=10)

plot_rand_freq_plot()
print("|  Rank    |  degree    |  Frequency |Zipf fraction|")
format_string = "|{:10}|{:12}|{:12.0f}|{:>12}|"
for index, item in enumerate(rf_table, start=1):
    print(format_string.format(item["rank"],
                               item["degree"],
                               item["frequency"],
                               item["relative frequency"]))<jupyter_output>|  Rank    |  degree    |  Frequency |Zipf fraction|
|         1|           1|      462768|         1/1|
|         2|           5|       59551|         1/2|
|         3|           7|       58790|         1/3|
|         4|           6|       53329|         1/4|
|         5|           4|       51002|         1/5|
|         6|           3|       39041|         1/6|
|         7|           8|       31612|         1/7|
|         8|           9|       23869|         1/8|
|         9|          10|       19492|         1/9|
|        10|          11|       16808|        1/10|
|        11|          12|       12090|        1/11|
|        12|           2|       11073|        1/12|
|        13|          13|       10074|        1/13|
|        14|          14|        8364|        1/14|
|        15|          15|        6950|        1/15|
|        16|          16|        5781|        1/16|
|        17|          17|        5062|        1/17|
|        18|          18|        4344|        1/18|
|        19[...]
{'fraction_non_alphanumeric': 0.12624364359938095, 'fraction_numerical': 0.04908246738890117, 'mean_word_length': 1.992063492063492, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '574318', 'license': 'Apache-2.0', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1415, 'n_words': 313, 'path': 'graph_analysis/Graph analysis.ipynb', 'repo_name': 'ashikbarua/knowledge_graph_alignment', 'size': '4523'}