<jupyter_start><jupyter_text>average degree<jupyter_code>def average_degree(num_edges, num_nodes):
  # Input: number of edges
  # and number of nodes, and 
  # output : returns the average node degree of 
  # the graph. Round the result to nearest integer (for example 
  # 3.3 will be rounded to 3 and 3.7 will be rounded to 4)

    avg_degree = 0

    ############# Your code here ############
    avg_degree = round(2 * num_edges / num_nodes)
    #########################################

    return avg_degree

num_edges = G.number_of_edges()
num_nodes = G.number_of_nodes()
print("Num edges:", num_edges, "Num nodeds:", num_nodes)
avg_degree = average_degree(num_edges, num_nodes)
print("Average degree of Flights network is {}".format(avg_degree))<jupyter_output>Num edges: 2308 Num nodeds: 286
Average degree of Flights network is 16<jupyter_text>average clustering coefficient<jupyter_code>def average_clustering_coefficient(G):
  # Input:  takes a nx.Graph
  # output : returns the average clustering coefficient. Round 
  # the result to 2 decimal places (for example 3.333 will
  # be rounded to 3.33 and 3.7571 will be rounded to 3.76)
    

    avg_cluster_coef = 0

    ############# Your code here ############
    ## Note: 
    ## 1: Please use the appropriate NetworkX clustering function
    avg_cluster_coef = nx.average_clustering(G)
    avg_cluster_coef = round(avg_cluster_coef, 2)
    #########################################

    return avg_cluster_coef

avg_cluster_coef = average_clustering_coefficient(G)
print("Average clustering coefficient of karate club network is {}".format(avg_cluster_coef))<jupyter_output>Average clustering coefficient of karate club network is 0.65<jupyter_text>PageRank value for node 0 (node with id 0) after one PageRank iteration PageRank equation: $r_j = \sum_{i \rightarrow j} \beta \frac{r_i}{d_i} + (1 - \beta) \frac{1}{N}$<jupyter_code>def one_iter_pagerank(G, beta, r0, node_id):
  # Input :  takes a nx.Graph, beta, r0 and node id.
  # o/p : The return value r1 is one interation PageRank value for the input node.
  # Please round r1 to 2 decimal places.

    r1 = 0

    ############# Your code here ############
    ## Note: 
    ## 1: You should not use nx.pagerank
    for neighbor in G.neighbors(node_id):
        r1 += beta * r0 / G.degree[neighbor]
    r1 += (1 - beta) / G.number_of_nodes()
    r1 = round(r1, 2)
    #########################################

    return r1

beta = 0.8
r0 = 1 / G.number_of_nodes()
node = "BIS"
r1 = one_iter_pagerank(G, beta, r0, node)
print("The PageRank value for node {} after one iteration is {}".format(node,r1))

node = "JFK"
r1 = one_iter_pagerank(G, beta, r0, node)
print("The PageRank value for node {} after one iteration is {}".format(node,r1))<jupyter_output>The PageRank value for node BIS after one iteration is 0.0
The PageRank value for node JFK after one iteration is 0.01<jupyter_text>closeness centralityThe equation for closeness centrality is $c(v) = \frac{1}{\sum_{u \neq v}\text{shortest path length between } u \text{ and } v}$<jupyter_code>def closeness_centrality(G, node):
  #  calculates closeness centrality 
  # for a node in graph. G is the input graph 
  # network and node is the node id in the graph. Please round the 
  # closeness centrality result to 2 decimal places.

    closeness = 0

    ## Note:
    ## 1: You can use networkx closeness centrality function.
    ## 2: Notice that networkx closeness centrality returns the normalized 
    ## closeness directly, which is different from the raw (unnormalized) 
    ## one that we learned in the lecture.

    closeness = nx.closeness_centrality(G, u=node)
    closeness /= len(nx.node_connected_component(G, node)) - 1

    closeness = round(closeness, 3)
    #########################################

    return closeness

node = "BIS"
closeness = closeness_centrality(G, node=node)
print("The node {} in the network has closeness centrality {}".format(node,closeness))

node = "ATL"
closeness = closeness_centrality(G, node=node)
print("The node {} in the network has closeness centrality {}".format(node,closeness))
import tensorflow as tf
flights.head()
flights['UniqueCarrier'].value_counts()
flights['FlightNum'].value_counts()
flights['TailNum'].value_counts()
flights[(flights['_from']=='JFK') & (flights['_to']=='ATL')]
nx.shortest_path(G, "JFK", "BIS")
sorted(nx.common_neighbors(G, "JFK", "BIS"))
def neighborhood_overlap(g, u, v):
    """
    https://stackoverflow.com/questions/49733244/how-can-i-calculate-neighborhood-overlap-in-weighted-network
    """
    n_common_nbrs = len(set(nx.common_neighbors(g, u, v)))
    n_join_nbrs = g.degree(u) + g.degree(v) - n_common_nbrs - 2
    return n_common_nbrs / n_join_nbrs
u='JFK'
v='BIS'
print("Neighbourhood overlap b/w {} and {} is : ".format(u,v), neighborhood_overlap(G, u, v))
def jaccard_coefficient(g, u, v):
    return neighborhood_overlap(g, u, v)/len(set(G.neighbors(u).union(set(G.neighbors(v))))
len(set(G.neighbors(u).union(set(G.neighbors(v)))))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15504950495049505, 'fraction_numerical': 0.01782178217821782, 'mean_word_length': 4.514192139737991, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '344969', 'license': 'MIT', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1695, 'n_words': 488, 'path': 'Node Embeddings.ipynb', 'repo_name': 'aman-sawarn/Graph-Networks', 'size': '5050'}