Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a Java program (based on a graph traversal algorithm you've learned in this class) that, for a given undirected graph, outputs: the vertices
Write a Java program (based on a graph traversal algorithm you've learned in this class) that, for a given undirected graph, outputs: the vertices of each connected component. Your programs should take inputs from a file via the command line with the following structure in the input file. Each line of the input file represents a graph. The first number in a line specifies the number of vertices in the graph. Then pairs of vertices define the edges. An example of an input file is as follows: 5 (1,2) (3,4) (3,5) (4,5) 4 (1,2) (2,3) (1,4) It specifies two graphs. The first graph has five vertices (1,2,3,4,5) and four edges. The second graph has four vertices (1,2,3,4) and three edges. Proper output should look (something) like: Graphl: Two connected components: {12} {3 4 5} Graph2: One connected component: [1 2 3 4] You must test your programs on a nontrivial input file (with at least 3 graphs and each graph having 7-10 nodes). Your output should be formatted nicely so that it is easy to read.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Below is a Java program that reads an input file containing multiple undirected graphs and outputs the vertices of each connected component for each g...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