Question

Preferably in python but java is good tooTask 1: Minimum Spanning Trees For this warm-up task you are to implement any efficient minimum spanning tree algorithm that takes a sequence of edge-weighted graphs and outputs the minimum cost weight of a spanning tree of each Input Format For this assignment we use adjacency matrices with positive integer weights. Here a zero entry at row i and column J indicates that no edge i] exists in the graph. The first line consists of an integer n < 1000 denoting the order of the graph. This is then followed by n lines of n white-space separated integers denoting edge weights The sequence of graphs is terminated by a value n 0, which is not processed 3 0 1 3 1 02 3 2 0 4 0 270 2 0 5 1 7 5 0 3 0 1 3 0 0 1 04 0 0 10 3 0 3 0 0 3 0 0 0 2 4 00 0 2 0 0 3 0 2 0 1 0 0 2 0 1 0 0 OutputFormat Output should be one line for each input graph indicating the minimum cost weighted tree. The sample output for the previous input cases is as follows. 3 6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

It is a python program I used Prim's algorithm

Solution:

import sys

class Graph():

    def __init__(self, vertices):
        self.V = vertices
        self.graph = [[0 for column in range(vertices)]
                    for row in range(vertices)]


    def printMST(self, parent):
        self.tree=0
        for i in range(1,self.V):
            self.tree=self.tree+self.graph[i][parent[i]]
        print("Minimum weighted tree's weight:",self.tree)


    def minKey(self, key, mstSet):


        min = sys.maxsize

        for v in range(self.V):
            if key[v] < min and mstSet[v] == False:
                min = key[v]
                min_index = v

        return min_index


    def primMST(self):


        key = [sys.maxsize] * self.V
        parent = [None] * self.V

        key[0] = 0
        mstSet = [False] * self.V

        parent[0] = -1

        for cout in range(self.V):


            u = self.minKey(key, mstSet)


            mstSet[u] = True


            for v in range(self.V):
                 #write below if in single line in your ide
                if self.graph[u][v] > 0 and mstSet[v] == False and key[v] > self.graph[u][v]:
                        key[v] = self.graph[u][v]
                        parent[v] = u

        self.printMST(parent)

n=int(input('Enter order of the graph:'))
g = Graph(n)
matrix=[]
for i in range(n):
    row=[]
    x=input()
    row=x.split()
    for j in range(n):
        row[j]=int(row[j])
    matrix.append(row)

g.graph = matrix

g.primMST();

Add a comment
Know the answer?
Add Answer to:
Preferably in python but java is good too Task 1: Minimum Spanning Trees For this warm-up...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT