Minimum Nodes to be Colored in a Graph such that every Node has a Colored Neighbour


In this problem, we will color the minimum nodes of the graph such that each node of the graph has a colored node at the maximum distance 1.

The simple logic to minimize the count of the colored node is that either color the nodes which are at an odd distance or color the nodes at an even distance. So, we can color the alternative node, and for each node, we can have a colored node at most distance 1.

Problem statement − We have given a graph containing N nodes and E edges. It is given that we can color at most floor(N/2) nodes. We need to find the minimum number of nodes to be colored such that each node of the graph has a colored node at the distance of at most 1 unit.

Note − The distance between two nodes is 1 unit.

Sample examples

Input

1 -> 4, 1 -> 3, 4 -> 5, 4 -> 2

Output

4,3

Explanation − Here is the visualization of the graph.

              1
            /    \ 
           4      3 
          /  \ 
         2    5

So, If we color the 4 and 3 nodes, we can have colored nodes at most 1 distance for each node.

Input

1 -> 2, 1 -> 3, 1 -> 4, 1 -> 5

Output

1

Explanation − As all nodes of the graph are connected to 1. If we color 1, we can get the desired outcomes.

Approach

This approach will use the map data structure to store the visited nodes. Also, we will use the list to store the nodes at even and odd distances from the source node. At last, if we find fewer nodes at an even distance, we will print that node. Otherwise, we will print the nodes which are at odd distances.

Algorithm

Step 1− Define the ‘visited’ map global variable. Also, define the global ‘odd_list’ and ‘even_list’ lists to store the nodes at an even distance.

Step 2− In the performBFS() function, initialize the ‘head’ with 1, and define the queue to store the pair of distance and node.

Step 3 − Insert the head with 0 distance into the queue, and also update the visited[head] with 1.

Step 4 − Traverse the queue.

Step 4.1 − Take the first pair from the queue.

Step 4.2 − If the distance is odd, insert the node into the ‘odd_dist’ list.

Step 4.3 − Otherwise, insert the node into the ‘even_list’ list.

Step 4.4 − Traverse all neighbor nodes of the current node. If the current node is not visited, insert it into the queue by incrementing the distance by 1. Also, update the visited[p] with 1.

Step 4.5 − In the main() method, if the size of the even_list is smaller, print all elements of the even_list. Otherwise, print all elements of the odd_list.

Example

#include <bits/stdc++.h>
using namespace std;

// For storing the graph
map<int, vector<int>> grp;
// Stores the visited nodes
map<int, int> visited;
// To store nodes that are at odd and even distances from the root node
vector<int> odd_dist;
vector<int> even_dist;
void performBFS() {
    int head = 1;
    queue<pair<int, int>> que;
    // Add head node to queue
    que.push({head, 0});
    visited[head] = 1;
    while (!que.empty()) {
        // Get first noed
        int node = que.front().first;
        int dist = que.front().second;
        que.pop();
        // For odd distance
        if (dist & 1) {
            odd_dist.push_back(node);
        }
        // For even distance
        else {
            even_dist.push_back(node);
        }
        // Traverse neighbor nodes
        for (auto p : grp[node]) {
            // Get unvisited nodes
            if (!visited.count(p)) {
                que.push({p, (dist + 1)});
                visited[p] = 1;
            }
        }
    }
}
int main() {
    grp[1].push_back(4);
    grp[4].push_back(1);
    grp[2].push_back(4);
    grp[4].push_back(2);
    grp[3].push_back(1);
    grp[1].push_back(3);
    grp[4].push_back(5);
    grp[5].push_back(4);
    performBFS();
    cout << "The nodes we should color is ";
    // Print nodes at odd or even distances according to the total number of nodes
    if (odd_dist.size() < even_dist.size()) {
        for (int p : odd_dist) {
            cout << p << " ";
        }
    } else {
        for (int p : even_dist) {
            cout << p << " ";
        }
    }
    return 0;
}

Output

The nodes we should color is 4 3

Time complexity − O(N) for traversing all nodes.

Space complexity − O(N) to store nodes in the queue.

We used the BFS traversal to visit each node of the given graph. Also, we keep track of the distance of each node from the source node to filter the nodes at even distances and those at odd distances.

Updated on: 02-Aug-2023

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements