Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**Implement the following algorithms for identifying the connected components of a graph and complete the codes of the attached file connected_comp_incomplete.cpp. I need C++ code

image text in transcribed

**Implement the following algorithms for identifying the connected components of a graph and complete the codes of the attached file connected_comp_incomplete.cpp.

I need C++ code only.

Code will be C++

connected_comp_incomplete.cpp:

#include using namespace std;

void make_set(int p[], int x){ p[x] = x; cout

int find_set(int p[], int x){ if (x!=p[x]) return find_set(p, p[x]); return p[x]; }

void _union(int p[], int x, int y){ int a = find_set(p, x); int b = find_set(p, y); p[a] = b; cout struct Edge{ int u, v; };

void connected_components(int p[], int V, vector edges){ /// implement this }

bool same_component(int p[], int u, int v){ /// implement this }

int main(){ /// This code is for an undirected graph

int V, E; /// take the number of vertices V and /// the number of edges E as input from user ...

/// take the E edges as input from the user vector edges; for (int i=0;i

/// create the parent array for a disjoint set of V elements int p[V];

/// call the function connected_components connected_components(p, V, edges);

while(1){ /// take an integer "option" as input. int option; ...

/// if "option" is 1, take another integer x as input, /// check if x is an element of the disjoint set or not, and /// if it is then print the root/representative-element of x if (option==1){ ... }

/// if "option" is 2, take integers x and y as input, /// check if x and y are elements of the disjoint set or not, and /// if they are, print whether they belong to the /// same connected component or not else if (option==2){ ... }

/// if "option" is 3, take integers x and y as input, /// check if x and y are elements of the disjoint set or not, and /// if they are, print if there is a path from x to y or not. else if (option==3){ ... }

/// if "option" is 4, print all the roots of this disjoint set else if (option==4){ ... }

/// if "option" is 5, print the vertices of each connected components /// in each line else if (option==5){ ... /// 0 1 5 /// 2 3 /// 4 }

else { return 0; } }

}

**Only C++ code needed.

Thank you.

Question 2: Implement the following algorithms for identifying the connected components of a graph and complete the codes of the attached file connected_comp_incomplete.cpp. [50\% marks] CONNECTED_COMPONENTS(G) for each vertex v in V[G] do MAKESET(v) for each edge (u,v) in E[G] do if FIND_SET(u)!=FIND_SET(v) then UNION(u,v) SAME_COMPONENT (u,v) if FIND_SET(u)==FIND_SET(v) then return TRUE else return FALSE

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

List the duties of CVBs.

Answered: 1 week ago