Answered step by step
Verified Expert Solution
Link Copied!

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:
(1) implement code to print an adjacency list of a graph;
(2) 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 (1), and
print minimum spanning tree (MST) of the graph (2).
Following is the output of the program run applied on provided test file:EdgeWeight(0,5)2(5,2)1(0,4)1(5,3)3(1,2)4
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 code/ideas 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 __init__(self): self.verList ={} self.numVertices =0 class __Vertex: def __init__(self, key): self.id = key self.connectedTo ={} def getId(self): return self.id def getConnections(self): return self.connectedTo.keys() def getWeight(self, nbr): return self.connectedTo[nbr] def addNeighbor(self, nbr, weight =0): self.connectedTo[nbr]= weight def __str__(self): return f"connected to: {str([x.id for x in self.connectedTo])}" def addVertex(self, key): self.numVertices +=1 newVertex = Graph.__Vertex(key) self.verList[key]= newVertex return newVertex def getVertex(self, n): if n in self.verList: return self.verList[n] else: return None def __contains__(self, n): return n in self.verList def addEdge(self, source, destination, weight =0): if source not in self.verList: newVertex = self.addVertex(source) if destination not in self.verList: newVertex = self.addVertex(destination) self.verList[source].addNeighbor(self.verList[destination], weight) def getVertices(self): return self.verList.keys() def __iter__(self): return iter(self.verList.values()) def dfs(self, s, visited = None): if visited is None: visited = set() if s not in visited: print(s, end ="") visited.add(s) for next_node in [x.id for x in self.verList[s].connectedTo]: self.dfs(next_node, visited) def bfs(self, s, visited = None): if visited is None: visited = set() q = Queue() q.put(s) visited.add(s) while not q.empty(): current_node = q.get() print(current_node, end ="") for next_node in [x.id for x in self.verList[current_node].connectedTo]: if next_node not in visited: q.put(next_node) visited.add(next_node) def kruskals(self): vertices_sets = set() edges_dict = 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 = open(sys.argv[1],"r") for line in file: values = line.split() graph.addEdge(int(values[0]), int(values[1]), int(values[2])) graph.addEdge(int(values[1]), int(values[0]), int(values[2])) # print adjacency list representation of the graph print() ### WRITE YOUR CODE HERE ### # create graph MST MST = graph.kruskals() # print graph MST print() print("Graph MST:") print("Edge\t\tWeight") for edge in MST: print(f"{edge[0]}\t\t{edge[1]}")main()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions