>x; 返回x; } 类图 { int V;//顶点数 //指向包含邻接列表的数组的指针 列表*adj; //DFS使用的函数 void DFSUtil(int v,bool已访问[]); 公众: 图(int V);//构造函数 无效补遗(整数v,整数w); void connectedComponents(); }; //方法以打印图形中连接的组件 //无向图 void图::connectedComponents() { //将所有顶点标记为未访问 bool*visted=新bool[V]; 对于(int v=0;v,c++,graph,C++,Graph" /> >x; 返回x; } 类图 { int V;//顶点数 //指向包含邻接列表的数组的指针 列表*adj; //DFS使用的函数 void DFSUtil(int v,bool已访问[]); 公众: 图(int V);//构造函数 无效补遗(整数v,整数w); void connectedComponents(); }; //方法以打印图形中连接的组件 //无向图 void图::connectedComponents() { //将所有顶点标记为未访问 bool*visted=新bool[V]; 对于(int v=0;v,c++,graph,C++,Graph" />

c++; 我正在寻找一个C++代码来寻找一个无向图中的连通分量。我有一个很大的矩阵,叫做“定向”。它的元素是零或一。定向(i,j)=1表示i vertice与j vertice有定向链接。我想找到由顶点直接或间接连接创建的不同组的数量和组的大小。下面的代码获取连接的顶点,并在不同的行中打印不同组的元素。 知道有向矩阵,有谁能帮我找到组的数量和每个组的大小吗?代码如下: // 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; } //C++程序打印连接的组件 //无向图 #包括 #包括 使用名称空间std; //Graph类表示无向图 //使用邻接列表表示法 int输入(istream&in=cin) { int x; 在>>x; 返回x; } 类图 { int V;//顶点数 //指向包含邻接列表的数组的指针 列表*adj; //DFS使用的函数 void DFSUtil(int v,bool已访问[]); 公众: 图(int V);//构造函数 无效补遗(整数v,整数w); void connectedComponents(); }; //方法以打印图形中连接的组件 //无向图 void图::connectedComponents() { //将所有顶点标记为未访问 bool*visted=新bool[V]; 对于(int v=0;v

c++; 我正在寻找一个C++代码来寻找一个无向图中的连通分量。我有一个很大的矩阵,叫做“定向”。它的元素是零或一。定向(i,j)=1表示i vertice与j vertice有定向链接。我想找到由顶点直接或间接连接创建的不同组的数量和组的大小。下面的代码获取连接的顶点,并在不同的行中打印不同组的元素。 知道有向矩阵,有谁能帮我找到组的数量和每个组的大小吗?代码如下: // 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; } //C++程序打印连接的组件 //无向图 #包括 #包括 使用名称空间std; //Graph类表示无向图 //使用邻接列表表示法 int输入(istream&in=cin) { int x; 在>>x; 返回x; } 类图 { int V;//顶点数 //指向包含邻接列表的数组的指针 列表*adj; //DFS使用的函数 void DFSUtil(int v,bool已访问[]); 公众: 图(int V);//构造函数 无效补遗(整数v,整数w); void connectedComponents(); }; //方法以打印图形中连接的组件 //无向图 void图::connectedComponents() { //将所有顶点标记为未访问 bool*visted=新bool[V]; 对于(int v=0;v,c++,graph,C++,Graph,修改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<

修改
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;
}
int图形::DFSUtil(int v,bool已访问[])
{
//将当前节点标记为已访问并打印
访问[v]=正确;
整数计数=0;
库特

请提供一个可运行的答案(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();
}

专业提示:
adj=新列表[V]
-这不是一个好主意…您应该将
adj
的类型更改为
std::vector
,并在成员初始值设定项列表中对其进行初始化。同样,
std::list
…如果您不需要
std::list
迭代器无效保证,请使用
std::vector
代替其他指针(例如,
connectedComponents
函数中的
visited
)。请改用。你们中的任何一位可以提供完整的答案吗?@SomeProgrammerDude您可以提供一个使用“定向”矩阵的可运行答案吗?-很抱歉,我可能没有完全解决您的问题:您可以添加一个可选的“定向”吗matrix让你的答案可以运行?这是一个错误boost/graph/adjacency_matrix.hpp:现在没有这样的文件或目录。它们正在运行。输出就在那里。如果你不想使用boost,只需在没有boost的情况下编写它:或者甚至与之间的区别是:哦,哈,你没有在e问题代码。如果我理解正确,现在您只需要从矩阵转换到邻接列表。好吧,这是我的2美分:。您可以自由地
convertGraph
从任何图形表示形式转换到另一个图形表示形式,并且
connectedComponents
可以在每个图形表示形式上工作(使用sinlge实现)演示程序甚至验证了矩阵表示法产生的连接组件是否与使用邻接列表表示法产生的连接组件相同。
// 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