please see image for instructions starter code for Main.java import java.io.*; import java.util.*;; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(countConnectedComponents("data.txt")); } public static int countConnectedComponents(String fileName) { try { //the file to be opened for reading FileInputStream fis=new FileInputStream(fileName); Scanner sc=new Scanner(fis); //file to be scanned //returns true if there is another line to read ArrayList edge = new ArrayList(); Set set = new HashSet(); while(sc.hasNextLine()) { int temp [] = new int[2]; int index = 0; for(String s : sc.nextLine().split(" ")) { temp[index] = Integer.parseInt(s); set.add(s); index++; } edge.add(temp); } sc.close(); //closes the scanner int n = set.size(); int[] root = new int[n]; // initialize each node is an island for(int i=0; i
please see image for instructions
starter code for Main.java
import java.io.*;
import java.util.*;;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(countConnectedComponents("data.txt"));
}
public static int countConnectedComponents(String fileName)
{
try
{
//the file to be opened for reading
FileInputStream fis=new FileInputStream(fileName);
Scanner sc=new Scanner(fis); //file to be scanned
//returns true if there is another line to read
ArrayList<int []> edge = new ArrayList<int[]>();
Set<String> set = new HashSet<String>();
while(sc.hasNextLine())
{
int temp [] = new int[2];
int index = 0;
for(String s : sc.nextLine().split(" "))
{
temp[index] = Integer.parseInt(s);
set.add(s);
index++;
}
edge.add(temp);
}
sc.close(); //closes the scanner
int n = set.size();
int[] root = new int[n];
// initialize each node is an island
for(int i=0; i<n; i++){
root[i]=i;
}
int count = n;
for(int i = 0; i < edge.size(); i++)
{
int x = edge.get(i)[0];
int y = edge.get(i)[1];
int xRoot = root[x - 1];
int yRoot = root[y - 1];
if(xRoot!=yRoot){
count--;
root[xRoot]=yRoot;
}
}
return count;
}
catch(IOException e)
{
e.printStackTrace();
}
return 0;
}
}
![A graph is made up of a set of nodes called vertices and a set of lines called edges that connect the nodes. Informally, a connected
component of a graph is a subset in which each pair of nodes is connected with each other via a path. For example, the following graph
consists of two connected components (subsets).
For this assignment, you are required to find out the number of connected components in a graph as specified below:
implement the following method:
public static int countConnected Components(String fileName): where fileName is the input data file representing a graph: each line in the
data file contains two numbers representing a pair of vertices that are connected (an edge between these two nodes exists). For example,
given the following data file:
1
2
4
6
6
2
3
3
5
8
7
Calling countConnected Components should return 3, because there are three connected components in the graph as shown below:
6
7
5](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6e916346-eb1f-468f-a417-f3db3524f0d2%2Fb3318e75-ac48-445d-878d-ef73fa496737%2F6780upa_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)