Find Count of Pair of Nodes at Even Distance

To discover the tally of sets of hubs at an indeed remove in a chart, we will utilise the chart traversal calculation. Beginning from each hub, we perform a traversal, such as a breadth−first look (BFS) or a depth−first look (DFS), and keep track of the separations of all hubs from the beginning hub. While navigating, we tally the number of hubs at indeed separations experienced. By repeating this handle for all hubs, we get the entire check of sets of hubs at separations within the chart. This approach permits us to productively decide the number of sets of hubs that fulfil the given condition in different chart structures.

Methods Used

  • Graph Traversal approach

  • DFS

Graph Traversal Approach

The graph traversal approach to finding the tally of sets of hubs at an indeed remove includes investigating the chart employing a traversal calculation like depth−first look (DFS) or breadth−first look (BFS). Starting from each hub within the chart, we navigate its neighbouring hubs and keep track of the separations between hubs. By tallying the number of nodes at indeed separations experienced amid the traversal, we will determine the number of sets of nodes at an indeed separation. This approach proficiently analyses the chart structure to recognise and tally the required sets of hubs, giving a dependable strategy for fathoming such issues.


  • Make a contiguousness list or contiguousness lattice representation of the graph.

  • Initialise a variable "tally" to keep track of the check of sets of hubs at indeed distance.

  • Iterate over each hub within the graph.

  • For each hub, initialise a line and a separate counter.

  • Enqueue the current hub into the line and stamp it as visited.

  • While the line isn't purge, perform the following steps:

  • Dequeue a hub from the queue.

    Iterate over its neighbouring nodes.

    If a neighbouring hub isn't visited, queue it into the line and stamp it as visited.

    Increment the separate counter for the neighbouring node.

    After navigating all the hubs reachable from the current hub, check on the off chance that the separate counter is even.

  • If it is indeed, increase the check variable by the number of hubs gone by from the current node.

    If it is odd, overlook the hubs visited from the current node.

    Repeat steps 4–7 for all hubs within the graph.

  • Return the value of the count variable, which speaks to the number of sets of hubs at any given location within the chart.


#include <iostream>
#include <vector>
#include <queue>

int countNodesAtEvenDistance(std::vector<std::vector<int>>& graph) {
    int count = 0;

    std::vector<bool> visited(graph.size(), false);

    for (int node = 0; node < graph.size(); ++node) {
        std::queue<int> q;
        int distance = 0;

        visited[node] = true;

        while (!q.empty()) {
            int current = q.front();

            for (int neighbor : graph[current]) {
                if (!visited[neighbor]) {
                    visited[neighbor] = true;


                    if (distance % 2 == 0) {
                        count += q.size();

    return count;

int main() {
    std::vector<std::vector<int>> graph = {
        {1, 2},
        {0, 2, 3},
        {0, 1, 3},
        {1, 2, 4},

    int pairCount = countNodesAtEvenDistance(graph);
    std::cout << "Count of pairs of nodes at even distance: " << pairCount << std::endl;

    return 0;


Count of pairs of nodes at even distance: 3


The count of sets of hubs at an indeed separate location can be found utilising the DFS (Depth−First Look) approach. Beginning from a hub within the chart, we perform a depth−first traversal, going to all its adjoining hubs. Amid the traversal, we keep up a separate counter that keeps track of the separate from the beginning hub. When we reach a hub at an indeed separate distance, we increase the tally of sets by the number of hubs already experienced at an odd distance. This is often the case since for each hub at an indeed separate, there's a potential combination with every hub at an odd separate. By investigating the whole chart utilising DFS, we are able to decide the number of sets of hubs that can indeed be removed effectively.


  • Initialise the counter variable pairCount to 0.

  • Perform a DFS traversal beginning at each hub within the graph:

    • Stamp the current hub as visited.

    • Initialise a separate counter separate from 0.

    • Call an aide work dfs with parameters (currentNode, distance).

  • In the dfs function:

    • Increase the separation by 1.

    • Check in case the separate is indeed In the event that it is, increase pairCount by the number of hubs experienced at odd distances.

    • Recursively call the dfs work for each unvisited adjoining hub, passing the adjoining hub and the upgraded remove as parameters.

  • Repeat steps 2 and 3 for all unvisited nodes within the graph.

  • Return the ultimate value of pairCount, which speaks to the tally of sets of hubs that are indeed separate.


#include <iostream>
#include <vector>

void dfs(const std::vector<std::vector<int>>& graph, int node, std::vector<int>& dist, std::vector<bool>& vis, int c) {
    if (vis[node]) {
    vis[node] = true;
    dist[node] = c;

    for (int i = 0; i < graph[node].size(); i++) {
        if (!vis[graph[node][i]]) {
            dfs(graph, graph[node][i], dist, vis, c + 1);

int countOfNodes(const std::vector<std::vector<int>>& graph, int n) {
    std::vector<bool> vis(n + 1, false);
    std::vector<int> dist(n + 1, 0);

    dfs(graph, 1, dist, vis, 0);

    int even = 0, odd = 0;
    for (int i = 1; i <= n; i++) {
        if (dist[i] % 2 == 0) {
        } else {

    int ans = (even * (even - 1) + odd * (odd - 1)) / 2;
    return ans;

int main() {
    int n = 5;
    std::vector<std::vector<int>> graph = {
        {1, 3},

    int ans = countOfNodes(graph, n);
    std::cout << ans << std::endl;

    return 0;




This article clarifies how to discover the check of sets of hubs at an exact distance in a chart. It talks about two approaches: the Chart Traversal approach and the DFS approach. The Chart Traversal approach includes navigating the chart using procedures like breadth−first look (BFS) or depth−first look (DFS) and checking the number of hubs at the separations experienced amid the traversal. The DFS approach particularly utilises a depth−first look to investigate the chart and keep up a remove counter. It incrementally tallies the number of hubs at the separations experienced and returns the ultimate check. The article also gives algorithmic clarifications and code illustrations in C for both approaches.

Updated on: 14-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started