-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAG.py
More file actions
40 lines (27 loc) · 929 Bytes
/
DAG.py
File metadata and controls
40 lines (27 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import networkx as nx
import numpy as np
class DAG(nx.DiGraph):
def __init__(self,data):
super(DAG,self).__init__(data)
#Checking for cycles
try:
nx.find_cycle(self)
except nx.NetworkXNoCycle:
pass
else:
raise ValueError("This graph has a directed cycle")
# Adding a single node
def add_node(self,node,weight=None):
super(DAG, self).add_node(node, weight=weight)
#Adding a node from an iterator
def add_nodes_from(self,nodes,weights = None):
if weights:
if len(weights) == len(nodes):
for i in range(len(nodes)):
self.add_node(nodes[i],weights[i])
else:
for i in range(len(nodes)):
self.add_node(nodes[i])
# Adding edges
def add_edge(self,edge):
pass