Random Graph Generator with Degree Distribution

  • Share this:

Code introduction


This function generates a random graph with a specified number of nodes and edges using the Igraph library, calculates its degree distribution, and then prints it.


Technology Stack : Igraph

Code Type : Function

Code Difficulty : Intermediate


                
                    
import igraph as ig
import random

def random_graph_generator(n_nodes, n_edges):
    """
    Generates a random graph with a specified number of nodes and edges using igraph.
    """
    # Generate a random graph
    graph = ig.Graph.Random(n_nodes, n_edges)
    
    # Calculate the degree distribution of the graph
    degree_distribution = graph.degree_distribution()
    
    # Print the degree distribution
    print(degree_distribution)
    
    return graph                
              
Tags: