如何找到图的最大连通分量

How I can find max connected component of graph?

本文关键字:分量 何找      更新时间:2023-10-16

我需要找出图的最大连通分量,我必须写这个

示例# 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;
}