Random Vertex Attribute Assignment

  • Share this:

Code introduction


This function assigns random integer attributes to all vertices of a graph-tool graph object.


Technology Stack : Graph-tool

Code Type : Function

Code Difficulty : Intermediate


                
                    
def random_vertex_attributes(graph, attribute_name):
    """
    Assigns random integer attributes to all vertices of the graph.
    """
    import random
    from graph_tool import Graph

    # Ensure the input is a graph
    if not isinstance(graph, Graph):
        raise TypeError("Input must be a graph-tool graph object.")

    # Create a new integer attribute
    graph.new_vertex_attribute(attribute_name, int)

    # Assign random integers to each vertex
    for vertex in graph.vertices():
        graph.vertex_attribute(attribute_name)[vertex] = random.randint(0, 100)

    return graph                
              
Tags: