Question: Modify Listing 28.1, TestGraph.java, to create a file representing graph1. The file format is described in Programming Exercise 28.1. Create the file from the array

Modify Listing 28.1, TestGraph.java, to create a file representing graph1. The file format is described in Programming Exercise 28.1. Create the file from the array defined in lines 8?21 in Listing 28.1. The number of vertices for the graph is 12, which will be stored in the first line of the file. The contents of the file should be as follows:12 0 13 5 1023 2134 10 301245 423578 10 503467 6

Listing

57 745 6 8 8479 10 11 98 11 10 24 8

11 11 8 9 10 public class TestGraph { public static void

main(String[] args) { String[] vertices = {"Seattie", "San Francisco", "Los Angeles", "Denver",

Write a program that reads a graph from a file and determines whether the graph is connected. 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, with the format?u v1 v2 ..., describes edges (u,?v1), (u,?v2), and so on. Figure gives the examples of two files for their corresponding graphs.

"Kansas City", "Chicago", "Boston", "New York", "Atlanta", "Miami", "Dallas", "Houston"}; 4 //

Edge array for graph in Figure 28.1 int[][] edges f0, 13, fo,

Your program should prompt the user to enter the name of the file, then it should read data from the file, create an instance?g?of?UnweightedGraph, invoke?g.printEdges()?to display all edges, and invoke?dfs()?to obtain an instance?tree?of?AbstractGraph.Tree. If?tree.getNumberOfVerticesFound()

is the same as the number of vertices in the graph, the graph is connected. Here is a sample run of the program:

3, t0, 5}, {1, 0}, {1, 2}, {1, 3}, {2, 1}, {2,

12 0 13 5 1023 2134 10 301245 423578 10 503467 6 57 745 6 8 8479 10 11 98 11 10 24 8 11 11 8 9 10 public class TestGraph { public static void main(String[] args) { String[] vertices = {"Seattie", "San Francisco", "Los Angeles", "Denver", "Kansas City", "Chicago", "Boston", "New York", "Atlanta", "Miami", "Dallas", "Houston"}; 4 // Edge array for graph in Figure 28.1 int[][] edges f0, 13, fo, 3, t0, 5}, {1, 0}, {1, 2}, {1, 3}, {2, 1}, {2, 3}, {2, 4}, {2, 10}, {3, 0}, {3, 1}, {3, 2}, {3, 4}, {3, 5}, {4, 2}, {4, 3}, {4, 5}, {4, 7}, {4, 8}, {4, 10}, {5, 0}, {5, 3}, {5, 4}, {5, 6}, {5, 7}, {6, 5}, {6, 7}, {7, 4}, {7, 5}, {7, 6}, {7, 8}, {8, 4}, {8, 7}, {8, 9}, {8, 10}, {8, 11}, {9, 8}, {9, 11}, {10, 2}, {10, 4}, {10, 8}, {10, 11}, {11, 8}, {11, 9}, {11, 10} }; { 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Graph graph1 = new UnweightedGraph (vertices, edges); System.out.println("The number of vertices in graph1: + graph1.getSize()); System.out.println("The vertex with index 1 is " + graph1.getVertex(1)); System.out.println("The index for Miami is " + graph1.getIndex("Miami")); System.out.println("The edges for graph1:"); graph1.printEdges(); 24 25 26 27 28 29 30 31 32 // List of Edge objects for graph in Figure 28.3a String[] names = {"Peter", "Jane", "Mark", "Cindy", "Wendy"}; 33 34

Step by Step Solution

3.45 Rating (164 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Output File is created Java Program to create a file for a graph Compile javac grap... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!