Count pairs of vertices in Tree such that distance between them is even

[ad_1]

Given a tree of N vertices, the task is to find the number of pairs of vertices such that the distance between them is even but cannot be 0

Examples:

Input: N = 5, Edges = [ [1, 0], [2, 1], [3, 1], [4, 3] ]

                        0
                     /          
                  1           
               /    \ 
            2        3
                        \
                         4 

Output: 4
Explanation: There are four pairs of vertices such that the distance between them is even.
They are [ 0, 2 ], [0, 3], [3, 2] and [1, 4].

Input: N = 6, Edges: [[1, 0], [2, 1], [3, 1], [4, 2], [5, 3]]

                           0
                        /         
                     1            
               /       \
           2            3
        /             /
     4              5
Output: 6
Explanation: There are 6 pairs of vertices such that the distance between them is even. They are [0, 2], [4, 1], [3, 0], [4, 5], [1, 5] and [2, 3].

 

Naive Approach: The naive approach is to try all possible pairs of vertices, find the distance between them and check if the distance is even. Follow the steps mentioned below to solve the problem:

  • Iterate over all the vertices for i = 0 to N-1:
    • Iterate from j = i+1 to N-1:
      • Find the distance from i to j using DFS.
      • If the distance is even then increment the count of pairs.
  • Return the count.

Below is the implementation of the above approach.

C++

  

#include <bits/stdc++.h>

using namespace std;

  

void dfs(int i, int par,

         vector<vector<int> >& adj,

         vector<int>& dis)

{

    

    for (int j : adj[i]) {

  

        

        if (j != par) {

  

            

            

            dis[j] = dis[i] + 1;

  

            

            dfs(j, i, adj, dis);

        }

    }

}

  

int countPairs(int n,

               vector<vector<int> >& edges)

{

    

    int ans = 0;

  

    

    vector<vector<int> > adj(n);

  

    for (int i = 0; i < n - 1; ++i) {

  

        

        adj[edges[i][0]].push_back(edges[i][1]);

        adj[edges[i][1]].push_back(edges[i][0]);

    }

  

    

    vector<int> dis(n);

  

    

    

    for (int i = 0; i < n; ++i) {

  

        

        

        fill(dis.begin(), dis.end(), 0);

  

        

        

        dfs(i, -1, adj, dis);

  

        

        

        for (int j = i + 1; j < n; ++j) {

  

            

            if (dis[j] % 2 == 0) {

  

                

                ans++;

            }

        }

    }

  

    

    return ans;

}

  

int main()

{

    int N = 5;

    vector<vector<int> > edges

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

  

    

    cout << countPairs(N, edges);

    return 0;

}

Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: The efficient approach to solve the problem is based on the concept of bipartite graph as shown below.

Every tree is a bipartite graph. So all the vertices are part of one of the two bipartite sets (say L and R). 
Any pair having both the values from different sets have an odd distance between them and pairs with vertices from the same set have even distance between them.

Based on the above observation it is clear that the total number of pairs is the possible pairs formed using vertices from the same set i.e., (xC2) + (yC2), where [ nC2 = n * (n – 1)/2, x is the size of set L and y is the size of set R ]. Follow the steps mentioned below to solve the problem.

  • Declare and initialize two variables x and y to 0 to store the size of the bipartite sets.
  • Make root a part of one of the bipartite set (say L).
  • Initialize an array (say dis[]) to store the distances from 0.
  • Start a DFS or BFS from the vertex 0:
    • At each instant, iterate through all the children, and if we have not visited this child yet (let’s say the child is j), then:
      • Increment its distance as dis[current node] = distance[parent] + 1.
      • If it is even, increment x and make it part of set L. Otherwise, increment y and make it part of set R.
    • Recursively do the same for its children.
  • Finally, return the value of xC2 + yC2.

Below is the implementation of the above approach.

C++

  

#include <bits/stdc++.h>

using namespace std;

  

void dfs(int i, int par,

         vector<vector<int> >& adj,

         vector<int>& dis)

{

    

    for (int j : adj[i]) {

  

        

        if (j != par) {

  

            

            

            dis[j] = dis[i] + 1;

  

            

            dfs(j, i, adj, dis);

        }

    }

}

  

int countPairs(int n,

               vector<vector<int> >& edges)

{

    

    vector<vector<int> > adj(n);

    for (int i = 0; i < n - 1; ++i) {

  

        

        adj[edges[i][0]].push_back(edges[i][1]);

        adj[edges[i][1]].push_back(edges[i][0]);

    }

  

    

    vector<int> dis(n);

  

    

    dfs(0, -1, adj, dis);

  

    

    

    int x = 0, y = 0;

  

    

    

    for (int i = 0; i < n; ++i) {

  

        

        if (dis[i] % 2 == 0) {

  

            

            x++;

        }

        else {

  

            

            y++;

        }

    }

  

    

    return x * (x - 1) / 2 + y * (y - 1) / 2;

}

  

int main()

{

    int N = 5;

    vector<vector<int> > edges

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

  

    

    cout << countPairs(N, edges);

    return 0;

}

Time Complexity: O(N)
Auxiliary Space: O(N)

[ad_2]

Leave a Reply