Given integers ‘N’ and ‘K’ where, N is the number of vertices of an undirected graph and ‘K’ denotes the number of edges in the same graph (each edge is denoted by a pair of integers where i, j means that the vertex ‘i’ is directly connected to the vertex ‘j’ in the graph).

The task is to find the maximum number of edges among all the connected components in the given graph.

**Examples:**

Input:N = 6, K = 4,

Edges = {{1, 2}, {2, 3}, {3, 1}, {4, 5}}

Output:3

Here, graph has 3 components

1st component 1-2-3-1 : 3 edges

2nd component 4-5 : 1 edges

3rd component 6 : 0 edges

max(3, 1, 0) = 3 edges

Input:N = 3, K = 2,

Edges = {{1, 2}, {2, 3}}

Output:2

**Approach:**

- Using Depth First Search, find the sum of the degrees of each of the edges in all the connected components separately.
- Now, according to Handshaking Lemma, the total number of edges in a connected component of an undirected graph is equal to half of the total sum of the degrees of all of its vertices.
- Print the maximum number of edges among all the connected components.

Below is the implementation of the above approach:

`// C++ program to find the connected component ` `// with maximum number of edges ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// DFS function ` `int` `dfs(` `int` `s, vector<` `int` `> adj[], vector<` `bool` `> visited, ` ` ` `int` `nodes) ` `{ ` ` ` `// Adding all the edges connected to the vertex ` ` ` `int` `adjListSize = adj[s].size(); ` ` ` `visited[s] = ` `true` `; ` ` ` `for` `(` `long` `int` `i = 0; i < adj[s].size(); i++) { ` ` ` `if` `(visited[adj[s][i]] == ` `false` `) { ` ` ` `adjListSize += dfs(adj[s][i], adj, visited, nodes); ` ` ` `} ` ` ` `} ` ` ` `return` `adjListSize; ` `} ` ` ` `int` `maxEdges(vector<` `int` `> adj[], ` `int` `nodes) ` `{ ` ` ` `int` `res = INT_MIN; ` ` ` `vector<` `bool` `> visited(nodes, ` `false` `); ` ` ` `for` `(` `long` `int` `i = 1; i <= nodes; i++) { ` ` ` `if` `(visited[i] == ` `false` `) { ` ` ` `int` `adjListSize = dfs(i, adj, visited, nodes); ` ` ` `res = max(res, adjListSize/2); ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `nodes = 3; ` ` ` `vector<` `int` `> adj[nodes+1]; ` ` ` ` ` `// Edge from vertex 1 to vertex 2 ` ` ` `adj[1].push_back(2); ` ` ` `adj[2].push_back(1); ` ` ` ` ` `// Edge from vertex 2 to vertex 3 ` ` ` `adj[2].push_back(3); ` ` ` `adj[3].push_back(2); ` ` ` ` ` `cout << maxEdges(adj, nodes); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

Time Complexity : O(nodes + edges) (Same as DFS)

Note : We can also use BFS to solve this problem. We simply need to traverse connected components in an undirected graph.

## Recommended Posts:

- Connected Components in an undirected graph
- Sum of the minimum elements in all connected components of an undirected graph
- Number of single cycle components in an undirected graph
- Count number of edges in an undirected graph
- All vertex pairs connected with exactly k edges in a graph
- Cycles of length n in an undirected and connected graph
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Strongly Connected Components
- Number of Triangles in an Undirected Graph
- Minimum number of edges between two vertices of a Graph
- Tarjan's Algorithm to find Strongly Connected Components
- Undirected graph splitting and its application for number pairs
- Program to find total number of edges in a Complete Graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.