Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Algorithms Design and Analysis - Fall 2020 Assignment #3 Elementary Graph Algorithms Due Friday, January 8th, 2021 All submissions must be done on e-learning only.

image text in transcribed
image text in transcribed
image text in transcribed
Algorithms Design and Analysis - Fall 2020 Assignment #3 Elementary Graph Algorithms Due Friday, January 8th, 2021 All submissions must be done on e-learning only. class Graph int V; 1 Noot list int> 'adj: public: Graph (int V) this-> = V; adj - new list int>[V); -Graph I delete ad void addEdge (int , int w adv).push_back(): 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 ivu) 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 vertica liat int> ady // Each vertex belongs to a connected component in the graph. // idroj stores the id of the connected component vertex o belongs to, 1/id111 stores the component id for vertex 1, etc. / idol - id[v] if and only if u and y belong to the same component int* id; publie: Graph (int V) this->V-V adj new listcint: 1 -Graph() delete adj:) void addEdge (int w, int w) ady (vlpush back (w): adj (w).push_back (v); 1 bool sameComponent(int , int w) { return id[v] - id[w]: } A. Perform the following modifications so that function sameComponent (v.) correctly returns true if vertices v and 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 2 0 0 1 0 2 2 3 3 4 3 0 0 1 0 34 3 3 3 0 0 1 0 2 3 4 3 3 3 1 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 Videos of acces Distanta adja IL Farch Vertex belong to connected components in the graphs la id el stories the id of the connected component vertek a talanta Paul Hal ids stores the component ad for vettek is sets. kalvo -- divi t and anty is and y Besona, to the fase waapanent inta id, publier Graph (int V) this->V-V: ady - new listint>V]: 1 Tazaphi (il delete [] adj; I voud addkaze (ant vi int w) adv).push back(); ads().push_back (v); 1 bool connectedComponent() {-} 11 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 contiected components or inside other functions called from within connected components. 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 (1.c. O[E+V)). Algorithms Design and Analysis - Fall 2020 Assignment #3 Elementary Graph Algorithms Due Friday, January 8th, 2021 All submissions must be done on e-learning only. class Graph int V; 1 Noot list int> 'adj: public: Graph (int V) this-> = V; adj - new list int>[V); -Graph I delete ad void addEdge (int , int w adv).push_back(): 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 ivu) 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 vertica liat int> ady // Each vertex belongs to a connected component in the graph. // idroj stores the id of the connected component vertex o belongs to, 1/id111 stores the component id for vertex 1, etc. / idol - id[v] if and only if u and y belong to the same component int* id; publie: Graph (int V) this->V-V adj new listcint: 1 -Graph() delete adj:) void addEdge (int w, int w) ady (vlpush back (w): adj (w).push_back (v); 1 bool sameComponent(int , int w) { return id[v] - id[w]: } A. Perform the following modifications so that function sameComponent (v.) correctly returns true if vertices v and 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 2 0 0 1 0 2 2 3 3 4 3 0 0 1 0 34 3 3 3 0 0 1 0 2 3 4 3 3 3 1 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 Videos of acces Distanta adja IL Farch Vertex belong to connected components in the graphs la id el stories the id of the connected component vertek a talanta Paul Hal ids stores the component ad for vettek is sets. kalvo -- divi t and anty is and y Besona, to the fase waapanent inta id, publier Graph (int V) this->V-V: ady - new listint>V]: 1 Tazaphi (il delete [] adj; I voud addkaze (ant vi int w) adv).push back(); ads().push_back (v); 1 bool connectedComponent() {-} 11 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 contiected components or inside other functions called from within connected components. 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 (1.c. O[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

Step: 3

blur-text-image

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