Question: A directed graph is call a Directed Acyclic Graph ( DAG ) if it does have any cycle. For example, figure 1 does not have

A directed graph is call a Directed Acyclic Graph (DAG) if it does have any cycle. For example, figure 1 does not have any cycle in all its paths, therefore, it is DAG graph. While, figure 2 is cyclic graph (not DAG) since there is a cycle ARrFRR
rigure : Lyclic rapI
Write a Python program that reads graph information from a file and check if it is DAG or not.
(1) Write a function that reads and graph information from an input file and store it a dictionary:
a) Reads the edges for a graph from an input file where each line has two strings A and B(to represent a directed edge from node A to node B). Note that the number of nodes is not specified.
\table[[Graph1.txt File,Graph2.txt File],[a b,A B],[a c,CE],[a e,B C],[ad,E D],[cd,E F],[b d,D B],[de,],[ce,]]
b) The graph information is saved in a dictionary myGraph to maintain the name (string) and list of adjacent nodes for each node in a graph {'a': ['b','c','e'],....}. The following table shows the adjacency nodes in figure 1.
\table[[Node Name (key),Adjacent Nodes (value)],[a,b,c,d,e
A directed graph is call a Directed Acyclic Graph

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!