0% found this document useful (0 votes)
27 views2 pages

Introduction To Data Structure - Python Assignment

The document describes an assignment to write a program that reads a connected graph from a file, finds the minimum spanning tree of that graph, and displays the spanning tree and its total weight. The program should prompt the user for a file, read the graph data from the file including the number of vertices and weighted edges, use that data to create a WeightedGraph instance, find the minimum spanning tree using getMinimumSpanningTree(), and output the weight and print out the spanning tree.

Uploaded by

codapab935
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Introduction To Data Structure - Python Assignment

The document describes an assignment to write a program that reads a connected graph from a file, finds the minimum spanning tree of that graph, and displays the spanning tree and its total weight. The program should prompt the user for a file, read the graph data from the file including the number of vertices and weighted edges, use that data to create a WeightedGraph instance, find the minimum spanning tree using getMinimumSpanningTree(), and output the weight and print out the spanning tree.

Uploaded by

codapab935
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment

1. (Finding a minimum spanning tree) Write a program that reads a connected graph from a
file and displays its minimum spanning tree. The first line in the file contains a number
that indicates the number of vertices (n). The vertices are labeled as 0, 1, ..., n-1. Each
subsequent line describes the edges in the form of u1, v1, w1 | u2, v2, w2 | .... Each
triplet in this form describes an edge and its weight. Figure 1 shows an example of the
file for the corresponding graph. Note that we assume the graph is undirected. If the
graph has an edge (u, v), it also has an edge (v, u). Only one edge is represented in the
file. When you construct a graph, both edges need to be considered.

Figure 1 The vertices and edges of a weighted graph can be stored in a

Your program should prompt the user to enter the name of the file, should read data from
a file, create an instance g of WeightedGraph, invoke g.printWeightedEdges() to
display all edges, invoke getMinimumSpanningTree() to obtain an instance tree of
WeightedGraph.MST, invoke tree.getTotalWeight() to display the weight of the
minimum spanning tree, and invoke tree.printTree() to display the tree. Here is a sample
run of the program:

(Hint: Use new WeightedGraph(list, numberOfVertices) to create a graph, where list


contains a list of WeightedEdge objects. Use new WeightedEdge(u, v, w) to create an
edge. Read the first line to get the number of vertices. Read each subsequent line into a
string s and use s.split("[\\|]") to extract the triplets. For each triplet, triplet.split("[,]")
to extract vertices and weight.)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy