Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

its a c++ code using algorithims design and analysis , please help All submissions must be done on e-learning only. class Graph int V; 1/

its a c++ code using algorithims design and analysis , please help image text in transcribed
image text in transcribed
image text in transcribed
All submissions must be done on e-learning only. class Graph int V; 1/ No. of vertices list *adj; public: Graph(int v) this->V - V; adj - new list int>[V]: -Graph() delete n adj; void addEdge (int v, int w) { adj [v].push_back (w); ) }; Answer the following questions using the above class. You are allowed to add public or private member functions as needed. (20 points) Back Edges Implement the member function void Graph: :printAllBackEdges (). This function prints all back edges in the current graph. (20 points) Undirected Graph Implement the member function bool Graph:: isUndirectedGraph(). This function checks if the graph can be treated as an undirected graph. (Hint: if for every edge (u, v) in the graph the edge (v, u) also exists, then the graph can be treated as an undirected graph). (20 points) Given a directed graph, describe an efficient algorithm for checking if there is a vertex v in the graph from which all other vertices are reachable. What is the order of growth of the running time of your algorithm in the worst case? (Note: inefficient algorithms will receive partial credit). (15 points) Connected Components (1) Consider the following modified version of class Graph for representing undirected graphs: class Graph int V; No. of vertices list int> *ad); 1/ Each vertex belongs to a connected component in the graph. // id[01 stores the id of the connected component vertex O belongs to, // Id[1] stores the component id for vertex 1. etc. Widtul id[v] if and only if u and y belong to the same component int* id: publie: Graph(int V) this->V - Vi adj - new list int>[V]; 3 -Graph() { delete 11 ad); void addEdge (int v, int w) { adj (v).push_back(w): adj [w] .push_back (v): bool sameComponent(int v, int w) return id[v] -- id[w] ; 1 > A. Perform the following modifications so that function sameComponent (v, w) correctly returns true if vertices v and w are in the same component: a. Modify the constructor so that the array id is allocated with size V. Initialize the array such that each vertex in the graph is in a separate component b. Modify function addEdge so that the components are updated (if necessary) each time a new edge is added. Example 0 0 1 0 0 1 0 2 2 3 3 4 3 1 0 2 3 3 3 2 3 3 3 0 3 0 0 3 B. What is the order of growth of the running time of creating a complete graph using the addEdge function you have created? (25 points) Connected Components (2) Consider the following modified version of class Graph for representing undirected graphs: class Graph int V; // No. o vertices list int>"ady: // Each vertex belongs to a connected component in the graph. //id101 stores the id of the connected component vertex O belongs to, // id[1] stores the component id for vertex 1, ete. // Idul - d[v] 1f and only if u and v belong to the same component int* id; public: Graph (int V) this->V - Vi adj - new list[V]; -Graph () delete t] adj;) void addEdge (int v, int w) adlv.push_back (w): adj (w).push_back(v): bool connectedComponents() (-) > Implement function connectedComponents() such that it finds all the connected components in the graph, fills the array id and returns it. Note the following: You are not allowed to modify the constructor or function addEdge. All the computation must be done inside function connectedComponents or inside other functions called from within connectedcomponents. You are allowed to add private data members and private or public member functions as needed. Function connectedcomponents must run in time that is at most linear in the graph size (i.e. 0(E+V)

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

Recommended Textbook for

Principles Of Multimedia Database Systems

Authors: V.S. Subrahmanian

1st Edition

1558604669, 978-1558604667

More Books

Students also viewed these Databases questions

Question

What does stickiest refer to in regard to social media

Answered: 1 week ago

Question

=+associated with political parties and if so, which ones? Are

Answered: 1 week ago