In this assignment you will be creating a graph from an input data file called graph.txt. 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. There will be one label to a line. Assume that 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, toVertex, and weight. If the weight is not given, assign a default weight of 1 to that edge. After the list of edges there will be a label for the starting vertex. This will be the starting vertex for the Depth First Search and Breadth First Search as well as the starting vertex for the Dijkstra's shortest path algorithm.
Here is the outline of the code that we developed in class that you will be modifying. You will be adding and Edge class. You will be adding the following functions to the Graph class and the following test cases to your main program.
def Graph (object): # get index from vertex label def getIndex (self, label): # get edge weight between two vertices # return -1 if edge does not exist def getEdgeWeight (self, fromVertexLabel, toVertexLabel): # get a list of neighbors that you can go to from a vertex # return empty list if there are none def getNeighbors (self, vertexLabel): # get a copy of the list of vertices def getVertices (self): # determine if the graph has a cycle def hasCycle (self): # return a list of vertices after a topological sort def toposort (self): # prints a list of edges for a minimum cost spanning tree # list is in the form [v1 - v2, v2 - v3, ..., vm - vn] def spanTree (self): # determine shortest path from a single vertex def shortestPath (self, fromVertexLabel): # delete an edge from the adjacency matrix def deleteEdge (self, fromVertexLabel, toVertexLabel): # delete a vertex from the vertex list and all edges from and # to it in the adjacency matrix def deleteVertex (self, vertexLabel): def main(): # test depth first search # test breadth first search # test topological sort # test minimum cost spanning tree # test single source shortest path algorithm main()
The file that you will be turning in will be called Graph.py. The file will have a header of the following form:
# File: Graph.py # Description: # Student Name: # Student UT EID: # Course Name: CS 313E # Unique Number: 53260 # Date Created: # Date Last Modified:
Use the turnin program to submit your Graph.py . The proctor should receive your work by 11 PM on Friday, 03 May 2013. There will be substantial penalties if you do not adhere to the guidelines. There are no extensions to this assignment.
Please read the following chapters from Think Complexity: