Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code to Sample From Networkx Graphs #1

Open
agrawalraj opened this issue Nov 22, 2019 · 0 comments
Open

Code to Sample From Networkx Graphs #1

agrawalraj opened this issue Nov 22, 2019 · 0 comments

Comments

@agrawalraj
Copy link

agrawalraj commented Nov 22, 2019

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant