Count of pair of nodes at even distance (Using BFS)


In this article, we will find the number of the pair of nodes, which are at an even distance of each other in a graph. We will be using the breadth first search (BFS) approach to find the total count.

In the approach discussed in this article, we will use a queue data structure which will contain pair of integers. The queue data structure will allow us to go through the graph using the breadth first search algorithm (BFS). We will pick a random node and apply the breadth first search from that node. We will use two variables to keep track of the number of nodes that appear on an even level from our given node, and the nodes that appear on the odd levels from our given node. All the nodes that are at an even distance from the source node will also be even distance away from each other, and all the nodes that are at an odd distance from the source node will also be even distance away from each other. So, using the two variables we can calculate the total count of nodes that are an even distance apart from each other.

Problem statement − Given a graph, which is connected and does not contain cycles, containing N nodes and N-1 edges, we need to find the total count of pair of nodes that are at an even distance from each other.

Note − The graph is given in the form of an adjacency list.

Sample Examples

Input

            0
           /
         1
       /
     2
   /
 3

Output

2

Explanation

The following are the only nodes which are at an even distance from each other {0,2}, {1,3}, So the output is 2

Input

    0
   /  \
  1    2
      / \
     3   4

Output

4

Explanation

The nodes that are at an even distance from each other are −

{0,3}, {0,4}, {1,2}, {3,4}

Hence, the output is 4.

Input

      0
     /  \
    1     2
   / \    / \
  3   4   5  6

Output

11

Explanation

The nodes which are at even distance from each other are −

{0,3}, {0,4}, {0,5}, {0,6}, {1,2}, {3,4}, {3,5}, {3,6}, {4,5}, {4,6}, {5,6}

So, the output is 11.

Approach

In this approach, we will use a queue data structure which will contain a pair of integers. We will select a source node at random and push it to the queue. We will then apply the breadth first search algorithm with the selected source node. We will maintain two variables, which will keep track of the number of nodes that are at an even distance from our source node and the number of nodes that are at an odd distance from our source node. The polarity of a certain node will be stored in the second integer in the pair. Using these two variables, we can calculate the total number of nodes that are at an even distance from each other.

Algorithm

  • Step 1 − Make a vector, vis, which will keep track of the nodes that we have visited.

  • Step 2 − Make a queue data structure.

  • Step 2.1 − The queue will contain a pair of integers, the first integer in the pair will be the node in the graph, and the second integer will represent the polarity, i.e., if the node is an even distance from our source node, or an odd distance.

  • Step 2.2 − Push the source node in the queue with polarity 0.

  • Step 2.3 − Make two variables which will store the number of nodes at odd and even levels.

  • Step 3 − Loop till the queue becomes empty:

  • Step 3.1 − In each iteration, take out the front element of the queue and mark it as visited.

  • Step 3.2 − Loop for each of the neighbors of the current node

  • Step 3.3 − If a neighbor is not visited, add it to the queue with polarity opposite to the polarity of the current node.

  • Step 3.4 − if the polarity of the current node is even, add 1 to the even variable else add 1 to the odd variable

  • Step 4 − After the queue becomes empty, the answer can be calculated as: answer = even*(even-1)/2 + odd*(odd-1)/2

  • Step 5 − Return the answer.

Example

Below C++ program is the implementation of the above approach −

#include <bits/stdc++.h>
using namespace std;
int Count_pair_nodes(vector<vector<int>> &gph){
   int N = gph.size();
   vector<int> vis(N,0);
   queue<pair<int,int>> que;
   que.push({0,0});
   long long even_pos = 0 , odd_pos = 0;
   while(!que.empty()){
      int vertex = que.front().first , odd_even = que.front().second;
      que.pop();
      vis[vertex] = 1;
      for(auto it:gph[vertex]){
         if(vis[it]==0)que.push({it,1-odd_even});
      }
      if(odd_even)even_pos++;
      else odd_pos++;
   }
   long long ans = even_pos*(even_pos-1)/2 + odd_pos*(odd_pos-1)/2;
   return ans;
}
int main(){
   vector<vector<int>> gph = {
      {1},
      {0,2},
      {1,3},
      {2}
   };
   cout<<Count_pair_nodes(gph)<<endl;
   return 0;
}

Output

2

Time complexity − O(N), where N is the number of nodes in the graph.

Space complexity − O(N)

Updated on: 01-Nov-2023

27 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements