In this assignment you will be creating a graph from an input gif file called dag.gif. You will complete the topo.txt file.
The first line in that file will be a single integer v. This number will denote the number of vertices to follow. The next v lines will be the labels for the vertices in alphabetical order. There will be one label to a line. The labels are unique. The next line after the labels for vertices will be a single number e. This number will denote the number of edges to follow. There will be one edge per line. Each edge will be of the form - fromVertex and toVertex. Assign a default weight of 1 to each edge.
Here is the outline of the code that we developed in class that you will be modifying. You will use topo.txt instead of graph.txt as your input file. You can add an Edge class if you want to. You may use any of the functions that you wrote for the GraphTraversal class in your last assignment. You can add more functions as needed. You will first test if the given Graph does not contain a cycle and then do a topological sort on that Graph. For your output, the vertices on a given level must be printed in alphabetical order.
class Graph (object): # determine if a directed graph has a cycle # this function should return a boolean and not print the result def has_cycle (self): # return a list of vertices after a topological sort # this function should not print the list def toposort (self): def main(): # create a Graph object theGraph = Graph() # test if a directed graph has a cycle if (theGraph.has_cycle()): print ("The Graph has a cycle.") else: print ("The Graph does not have a cycle.") # test topological sort if (not theGraph.has_cycle()): vertex_list = theGraph.toposort() print ("\nList of vertices after toposort") print (vertex_list) main()
For the data file given, your output will look as follows:
The Graph does not have a cycle. List of vertices after toposort ['m', 'n', 'p', 'o', 'q', 's', 'r', 'u', 'y', 't', 'v', 'w', 'x', 'z']
For this assignment you may work with a partner. Both of you must read the paper on Pair Programming. Only one of you will submit the program. Make sure you have your partner's name and UT EID in the header of your program on HackerRank.
Use the HackerRank system to submit your program. We should receive your work by 11 PM on Monday, 10 Aug 2020. There will be substantial penalties if you do not adhere to the guidelines. HackerRank will not assign late penalties (if any), we will make those adjustments.