Question

4.1.19 Show, in the style of the figure on page 545, a detailed trace of CC for finding the connected components in the graph built by Graph’s input stream constructor for the file tinyGex2.txt (see Exercise 4.1.2).4316631880221111 1 2682-03 10 7 7 1 2 6 5 5 3 8 4

0 0
Add a comment Improve this question Transcribed image text
Answer #1

It's not exactly clear what you want in the question, but I assume you want an algorithm to find the connected components in the graph. So, I will provide you with an algorithm that exactly does that plus I will provide you with a working c++ code that solves the given problem. The code will accept the input as suggested in the input section and will output all the connected components with simple numbering.

So, we can use DFS (Depth-first search)or BFS(Breadth-first search) to find out the connected components in a graph. Basically what we want to do is to isolate the vertices that are connected together. So one easy way to do this is to run a DFS or BFS on the graph and keep the DFS/BFS running as long as we can from a single source, once the BFS/DFS stops running we print all the vertices visited during that run of the DFS/BFS and then we repeat the process for the remaining unvisited vertices.

Code:-

#include <bits/stdc++.h>
#define ll long long int

using namespace std;

vector <ll> graph[100000], now;
ll vis[100000];

inline void dfs(ll x)
{
   vis[x] = true;
   now.push_back(x);
   for(auto it : graph[x])
   {
       if(!vis[it])
           dfs(it);
   }
}

int main()
{  
   ll n, m;
   cin >> n >> m;
   for(ll i = 0; i < m; i++)
   {
       ll x, y;
       cin >> x >> y;
       graph[x].push_back(y);
       graph[y].push_back(x);
   }
   ll idx = 1;
   for(ll i = 1; i <= n; i++)
   {
       if(!vis[i])
       {
           now.clear();
           dfs(i);
           for(auto it : now)
               cout << it << " ";
           cout << endl;
       }
   }
   return 0;
}
The code is kind of self-explanatory and is easy to understand. The algorithm is also fairly simple to understand and you can make changes in then dfs() function to print out the details according to your necessity. However, if you still have problems in understanding the solution then leave a comment down below and I will be more than happy to clear your doubts.

Add a comment
Know the answer?
Add Answer to:
4.1.19 Show, in the style of the figure on page 545, a detailed trace of CC for finding the connected components in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this assignment in C++, please! *** The instructions and programming style detai...

    I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...

  • I am currently trying to figure out the experiment below. Please complete Table 1 with an...

    I am currently trying to figure out the experiment below. Please complete Table 1 with an explanation, I appreciate it thank you!  Promise to give thumbs up! Introduction The phase differences between the output voltage, the voltage across the inductor, the voltage across the capacitor, and the voltage across the resistor will be examined at resonant frequency. The voltage and phase relationship will also be examined for frequencies above and below resonance. Theory An inductor, a capacitor, and a resistor are...

  • This is my process, can you fix it for me? 0 Input: Output: Value: Under control...

    This is my process, can you fix it for me? 0 Input: Output: Value: Under control of main function Under control of main function The purpose of this assignment is to become familiar with the process of providing overloaded operators for a class. The Rational class from Labs 02, 03, and 05 will be modified to provide overloaded operators for performing arithmetic on Rational numbers an overloaded unary minus for negating a Rational number (previously implemented as the additive an...

  • write a detailed summary in an organized format. It must include 3-4 key points of the...

    write a detailed summary in an organized format. It must include 3-4 key points of the controversy. Break up your summary in 3-4 paragraphs. Must include specific reasons as to why a vegetarian diet can be better and or worse than the meat heavy diets? Explain briefly using examples how reading this controversy has helped you in making better dietary choices in future? CONTROVERSY 6 Table of Contents Vegetarian and Meat-Containing Diets: What Are the Benefits and Pitfalls? Notebook LO...

  • Risk management in Information Security today Everyday information security professionals are bombarded with marketing messages around...

    Risk management in Information Security today Everyday information security professionals are bombarded with marketing messages around risk and threat management, fostering an environment in which objectives seem clear: manage risk, manage threat, stop attacks, identify attackers. These objectives aren't wrong, but they are fundamentally misleading.In this session we'll examine the state of the information security industry in order to understand how the current climate fails to address the true needs of the business. We'll use those lessons as a foundation...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT