This is a Directed Acyclic Graph Tutorial

This is a sample for http://blog.abyz.ir

Importing nedded dependencies

At first we import all needed dependencies

In [2]:
%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'

Generating DAG

This section generates the DAG

In [3]:
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)

Generate and Draw the DAG

In this section we just generate and draw DAG

In [5]:
nodeCount = 10
dependencyProbability= 0.7
dag, G, deps = generateDAG(nodeCount,dependencyProbability)
draw(G)
Out[5]:

It is easy to generate and draw a DAG in jupyter notebook