Skip to content

Code to Sample From Networkx Graphs #1

@agrawalraj

Description

@agrawalraj

import numpy as np
from networkx import barabasi_albert_graph
from causaldag import DAG

def directed_random_graph(nnodes, random_graph_model):
edges = random_graph_model(nnodes).edges
random_permutation = np.arange(nnodes)
np.random.shuffle(random_permutation)
arcs = []
for edge in edges:
node1, node2 = edge
node1_position = np.where(random_permutation == node1)[0][0]
node2_position = np.where(random_permutation == node2)[0][0]
if node1_position < node2_position:
source = node1
endpoint = node2
else:
source = node2
endpoint = node1
arcs.append((source, endpoint))
return DAG(nodes=set(range(nnodes)), arcs=arcs)

def directed_barbasi(nnodes, nattach):
random_graph_model = lambda nnodes: barabasi_albert_graph(nnodes, nattach)
return directed_random_graph(nnodes, random_graph_model)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions