Answered step by step
Verified Expert Solution
Question
1 Approved Answer
8. (25 points) Write a C++ program called prim.cpp that implements the Prim's algorithm to calculate the minimum spanning tree (MST) from an input graph.
8. (25 points) Write a C++ program called prim.cpp that implements the Prim's algorithm to calculate the minimum spanning tree (MST) from an input graph. Your program should ask a user for an input file name that includes the input graph information. Then, your program should present the edges to be added one-by-one to construct the MST. In the problem, you can assume that the maximum number of vertices (=nodes) in the input graph is less than or equal to 20. You can also assume that the input graph is always connected. Input file format: This is a sample input file called ti.txt. 1 2 2 2 36 1 34 2 4 3 3 4 1 The first line = 4 in the example) indicates that there are four vertices (=nodes) in the graph. The second line = 5 in the example) presents the number of edges in the graph. The remaining five lines present the edge information (=starting vertex, ending vertex, cost) in the graph. For the homework, you should assume that the first vertex starts from the number 1. Thus, t1.txt describes a graph like below: For the homework, you have to assume that a blank space is used to delimiter the values in the edge information. However, there's no space at the end of each line. If your program does not read the file properly, your program will get no credit. And also, do not assume that the starting vertex is always 1. Your program has to ask the user for the starting vertex. This is a sample run Enter a file name: ./t1.txt Enter the first vertex to start: 1 (1) New edge: 1,2 - cost 2 (2) New edge: 2,4 - cost 3 (3) New edge: 3,4 - cost 1 At the result, the sequence of vertices in an edge is not important. For example, the following is fine (1) New edge: 1, 2 - cost 2 OR (1) New edge: 2,1 - cost 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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