-
Notifications
You must be signed in to change notification settings - Fork 62
/
Directed_Acyclic_Graph.py
52 lines (40 loc) · 1.59 KB
/
Directed_Acyclic_Graph.py
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
41
42
43
44
45
46
47
48
49
50
51
52
import networkx as nx
from matplotlib import pyplot as plt
class DAG:
def __init__(self):
self.graph = nx.DiGraph()
def addEdges(self, edges):
"""Function to add one edge at a time and
check if the graph is acyclic post insertion"""
self.graph.add_edge(edges)
if nx.is_directed_acyclic_graph(self.graph):
pass
else:
raise "Unable to insert " + str(edges) + "This is an Acyclic graph"
self.graph.remove_edge(edges)
def AddSetofEdges(self, listt):
"""Function to all a list of edges and
check is the graph is an DAG for furthur details refer networkx"""
self.graph.add_edges_from(listt)
if nx.is_directed_acyclic_graph(self.graph):
pass
else:
raise "This is an acyclic graph check your edges"
self.graph.remove_edge(listt)
def Visualise(self, location="home"):
"""It uses Matplotlib to visualise the DAG .
The graph is stored in a PNG format . So name the file accourdingly
eg
>>> DAG.Visualise(home / img.png)"""
if self.graph is None:
return "There is no graph consider adding edges to visualise"
plt.tight_layout()
nx.draw_networkx(self.graph, arrows=True, node_size=800)
plt.savefig(location, format="PNG")
plt.clf()
return "Graph generated"
graph = DAG()
graph.AddSetofEdges([("root", "a"), ("a", "b"),
("a", "e"), ("b", "c"),
("b", "d"), ("d", "e")])
graph.Visualise("Python/DataStructure/Graph/Dag.png")