Discover rely of pair of nodes at even distance | Set 2 (Utilizing BFS)

[ad_1]

Given a related acyclic graph with N nodes numbered from 1 to N and N-1 edges, discover out the pair of nodes which are at even distance from one another.

Observe: The graph is represented within the type of an adjacency record.

Examples:

Enter: N = 3, graph = {{}, {2}, {1, 3}, {2}}
Output:1
Clarification: Right here there are three pairs {1, 2}, {1, 3}
and {2, 3} and solely {1, 3} has even distance between them.
i.e.,         1
            /
         2
      /
   3

Enter: N = 5, graph = {{}, {2, 4}, {1, 3}, {2}, {1, 5}, {4}}
Output: 4
Clarification: There are 4 pairs {1, 3}, {1, 5}, {2, 4}
and {3, 5} which has even distance.

 

Strategy: The DFS method of this text is already mentioned in Set-1 of this text. Right here we will probably be utilizing the BFS Traversal Of Graph to unravel the issue primarily based on the next concept:

Begin traversing from any node (say 1) as the basis of the graph and retailer the variety of nodes in odd and even numbered degree. The nodes in even numbered degree are distance away from one another. The identical is true for nodes in odd numbered ranges.

Comply with the steps talked about under to implement the concept:

  • Create an array to maintain monitor of all of the visited nodes.
  • Utilizing queue do BFS traversal of the graph.
  • Initially push the primary node within the queue after which traverse and push its neighbours which aren’t but visited into the queue and proceed the BFS on this means.
  • Rely the variety of nodes in even numbered ranges (say X) and odd numbered ranges (say Y).
  • The reply would be the sum of the variety of methods to decide on any two parts from X (i.e (X * (X – 1)) / 2)and any two parts from Y (i.e. (Y * (Y – 1)) / 2 ).

Beneath is the implementation of the above method:

C++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int countOfNodes(vector<int> graph[], int n)

{

    

    

    vector<int> vis(n + 1);

  

    

    queue<pair<int, int> > q;

    lengthy lengthy _0 = 0, _1 = 0;

  

    

    

    q.push({ 1, 0 });

  

    

    whereas (!q.empty()) {

        vis[q.front().first] = 1;

  

        

        for (auto youngster : graph[q.front().first]) {

            

            

            if (!vis[child])

                q.push({ youngster, 1 - q.entrance().second });

        }

        if (q.entrance().second)

            _1++;

        else

            _0++;

        q.pop();

    }

  

    

    

    

    int ans

        = (_0 * (_0 - 1)) / 2

          + (_1 * (_1 - 1)) / 2;

    return ans;

}

  

int predominant()

{

    int N = 3;

  

    

    vector<int> graph[N + 1];

    graph[1].push_back(2);

    graph[2].push_back(1);

    graph[2].push_back(3);

    graph[3].push_back(2);

  

    

    cout << countOfNodes(graph, N);

    return 0;

}

Time Complexity: O(V+E) = O(N). As V = variety of nodes = N, E = variety of edges = N-1 as given.
Auxiliary Area: O(N)

[ad_2]

Leave a Reply