Given a graph **G **with **V **nodes and **E** edges, the task is to colour no more than **floor(V/2)** nodes such that every node has *at least one coloured node* at a distance of atmost 1 unit. The distance between any two connected nodes of the graph is always exactly 1 unit. Print the nodes that need to be colored.

**Examples:**

**Approach:** This problem can be solved using **BFS** traversal. Follow the steps below to solve the problem:

- Initialize arrays
**odd[]**and**even[]**to store nodes which are at a distance of odd and even number of nodes respectively from the source. - Starting from the source node, perform BFS traversal with
**distance**initialized 0, which denotes the distance from source node. Store all the nodes at a particular level in odd[] or even[] based on the value of**distance**. - If distance is odd, that is
**(distance & 1)**is 1, insert that node into odd[]. Otherwise, insert into even[]. - Now print the nodes from the array with minimum elements.
- Since minimum among count of nodes at odd distance or even distance does not exceed
**floor(V/2)**, the answer holds correct as every node at odd distance is connected to nodes at even distance and vice-versa. - Hence, if number of nodes at even distance from source is less, print the nodes from even[]. Otherwise, print all nodes from odd[].

Illustration:

For the graph G given below,

Source Node S = 1

- even[] = {1, 3, 5}
- odd[] = {2, 6, 4}
- minimum(even.size(), odd.size()) = min(3, 3) = 3
- Hence, coloring either all nodes at odd distance from the source or even distance from the source is correct as both their count is same.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement the ` `// above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Stores the graph ` `map<` `int` `, vector<` `int` `> > graph; ` ` ` `// Stores the visited nodes ` `map<` `int` `, ` `int` `> vis; ` ` ` `// Stores the nodes ` `// at odd distance ` `vector<` `int` `> odd; ` ` ` `// Stores the nodes at ` `// even distance ` `vector<` `int` `> even; ` ` ` `// Function to seperate and ` `// store the odd and even ` `// distant nodes from source ` `void` `bfs() ` `{ ` ` ` `// Source node ` ` ` `int` `src = 1; ` ` ` ` ` `// Stores the nodes and their ` ` ` `// respective distances from ` ` ` `// the source ` ` ` `queue<pair<` `int` `, ` `int` `> > q; ` ` ` ` ` `// Insert the source ` ` ` `q.push({ src, 0 }); ` ` ` ` ` `// Mark the source visited ` ` ` `vis[src] = 1; ` ` ` ` ` `while` `(!q.empty()) { ` ` ` ` ` `// Extract a node from the ` ` ` `// front of the queue ` ` ` `int` `node = q.front().first; ` ` ` `int` `dist = q.front().second; ` ` ` `q.pop(); ` ` ` ` ` `// If distance from source ` ` ` `// is odd ` ` ` `if` `(dist & 1) { ` ` ` `odd.push_back(node); ` ` ` `} ` ` ` ` ` `// Otherwise ` ` ` `else` `{ ` ` ` `even.push_back(node); ` ` ` `} ` ` ` ` ` `// Traverse its neighbors ` ` ` `for` `(` `auto` `i : graph[node]) { ` ` ` ` ` `// Insert its unvisited ` ` ` `// neighbours into the queue ` ` ` `if` `(!vis.count(i)) { ` ` ` ` ` `q.push({ i, (dist + 1) }); ` ` ` `vis[i] = 1; ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `graph[1].push_back(2); ` ` ` `graph[2].push_back(1); ` ` ` `graph[2].push_back(3); ` ` ` `graph[3].push_back(2); ` ` ` `graph[3].push_back(4); ` ` ` `graph[4].push_back(3); ` ` ` `graph[4].push_back(5); ` ` ` `graph[5].push_back(4); ` ` ` `graph[5].push_back(6); ` ` ` `graph[6].push_back(5); ` ` ` `graph[6].push_back(1); ` ` ` `graph[1].push_back(6); ` ` ` ` ` `bfs(); ` ` ` ` ` `if` `(odd.size() < even.size()) { ` ` ` `for` `(` `int` `i : odd) { ` ` ` `cout << i << ` `" "` `; ` ` ` `} ` ` ` `} ` ` ` `else` `{ ` ` ` `for` `(` `int` `i : even) { ` ` ` `cout << i << ` `" "` `; ` ` ` `} ` ` ` `} ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 3 5

**Time Complexity:** O(V + E)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of subtrees from an N-ary tree consisting of single colored nodes
- Find the minimum spanning tree with alternating colored edges
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Maximum number of nodes which can be reached from each node in a graph.
- Print all neighbour nodes within distance K
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Minimum value of distance of farthest node in a Graph
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Detect cycle in the graph using degrees of nodes of graph
- Minimum cost path from source node to destination node via an intermediate node
- Sum of degrees of all nodes of a undirected graph
- Number of sink nodes in a graph
- Nodes with prime degree in an undirected Graph
- Maximize number of nodes which are not part of any edge in a Graph
- Difference Between sum of degrees of odd and even degree nodes in an Undirected Graph
- Maximum sum of values of nodes among all connected components of an undirected graph

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.