This is a sample for http://blog.abyz.ir
At first we import all needed dependencies
%matplotlib inline
import numpy as np
import random
import networkx as nx
import matplotlib.pyplot as plt
from nxpd import draw
from nxpd import nxpdParams
import copy
nxpdParams['show'] = 'ipynb'
This section generates the DAG
def generateDAG(nodeNo, edgeProb):
adj = np.zeros((nodeNo, nodeNo))
G=nx.DiGraph()
deps = dict()
nodeLables = list(range(nodeNo))
addedLabels = list()
for i in range(nodeNo):
lIdx = random.randrange(len(nodeLables))
lable = nodeLables[lIdx]
addedLabels.append(lable)
nodeLables.remove(lable)
G.add_node(str(addedLabels[i]))
deps[str(addedLabels[i])] = set()
for j in range(i):
rnd = random.random()
if rnd < edgeProb and i != j:
G.add_edge(str(addedLabels[j]), str(addedLabels[i]))
deps[str(addedLabels[i])].add(str(addedLabels[j]))
adj[i,j]= 1
return (adj, G, deps)
In this section we just generate and draw DAG
nodeCount = 10
dependencyProbability= 0.7
dag, G, deps = generateDAG(nodeCount,dependencyProbability)
draw(G)
It is easy to generate and draw a DAG in jupyter notebook