如何找到图的最大连通分量
How I can find max connected component of graph?
我需要找出图的最大连通分量,我必须写这个
示例# 1:
4 4 2
1 2
2 3
3 4
4 2
结果# 1:
3
2 3 4
例2:
3 2 2
1 2
2 3
结果# 2:
"NO"
下面是我的代码:
#include <iostream>
#include <vector>
using namespace std;
const int MX = 100000;
bool kosz[100000];
vector<int> graph[MX];
vector<int> graphQ;
int n, m , d;
int deq[MX];
int main() {
cin >> n >> m >> d;
for (int i = 1; i <= m; i++){
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for(int i = 0; i < n; i++){
if(graph[i].size() < d){
graphQ.push_back(i);
kosz[i]=1;
}
}
for(int i = 0; i < graphQ.size(); i++){
int x = graphQ[i];
for(int j = 0; j < graph[x].size(); j++){
if(deq[graph[x][j]] < d && kosz[graph[x][j]] == 0){
graphQ.push_back(graph[x][j]);
kosz[graph[x][j]]=1;
}
}
}
for(int i = 0; i < n; i++){
if(kosz[i])graph[i].clear();
for(int j = 0; j <graph[i].size(); j++)
if(kosz[graph[i][j]]){
swap(graph[i][j],graph[i].back());
graph[i].pop_back();
}
}
return 0;
}
编辑:
如何确定最大连接组件?
(en.wikipedia.org/wiki/Connected_component_%28graph_theory%29)这是一个求无向图的连通分量的程序。这可以使用深度优先搜索来完成,并将其作为成功开发和完成您的游戏的基准。
// Implementation of Kosaraju's algorithm to print all SCCs
#include <iostream>
#include <list>
#include <stack>
using namespace std;
class Graph{
int V; // No. of vertices
list<int> *adj; // An array of adjacency lists
// Fills Stack with vertices (in increasing order of finishing times)
// The top element of stack has the maximum finishing time
void fillOrder(int v, bool visited[], stack<int> &Stack);
// A recursive function to print DFS starting from v
void DFSUtil(int v, bool visited[]);
public:
Graph(int V);
void addEdge(int v, int w);
// The main function that finds and prints strongly connected components
int printSCCs();
// Function that returns reverse (or transpose) of this graph
Graph getTranspose();
};
Graph::Graph(int V){
this->V = V;
adj = new list<int> [V];
}
// A recursive function to print DFS starting from v
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::getTranspose(){
Graph g(V);
for (int v = 0; v < V; v++){
// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i){
g.adj[*i].push_back(v);
}
}
return g;
}
void Graph::addEdge(int v, int w){
adj[v].push_back(w); // Add w to v’s list.
}
void Graph::fillOrder(int v, bool visited[], stack<int> &Stack){
// Mark the current node as visited and print it
visited[v] = true;
// 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])
fillOrder(*i, visited, Stack);
// All vertices reachable from v are processed by now, push v to Stack
Stack.push(v);
}
// The main function that finds and prints all strongly connected components
int Graph::printSCCs(){
stack<int> Stack;
// Mark all the vertices as not visited (For first DFS)
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// Fill vertices in stack according to their finishing times
for (int i = 0; i < V; i++)
if (visited[i] == false)
fillOrder(i, visited, Stack);
// Create a reversed graph
Graph gr = getTranspose();
// Mark all the vertices as not visited (For second DFS)
for (int i = 0; i < V; i++)
visited[i] = false;
int count = 0;
// Now process all vertices in order defined by Stack
while (Stack.empty() == false){
// Pop a vertex from stack
int v = Stack.top();
Stack.pop();
// Print Strongly connected component of the popped vertex
if (visited[v] == false){
gr.DFSUtil(v, visited);
cout << endl;
}
count++;
}
return count;
}
//-----------------------------------------------------------------------------------------------
// test above functions
int main(){
// Create a graph given in the above diagram
Graph g(5);
g.addEdge(1, 0);
g.addEdge(0, 2);
g.addEdge(2, 1);
g.addEdge(0, 3);
g.addEdge(3, 4);
cout << "Following are strongly connected components in given graph n";
if (g.printSCCs() > 1){
cout << "Graph is weakly connected.";
}else{
cout << "Graph is strongly connected.";
}
return 0;
}
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 如何找出GDB的SIGTRAP核心转储的根本原因
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 找不到QtResource文件中的文件
- Python中的for循环与C++有何不同
- VC++本机单元测试,找不到调试符号
- RegGetValue在当前用户下找不到名称
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- c++找不到具有相同哈希的无序集合元素
- 找不到以下加速库:boost_fiber
- 找不到 FLTK(缺少:FLTK_INCLUDE_DIR)
- 设置 Visual Studio for MPI: 找不到标识符错误
- "assimp/config.h"找不到,但它在文件夹中
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- C++系统找不到指定的文件错误
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",