[英]connected components in an undirected graph in c++
我正在搜索c ++代码以在无向图中找到连接的组件。 我有一个大矩阵,称为“定向”。 其元素为零或一。 directed(i,j)= 1表示i顶点具有指向j顶点的定向链接。 我想找到通过直接或间接连接顶点和组大小创建的不同组的数量。 下面的代码获取相连的顶点,并在不同的行中打印不同组的元素。 知道了定向矩阵,谁能帮助我找到每个组的数量和大小? 这是代码:
// C++ program to print connected components in
// an undirected graph
#include<iostream>
#include <list>
using namespace std;
// Graph class represents a undirected graph
// using adjacency list representation
int input(istream& in=cin)
{
int x;
in >> x;
return x;
}
class Graph
{
int V; // No. of vertices
// Pointer to an array containing adjacency lists
list<int> *adj;
// A function used by DFS
void DFSUtil(int v, bool visited[]);
public:
Graph(int V); // Constructor
void addEdge(int v, int w);
void connectedComponents();
};
// Method to print connected components in an
// undirected graph
void Graph::connectedComponents()
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int v = 0; v < V; v++)
visited[v] = false;
for (int v=0; v<V; v++)
{
if (visited[v] == false)
{
// print all reachable vertices
// from v
DFSUtil(v, visited);
cout << "\n";
}
}
}
void Graph::DFSUtil(int v, bool visited[])
{
// Mark the current node as visited and print it
visited[v] = true;
cout << v << " ";
// Recur for all the vertices
// adjacent to this vertex
list<int>::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i)
if(!visited[*i])
DFSUtil(*i, visited);
}
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
// method to add an undirected edge
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
// Drive program to test above
int main()
{
int directed[2][2];
directed[0][0]=1;
directed[0][1]=0;
directed[0][2]=0;
directed[1][1]=1;
directed[1][0]=1;
directed[1][2]=1;
directed[2][1]=0;
directed[2][2]=0;
directed[2][0]=1;
return 0;
}
修改DFSUtil
方法以返回一个int
,它指定访问的节点数。
int Graph::DFSUtil(int v, bool visited[])
{
// Mark the current node as visited and print it
visited[v] = true;
int count = 0;
cout << v << " ";
// Recur for all the vertices
// adjacent to this vertex
list<int>::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i)
if(!visited[*i])
count += DFSUtil(*i, visited, count + 1);
else
return 1;
return count;
}
通过保留一些预订,我们将了解到许多团体以及每个团体的人数
// Method to print connected components in an
// undirected graph
void Graph::connectedComponents()
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int v = 0; v < V; v++)
visited[v] = false;
vector<pair<int,int>> groups;
for (int v=0; v<V; v++)
{
if (visited[v] == false)
{
// print all reachable vertices
// from v
int groupSize = DFSUtil(v, visited);
groups.push_back(make_pair(v, groupSize))
cout << "\n";
}
}
}
您能否提供一个可回答的答案(x4)
当然:
// C++ program to print connected components in
// an undirected graph
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
// Graph class represents a undirected graph
// using adjacency list representation
class Graph {
using G = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;
using Component = int;
using Mapping = std::map<G::vertex_descriptor, Component>;
G g;
public:
Graph(int V) : g(V) {}
void addEdge(int v, int w);
void connectedComponents();
};
// Method to print connected components in an
// undirected graph
void Graph::connectedComponents() {
Mapping mappings;
int n = boost::connected_components(g, boost::make_assoc_property_map(mappings));
for (Component c = 0; c<n; ++c) {
std::cout << "component " << c << ":";
for (auto& mapping : mappings)
if (mapping.second == c) std::cout << " " << mapping.first;
std::cout << "\n";
}
}
void Graph::addEdge(int v, int w) {
boost::add_edge(v, w, g);
}
int main() {
// Create a graph given in the above diagram
Graph g(5); // 5 vertices numbered from 0 to 4
g.addEdge(1, 0);
g.addEdge(2, 3);
g.addEdge(3, 4);
std::cout << "Following are connected components \n";
g.connectedComponents();
}
打印
Following are connected components
component 0: 0 1
component 1: 2 3 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.