Question
Sample output input:Reading input values from test1.txt. output:Does Prim MST = Kruskal MST? true code template: import edu.princeton.cs.algs4.*; import java.util.Scanner; import java.io.File; //Do not change
Sample output
input:Reading input values from test1.txt.
output:Does Prim MST = Kruskal MST? true
code template:
import edu.princeton.cs.algs4.*; import java.util.Scanner; import java.io.File;
//Do not change the name of the PrimVsKruskal class public class PrimVsKruskal{
/* PrimVsKruskal(G) Given an adjacency matrix for connected graph G, with no self-loops or parallel edges, determine if the minimum spanning tree of G found by Prim's algorithm is equal to the minimum spanning tree of G found by Kruskal's algorithm. If G[i][j] == 0.0, there is no edge between vertex i and vertex j If G[i][j] > 0.0, there is an edge between vertices i and j, and the value of G[i][j] gives the weight of the edge. No entries of G will be negative. */ static boolean PrimVsKruskal(double[][] G){ int n = G.length;
/* Build the MST by Prim's and the MST by Kruskal's */ /* (You may add extra methods if necessary) */ /* ... Your code here ... */ /* Determine if the MST by Prim equals the MST by Kruskal */ boolean pvk = true; /* ... Your code here ... */
return pvk; } /* main() Contains code to test the PrimVsKruskal function. You may modify the testing code if needed, but nothing in this function will be considered during marking, and the testing process used for marking will not execute any of the code below. */ public static void main(String[] args) { Scanner s; if (args.length > 0){ try{ s = new Scanner(new File(args[0])); } catch(java.io.FileNotFoundException e){ System.out.printf("Unable to open %s ",args[0]); return; } System.out.printf("Reading input values from %s. ",args[0]); }else{ s = new Scanner(System.in); System.out.printf("Reading input values from stdin. "); } int n = s.nextInt(); double[][] G = new double[n][n]; int valuesRead = 0; for (int i = 0; i Input: An n x n array G, of type double, representing an edge-weighted graph. Output: A boolean value which is true if the Prim's MST equals the Kruskal's MST and false otherwise. A correct implementation of the PrimVsKruskal class will find the minimum weight spanning tree using the textbook's implementation of Prim's algorithm (eager version) and the minimum weight spanning tree using the textbook's implementation of Kruskal's algorithm and compare. If they are the same it returns true, otherwise false. You must use the provided Java template as the basis of your submission, and put your implementation inside the PrimVsKruskal method in the template. You may not change the name, return type or parameters of the PrimVsKruskal method. You may add additional methods as needed. You may use any of the classes provided by the textbook as your code will be run with the algs4.jar file. Most likely you will use some or all of the following: UF class, IndexMinPQ class, MinPQ class, Edge class, Queue class, etc. The main method in the template contains code to help you test your implementation by reading an adjacency matrix from a file. You may modify the main method to help with testing, but only the contents of the PrimVsKruskal method (and any methods you have added) will be marked, since the main function will be deleted before marking begins. Please read through the comments in the template file before starting. I foresee three ways of solving this problem, each increasingly more difficult and worth increasingly more marks. (1) The simplest way to solve this problem is convert the adjacency matrix into an EdgeWeightedGraph object, running PrimMST and KruskalMST on the graph and then comparing the output trees to one another. (2) Another way to solve the problem is to leave the graph as adjacency matrix and modifying the code of PrimMST and KruskalMST (inside your PrimVsKruskal class) to work directly with the adjacency matrix. You would still generate the two MSTs and compare them. (3) The most difficult but most efficient way to solve it also involves working directly with the adjacency matrix but here you build the two trees concurrently, testing the consistency of the trees after adding each edge. We will call this an early detection system, which will recognize if two trees are unequal before completion of the trees Input: An n x n array G, of type double, representing an edge-weighted graph. Output: A boolean value which is true if the Prim's MST equals the Kruskal's MST and false otherwise. A correct implementation of the PrimVsKruskal class will find the minimum weight spanning tree using the textbook's implementation of Prim's algorithm (eager version) and the minimum weight spanning tree using the textbook's implementation of Kruskal's algorithm and compare. If they are the same it returns true, otherwise false. You must use the provided Java template as the basis of your submission, and put your implementation inside the PrimVsKruskal method in the template. You may not change the name, return type or parameters of the PrimVsKruskal method. You may add additional methods as needed. You may use any of the classes provided by the textbook as your code will be run with the algs4.jar file. Most likely you will use some or all of the following: UF class, IndexMinPQ class, MinPQ class, Edge class, Queue class, etc. The main method in the template contains code to help you test your implementation by reading an adjacency matrix from a file. You may modify the main method to help with testing, but only the contents of the PrimVsKruskal method (and any methods you have added) will be marked, since the main function will be deleted before marking begins. Please read through the comments in the template file before starting. I foresee three ways of solving this problem, each increasingly more difficult and worth increasingly more marks. (1) The simplest way to solve this problem is convert the adjacency matrix into an EdgeWeightedGraph object, running PrimMST and KruskalMST on the graph and then comparing the output trees to one another. (2) Another way to solve the problem is to leave the graph as adjacency matrix and modifying the code of PrimMST and KruskalMST (inside your PrimVsKruskal class) to work directly with the adjacency matrix. You would still generate the two MSTs and compare them. (3) The most difficult but most efficient way to solve it also involves working directly with the adjacency matrix but here you build the two trees concurrently, testing the consistency of the trees after adding each edge. We will call this an early detection system, which will recognize if two trees are unequal before completion of the trees
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