Maximize number of edges added to convert given Tree into a Bipartite Graph
Given a tree of N nodes, the task is to find the maximum number of edges that can be added to the tree such that it becomes a bipartite graph.
Note: Self loop or multiple edges are not allowed but cycles are permitted.
Examples:
Input: N = 4, Edges = {{1, 2}, {2, 3}, {1, 4}}
1
/ \
2 4
/
3
Output: 1
Explanation: An edge between nodes 3 and 4 can be added such that the graph still remains bipartite.
No more than 1 edge can be added such that the resultant graph is bipartite.Input: N = 5, Edges = {{1, 2}, {1, 3}, {2, 4}, {2, 5}}
1
/ \
2 3
/ \
4 5
Output: 2
Explanation: Two edges can be added, (3, 4) and (3, 5) and the graph still remains bipartite.
Naive Approach: The basic way to solve the problem is as follows:
Assign each node of the tree as black or white such that a black node is connected with a white node (There is always such a configuration because a tree is always bipartite).
Then for all possible pair of nodes check if an edge can be added between them.
Follow the steps mentioned below to implement the above idea:
- Traverse the tree initially and assign each node as black or white such that each edge connects a black and a white node. (Trees are always bipartite).
- Iterate over all pairs of nodes, and check if an edge can be added between them.
- If both the nodes are of different colors and there is no edge between them, an edge can be added. So increment the count.
- Otherwise, no edge can be added.
- The final value of count is the answer.
Below is the implementation of the above approach.
C++
// C++ code for the above approach: #include <bits/stdc++.h>using namespace std; // DFS to mark nodes as black or white.void dfs(int node, int par, bool isBlack, vector<vector<bool> >& adj, vector<bool>& color){ // Mark color as black or white. color[node] = isBlack; for (int i = 1; i < adj.size(); ++i) { // If there is no edge, // or 'i' is parent, continue. if (!adj[node][i] || i == par) continue; dfs(i, node, !isBlack, adj, color); }} // Function to calculate// maximum number of edges// that can be addedlong long maxEdges(int n, vector<pair<int, int> > edges){ // Build adjacency matrix. vector<vector<bool> > adj(n + 1, vector<bool>( n + 1, 0)); for (auto i : edges) { adj[i.first][i.second] = 1; adj[i.second][i.first] = 1; } // Call DFS to color nodes. vector<bool> color(n + 1); dfs(1, 0, 1, adj, color); long long ans = 0; // Iterate over all pairs of nodes. for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { // If the color is different // And there is no edge // Between them, increment answer. if (color[i] != color[j] && !adj[i][j]) ans++; } } // Return answer. return ans;} // Driver Codeint main(){ int N = 4; vector<pair<int, int> > edges = { { 1, 2 }, { 2, 3 }, { 1, 4 } }; cout << maxEdges(N, edges); return 0;} |
1
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Efficient Approach: The time taken in the above approach can be optimized by using the following observation:
Say, there were initially B black nodes and W white nodes in the tree. So a bipartite graph made from these nodes can have maximum B*W edges.
Therefore, the maximum number of edges that can be added to the tree with N nodes are B*W – (N-1) [as a tree with N node has N-1 edges]
Follow the steps mentioned below:
- Traverse the tree initially and assign each node as black or white such that each edge connects a black and a white node. (Trees are always bipartite).
- Count the number of black nodes and white nodes.
- Use the formula derived above from the observation and calculate the maximum number of edges that can be added.
Below is the implementation of the above approach.
C++
// C++ code for the above approach: #include <bits/stdc++.h>using namespace std; // DFS to count number of black nodes.int dfs(int node, int par, bool isBlack, vector<vector<int> >& adj){ int no_Of_Black = isBlack; for (int i : adj[node]) { if (i == par) continue; // Number of black nodes // in each subtree. no_Of_Black += dfs(i, node, !isBlack, adj); } return no_Of_Black;} // Function to find maximum edgeslong long maxEdges(int n, vector<pair<int, int> > edges){ // Build adjacency list. vector<vector<int> > adj(n + 1); for (auto i : edges) { adj[i.first].push_back(i.second); adj[i.second].push_back(i.first); } // Number of black nodes. int no_Of_Black = dfs(1, 0, 1, adj); // Number of white nodes. int no_Of_White = n - no_Of_Black; // Number of edges that can be added. return (1LL * (no_Of_Black) * (no_Of_White) - (n - 1));} // Driver codeint main(){ int N = 4; vector<pair<int, int> > edges = { { 1, 2 }, { 2, 3 }, { 1, 4 } }; cout << maxEdges(N, edges); return 0;} |
1
Time Complexity: O(N)
Auxiliary Space: O(N)

