Maximum Number of Edges to be Added to a Tree so that it stays a Bipartite GraphThe goal is to convert an N-node tree into a bipartite graph by adding as many edges as possible. Remember that self-loops and multiple edges are not permitted, although cycles are. Illustration: Explanation: An edge connecting nodes 3 and 4 can be added to keep the graph bipartite. There can only be one edge added to the graph to make it bipartite. Explanation: Even if two edges are added, (3, 4) and (3, 5), the graph remains bipartite. Simple Approach:The basic solution to the problem is as follows: Each node in the tree should be given a colour, resulting in a connection between a black and a white node (A tree is always bipartite, hence there is always such a configuration). Then, for each feasible pair of nodes, determine whether an edge may be added between them. To put the above idea into action, follow the procedures outlined below:
The aforementioned approach is implemented in the following manner. C++ Program: Output: 1
Efficient ApproachThe time required in the preceding approach can be reduced by making the following observation:
Follow the instructions below:
The aforementioned approach is implemented in the following manner. C++ Program: Output: 1
Next TopicModulus of two Float or Double Numbers
|