def plot_graph(graph, title):
    """
    Plot a graph with node degrees displayed and highlight the node with the highest degree in red.
    
    Parameters:
    - graph: NetworkX graph object
    - title: Title of the plot
    """
    plt.figure(figsize=(12, 12))
    pos = nx.spring_layout(graph, k=1, iterations=50)
    
    # Draw edges
    nx.draw_networkx_edges(graph, pos, alpha=0.2)
    
    # Identify the node with the highest degree
    degrees = dict(graph.degree())
    max_degree_node = max(degrees, key=degrees.get)
    
    # Draw nodes
    node_colors = ['red' if node == max_degree_node else 'skyblue' for node in graph.nodes()]
    nx.draw_networkx_nodes(graph, pos, node_size=500, node_color=node_colors)
    
    # Add labels with node degrees
    labels = {node: f"{node}\nDegree: {degree}" for node, degree in degrees.items()}
    nx.draw_networkx_labels(graph, pos, labels, font_size=10)
    
    plt.title(title)
    plt.axis('off')
    plt.tight_layout()

# ----------------------------#
#   Generate and Analyze Graphs
# ----------------------------#

# Set a fixed random seed for reproducibility
seed = 42

# Generate and plot Erdős-Rényi (ER) Graph
er_graph = nx.erdos_renyi_graph(n=num_nodes, p=er_p, seed=seed)
plot_graph(er_graph, "Erdős-Rényi (ER) Graph with Node Degrees")
plt.show()

# Generate and plot Barabási-Albert (BA) Graph
ba_graph = nx.barabasi_albert_graph(n=num_nodes, m=m, seed=seed)
plot_graph(ba_graph, "Barabási-Albert (BA) Graph with Node Degrees")
plt.show()

# Print summary statistics
print("\nGraph Statistics:")
print(f"ER Graph: {len(er_graph.edges())} edges")
print(f"BA Graph: {len(ba_graph.edges())} edges")


# Generate Erdos-Renyi (ER) and Barabasi-Albert (BA) graphs
er_graph = nx.erdos_renyi_graph(n=15, p=0.2, seed=42)
ba_graph = nx.barabasi_albert_graph(n=15, m=2, seed=42)

# Compute PageRank using NetworkX's predefined implementation
er_pagerank_predefined = nx.pagerank(er_graph, alpha=0.85)
ba_pagerank_predefined = nx.pagerank(ba_graph, alpha=0.85)

# Display PageRank scores for ER Graph
print("PageRank Scores - ER Graph (Predefined):")
for node, rank in sorted(er_pagerank_predefined.items()):
    print(f"Node {node}: {rank:.4f}")

print("\nPageRank Scores - BA Graph (Predefined):")
for node, rank in sorted(ba_pagerank_predefined.items()):
    print(f"Node {node}: {rank:.4f}")


## **5. Analyzing PageRank Distributions**

We'll compare the distributions of PageRank scores by calculating statistical moments:
- **Mean**: Average PageRank score.
- **Variance**: Measure of spread.
- **Skewness**: Asymmetry of the distribution.
- **Kurtosis**: "Peakedness" of the distribution.

These metrics help understand the shape and spread of the PageRank distributions for both graph models.

# Extract PageRank values
er_pagerank_values = np.array(list(er_pagerank.values()))
ba_pagerank_values = np.array(list(ba_pagerank.values()))

# Function to compute statistical moments
def compute_statistics(pagerank_values):
    mean = np.mean(pagerank_values)
    variance = np.var(pagerank_values)
    skewness = pd.Series(pagerank_values).skew()
    kurtosis = pd.Series(pagerank_values).kurt()
    return mean, variance, skewness, kurtosis

# Compute statistics
er_stats = compute_statistics(er_pagerank_values)
ba_stats = compute_statistics(ba_pagerank_values)

# Create a DataFrame for comparison
stats_df = pd.DataFrame({
    'Statistic': ['Mean', 'Variance', 'Skewness', 'Kurtosis'],
    'ER Graph': er_stats,
    'BA Graph': ba_stats
})

print("Statistical Moments of PageRank:")
print(stats_df)

this last code segment is not adjusted to the previous ones please fix this and make it compatible also explain what happens in it since it has not much comments. Also check if all ideas are implementet meaning moment and the oters