Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

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
expand button
Transcribed Image Text: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
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY