Connected Components Algorithm

The Connected Components Algorithm is a powerful graph traversal technique used to identify and group the connected components in an undirected graph. Connected components are the subgraphs in which every pair of nodes is connected by a path, and they are not connected to any additional nodes in the subgraph. The algorithm works by performing a depth-first search (DFS) or breadth-first search (BFS) on each node in the graph, marking nodes as visited when traversed, and assigning all the visited nodes to a connected component group. By the time the algorithm completes, all the nodes in the graph have been visited and assigned to a connected component group, allowing us to understand the structure and connectivity of the graph. In the Connected Components Algorithm, we start by selecting an arbitrary node from the graph and perform a DFS or BFS traversal from that node, marking all the visited nodes during the traversal. Once the traversal is complete, we pick another unvisited node from the graph and initiate another DFS or BFS traversal from that node, marking all the visited nodes in this new traversal. This process is repeated until all the nodes in the graph have been visited and marked. As a result, we obtain a collection of connected component groups, where each group consists of nodes that belong to the same connected component in the graph. This information can be utilized for various applications, such as analyzing social networks, transportation networks, or electrical grids, to understand the connectivity and structure of the underlying system.
/*
 Petar 'PetarV' Velickovic
 Algorithm: Connected Components
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#define MAX_N 5001
using namespace std;
typedef long long lld;

int n;

struct Node
{
    vector<int> adj;
};
Node graf[MAX_N];
bool mark[MAX_N];

//Algoritam za odredjivanje broja povezanih komponenti neusmerenog grafa, koristeci DFS
//Slozenost: O(V + E)

inline void DFS(int start)
{
    stack<int> dfs_stek;
    dfs_stek.push(start);
    while (!dfs_stek.empty())
    {
        int xt = dfs_stek.top();
        dfs_stek.pop();
        mark[xt] = true;
        for (int i=0;i<graf[xt].adj.size();i++)
        {
            if (!mark[graf[xt].adj[i]])
            {
                dfs_stek.push(graf[xt].adj[i]);
                mark[graf[xt].adj[i]] = true;
            }
        }
    }
}

inline int numComponents(int n)
{
    int ret = 0;
    for (int i=0;i<n;i++)
    {
        if (!mark[i])
        {
            DFS(i);
            ret++;
        }
    }
    return ret;
}

int main()
{
    n = 6;
    
    graf[0].adj.push_back(1);
    graf[1].adj.push_back(0);
    
    graf[0].adj.push_back(2);
    graf[2].adj.push_back(0);
    
    graf[4].adj.push_back(4);
    
    graf[3].adj.push_back(5);
    graf[5].adj.push_back(3);
    
    printf("%d\n",numComponents(n));
    
    return 0;
}

LANGUAGE:

DARK MODE: