Question: Write a program that reads a connected graph from a file. The graph is stored in a file using the same format specified in Programming
Write a program that reads a connected graph from a file. The graph is stored in a file using the same format specified in Programming Exercise. Your program should prompt the user to enter the name of the file then two vertices, and should display a shortest path between the two vertices. For example, for the graph in Figure, a shortest path between 0 and 1 can be displayed as 0 2 4 3 1.
Here is a sample run of the program:

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 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 added. Your program should prompt the user to enter the name of the file, read data from the 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, use?triplet.split("[,]")?to extract vertices and weight.)

Enter a file name: WeightedGraphSample2.txt Jenter Enter two vertices (integer indexes): 0 1 Erter The number of vertices is 6 Vertex 0: (0, 2, 3) (0, 1, 100) Vertex 1: (1, 3, 20) (1, 0, 100) Vertex 2: (2, 4, 2) (2, 3, 40) (2, 0, 3) Vertex 3: (3, 4, 5) (3, 5, 5) (3, 1, 20) (3, 2, 40) Vertex 4: (4, 2, 2) (4, 3, 5) (4, 5, 9) Vertex 5: (5, 3, 5) (5, 4, 9) A path from 0 to 1: 0 2 4 3 1 Enter a file name: c:\exercise\WeightedGraphSample.txt The number of vertices is 6 Vertex 0: (0, 2, 3) (0, 1, 100) Vertex 1: (1, 3, 20) (1, 0, 100) Vertex 2: (2, 4, 2) (2, 3, 40) (2, 0, 3) Vertex 3: (3, 4, 5) (3, 5, 5) (3, 1, 20) (3, 2, 40) Vertex 4: (4, 2, 2) (4, 3, 5) (4, 5, 9) Vertex 5: (5, 3, 5) (5, 4, 9) Total weight in MST is 35 Root is: 0 Edges: (3, 1) (0, 2) (4, 3) (2, 4) (3, 5) JEnter
Step by Step Solution
3.47 Rating (176 Votes )
There are 3 Steps involved in it
Program Plan Create a class ShortestPathPrint and define main method in it In main method create an object file of class File to read the graph file Start reading the graph file by using Scanner objec... View full answer
Get step-by-step solutions from verified subject matter experts
