Question: Use c++ to write code cor the functions. size_t CountGroups () const: Return the number of connected components in the graph (i.e. the number of
Use c++ to write code cor the functions.
size_t CountGroups () const: Return the number of connected components in the graph (i.e. the number of groups who are connected by ANY activity).
size_t CountGroups(const std::string& relationship) const: Return the number of connected components in the graph after edges in the graph are filtered: only consider edges
size_t CountGroups(const std::vector& relationships) const: Return the number of connected components in the graph after edges in the graph are filtered: only consider edges in
Exist code:
relationship.relationships.class Graph {
public:
Constractor(const std::string &people_fpath, const std::string &relations_fpath);
size_t CountGroups () const;
size_t CountGroups(const std::string& relationship) const;
size_t CountGroups(const std::vector& relationships) const
void AddVertex(const std::string& vertex);
void AddEdge(const std::string& v1, const std::string& v2);
private:
bool VertexInGraph(const std::string vertex);
std::vector relation;
std::map> graph_;
};
bool Graph::VertexInGraph(const std::string vertex) {
return static_cast(graph_.count(vertex));
}
void Graph::AddEdge(const std::string& v1, const std::string& v2) {
if (!VertexInGraph(v1) || !VertexInGraph(v2)) {
throw std::runtime_error("invalid argument");
}
graph_.at(v1).push_back(v2);
graph_.at(v2).push_back(v1);
}
void Graph::AddVertex(const std::string& vertex) {
if (VertexInGraph(vertex))
throw std::runtime_error("vertex already in graph.");
graph_.insert({vertex, std::list()});
}
Graph::Constractor(const std::string &people_fpath, const std::string &relations_fpath) {
std::ifstream ifs{people_fpath};
for (std::string line; std::getline(ifs, line); line = "") {
AddVertex(line);
}
std::ifstream ifp{relations_fpath};
for (std::string line; std::getline(ifp, line); line = "") {
std::vector one;
one = utilities::Split(line, ',');
AddEdge(one[0], one[1]);
relation.push_back(one[2]);
}
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
