Thursday, June 17, 2021

Published June 17, 2021 by Anonymous with 0 comment

Shortest path length between two given nodes such that adjacent nodes are at bit difference 2

Shortest path length between two given nodes such that adjacent nodes are at bit difference 2

Given an unweighted and undirected graph consisting of N nodes and two integers a and b. The edge between any two nodes exists only if the bit difference between them is 2, the task is to find the length of the shortest path between the nodes a and b. If a path does not exist between the nodes a and b, then print -1.

Examples:

Input: N = 15, a = 15, b = 3
Output: 1
Explanation: a = 15 = (1111)2 and b = 3 = (0011)2. The bit difference between 15 and 3 is 2. Therefore, there is a direct edge between 15 and 3. Hence, length of the shortest path is 1.

Input: N = 15, a = 15, b = 2
Output: -1
Explanation: a = 15 = (1111)2 and b= 2 = (0010)2. The bit difference between 15 and 2 is 3. As the bit difference can only be 2, it is impossible to reach 15
from 2.

Naive Approach: The simplest approach to solve this problem is to first construct the graph using the given conditions, then find the shortest path between the nodes using a and b using bfs by considering a as the source node of the graph.
Time Complexity: O(N2)
Auxiliary Space: O(N) 

Efficient Approach:The problem can be solved by observing that the sum of bit differences between any two nodes must be a factor 2 and their shortest distance must be half of that sum. Follow the steps given below to understand the approach:


  1. Count of set bits in Bitwise XOR of a and b gives the count of bit difference between the nodes a and b.
  2. If the count of set bits in Bitwise XOR of a and b is a multiple of 2, then a and b are connected.
  3. If the count of set bits is 2, that means they are 1 unit apart from each other.If the count of set bits in xor of a and b is 4 that means node a and b are 2 units apart. Therefore, if the bit difference is x then the shortest path would be x/2.
  4. If the bit difference is odd then they are not connected, therefore, print -1.

 Below is the implementation of the above approach:

C++

  

#include <bits/stdc++.h>

using namespace std;

  

int countbitdiff(int xo)

{

  

    

    

    int count = 0;

  

    

    

    while (xo) {

  

        

        

        if (xo % 2 == 1) {

  

            

            count++;

        }

  

        

        xo = xo / 2;

    }

    return count;

}

  

void shortestPath(int n, int a, int b)

{

  

    

    int xorVal = a ^ b;

  

    

    

    int cnt = countbitdiff(xorVal);

  

    

    if (cnt % 2 == 0)

        cout << cnt / 2 << endl;

    else

        cout << "-1" << endl;

}

  

int main()

{

    

    int n = 15;

  

    

    int a = 15, b = 3;

  

    

    shortestPath(n, a, b);

    return 0;

}

Output:
1

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 


Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment