Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I In this assignment you are to write a Python program to create a minimum spanning tree ( MST ) of a graph using Kruskal's
I In this assignment you are to write a Python program to create a minimum spanning tree MST of a graph using Kruskal's algorithm. You are provided with the template in which you must: implement code to print an adjacency list of a graph; implement a function to construct a minimum spanning tree MST of a given graph using Kruskal's algorithm. Your program must: take a file containing graph vertices, edges, and weights as a command line argument, insert vertices, edges, and weights into the graph, print adjacency list representation of a graph and print minimum spanning tree MST of the graph Following is the output of the program run applied on provided test file:EdgeWeight Together with your source code include a separate file Namingformat.py with your information as the values of the following variables:You must properly cite the sources if you use any help from peers or any codeideas from online resources. Failure to do so will be treated as a plagiarism. Properly citing the sources should be done as a comment in your code. Some examples: mport sysclass Graph: def initself: self.verList self.numVertices class Vertex: def initself key: self.id key self.connectedTo def getIdself: return self.id def getConnectionsself: return self.connectedTo.keys def getWeightself nbr: return self.connectedTonbr def addNeighborself nbr weight : self.connectedTonbr weight def strself: return fconnected to: strxid for x in self.connectedTo def addVertexself key: self.numVertices newVertex Graph.Vertexkey self.verListkey newVertex return newVertex def getVertexself n: if n in self.verList: return self.verListn else: return None def containsself n: return n in self.verList def addEdgeself source, destination, weight : if source not in self.verList: newVertex self.addVertexsource if destination not in self.verList: newVertex self.addVertexdestination self.verListsourceaddNeighborselfverListdestination weight def getVerticesself: return self.verList.keys def iterself: return iterselfverList.values def dfsself s visited None: if visited is None: visited set if s not in visited: prints end visited.adds for nextnode in xid for x in self.verListsconnectedTo: self.dfsnextnode, visited def bfsself s visited None: if visited is None: visited set q Queue qputs visited.adds while not qempty: currentnode qget printcurrentnode, end for nextnode in xid for x in self.verListcurrentnodeconnectedTo: if nextnode not in visited: qputnextnode visited.addnextnode def kruskalsself: verticessets set edgesdict dict MST set ### WRITE YOUR CODE HERE ### return MSTdef main: # create an empty graph graph Graph # get graph vertices & edges from input file and add them to the graph file opensysargvr for line in file: values line.split graph.addEdgeintvalues intvalues intvalues graph.addEdgeintvalues intvalues intvalues # print adjacency list representation of the graph print ### WRITE YOUR CODE HERE ### # create graph MST MST graph.kruskals # print graph MST print printGraph MST: printEdgettWeight for edge in MST: printfedgettedgemain
I
In this assignment you are to write a Python program to create a minimum spanning tree MST of a graph using Kruskal's algorithm.
You are provided with the template in which you must:
implement code to print an adjacency list of a graph;
implement a function to construct a minimum spanning tree MST of a given graph using Kruskal's algorithm.
Your program must:
take a file containing graph vertices, edges, and weights as a command line argument,
insert vertices, edges, and weights into the graph,
print adjacency list representation of a graph and
print minimum spanning tree MST of the graph
Following is the output of the program run applied on provided test file:EdgeWeight
Together with your source code include a separate file
Namingformat.py with your information as the values of the following variables:You must properly cite the sources if you use any help from peers or any codeideas from online resources. Failure to do so will be treated as a plagiarism. Properly citing the sources should be done as a comment in your code. Some examples:
mport sysclass Graph: def initself: self.verList self.numVertices class Vertex: def initself key: self.id key self.connectedTo def getIdself: return self.id def getConnectionsself: return self.connectedTo.keys def getWeightself nbr: return self.connectedTonbr def addNeighborself nbr weight : self.connectedTonbr weight def strself: return fconnected to: strxid for x in self.connectedTo def addVertexself key: self.numVertices newVertex Graph.Vertexkey self.verListkey newVertex return newVertex def getVertexself n: if n in self.verList: return self.verListn else: return None def containsself n: return n in self.verList def addEdgeself source, destination, weight : if source not in self.verList: newVertex self.addVertexsource if destination not in self.verList: newVertex self.addVertexdestination self.verListsourceaddNeighborselfverListdestination weight def getVerticesself: return self.verList.keys def iterself: return iterselfverList.values def dfsself s visited None: if visited is None: visited set if s not in visited: prints end visited.adds for nextnode in xid for x in self.verListsconnectedTo: self.dfsnextnode, visited def bfsself s visited None: if visited is None: visited set q Queue qputs visited.adds while not qempty: currentnode qget printcurrentnode, end for nextnode in xid for x in self.verListcurrentnodeconnectedTo: if nextnode not in visited: qputnextnode visited.addnextnode def kruskalsself: verticessets set edgesdict dict MST set ### WRITE YOUR CODE HERE ### return MSTdef main: # create an empty graph graph Graph # get graph vertices & edges from input file and add them to the graph file opensysargvr for line in file: values line.split graph.addEdgeintvalues intvalues intvalues graph.addEdgeintvalues intvalues intvalues # print adjacency list representation of the graph print ### WRITE YOUR CODE HERE ### # create graph MST MST graph.kruskals # print graph MST print printGraph MST: printEdgettWeight for edge in MST: printfedgettedgemain
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started