📌  相关文章
📜  两个给定节点之间的最短路径长度,使得相邻节点的位差为 2

📅  最后修改于: 2021-10-25 03:24:56             🧑  作者: Mango

给定一个由 N 个节点和两个整数ab组成的未加权和无向图。任意两个节点之间的边仅在它们之间的位差为2时才存在,任务是找到节点ab之间的最短路径的长度。如果节点ab之间不存在路径,则打印-1

例子:

朴素的方法:解决这个问题的最简单的方法是首先使用给定的条件构造图,然后通过将a作为图的源节点,使用 bfs 找到使用ab的节点之间的最短路径。

时间复杂度: O(N 2 )
辅助空间: O(N)

有效方法:可以通过观察任意两个节点之间的位差之和必须是因子2并且它们的最短距离必须是该和的一半来解决该问题。按照下面给出的步骤来理解该方法:

  1. ab 的按位异或中的设置位计数给出节点ab之间的位差计数。
  2. 如果ab 的按位异或中的设置位计数是2的倍数,则ab连接。
  3. 如果设置位的计数为2 ,则意味着它们彼此相距1 个单位。如果ab 的异或中的设置位计数为4 ,则意味着节点ab相距2 个单位。因此,如果位差为x则最短路径为x/2
  4. 如果位差是奇数,则它们没有连接,因此,打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count set bits
// in a number
int countbitdiff(int xo)
{
 
    // Stores count of
    // set bits in xo
    int count = 0;
 
    // Iterate over each
    // bits of xo
    while (xo) {
 
        // If current bit of xo
        // is 1
        if (xo % 2 == 1) {
 
            //  Update count
            count++;
        }
 
        // Update xo
        xo = xo / 2;
    }
    return count;
}
 
// Function to find length of shortest
// path between the nodes a and b
void shortestPath(int n, int a, int b)
{
 
    // Stores XOR of a and b
    int xorVal = a ^ b;
 
    // Stores the count of
    // set bits in xorVal
    int cnt = countbitdiff(xorVal);
 
    // If cnt is an even number
    if (cnt % 2 == 0)
        cout << cnt / 2 << endl;
    else
        cout << "-1" << endl;
}
 
// Driver Code
int main()
{
    // Given N
    int n = 15;
 
    // Given a and b
    int a = 15, b = 3;
 
    // Function call
    shortestPath(n, a, b);
    return 0;
}


Java
// Java program for tha above approach
import java.util.*;
 
class GFG{
     
// Function to count set bits
// in a number
static int countbitdiff(int xo)
{
     
    // Stores count of
    // set bits in xo
    int count = 0;
 
    // Iterate over each
    // bits of xo
    while (xo != 0)
    {
         
        // If current bit of xo
        // is 1
        if (xo % 2 == 1)
        {
             
            // Update count
            count++;
        }
         
        // Update xo
        xo = xo / 2;
    }
    return count;
}
 
// Function to find length of shortest
// path between the nodes a and b
static void shortestPath(int n, int a, int b)
{
     
    // Stores XOR of a and b
    int xorVal = a ^ b;
 
    // Stores the count of
    // set bits in xorVal
    int cnt = countbitdiff(xorVal);
 
    // If cnt is an even number
    if (cnt % 2 == 0)
        System.out.print(cnt / 2);
    else
        System.out.print("-1");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N
    int n = 15;
 
    // Given a and b
    int a = 15, b = 3;
 
    // Function call
    shortestPath(n, a, b);
}   
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
 
# Function to count set bits
# in a number
def countbitdiff(xo):
 
    # Stores count of
    # set bits in xo
    count = 0
 
    # Iterate over each
    # bits of xo
    while (xo):
 
        # If current bit of xo
        # is 1
        if (xo % 2 == 1):
            #  Update count
            count+=1
 
 
        # Update xo
        xo = xo // 2
 
    return count
 
 
# Function to find length of shortest
# path between the nodes a and b
def shortestPath(n, a, b):
 
    # Stores XOR of a and b
    xorVal = a ^ b
 
    # Stores the count of
    # set bits in xorVal
    cnt = countbitdiff(xorVal)
 
    # If cnt is an even number
    if (cnt % 2 == 0):
        print(cnt // 2)
    else:
        print("-1")
 
 
# Driver Code
if __name__ == '__main__':
    # Given N
    n = 15
 
    # Given a and b
    a,b = 15,3
 
    # Function call
    shortestPath(n, a, b)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
class GFG {
 
// Function to count set bits
// in a number
static int countbitdiff(int xo)
{
     
    // Stores count of
    // set bits in xo
    int count = 0;
 
    // Iterate over each
    // bits of xo
    while (xo != 0)
    {
         
        // If current bit of xo
        // is 1
        if (xo % 2 == 1)
        {
             
            // Update count
            count++;
        }
         
        // Update xo
        xo = xo / 2;
    }
    return count;
}
 
// Function to find length of shortest
// path between the nodes a and b
static void shortestPath(int n, int a, int b)
{
     
    // Stores XOR of a and b
    int xorVal = a ^ b;
 
    // Stores the count of
    // set bits in xorVal
    int cnt = countbitdiff(xorVal);
 
    // If cnt is an even number
    if (cnt % 2 == 0)
        Console.Write(cnt / 2);
    else
        Console.Write("-1");
}
 
  // Driver code
  public static void Main (String[] args)
  {
 
    // Given N
    int n = 15;
 
    // Given a and b
    int a = 15, b = 3;
 
    // Function call
    shortestPath(n, a, b);
  }
}
 
 
// This code is contributed by code_hunt.


Javascript


输出:
1

时间复杂度: O(log 2 (N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程