给定一个代表图形节点的N个整数的Arr数组。边缘被一对那些其位与不等于零之间。任务是查找图中是否存在长度为3的循环。
例子:
Input: Arr[] = {26, 33, 35, 40, 50}
Output: Yes
A cycle exists between 26, 33 and 50.
Input: Arrr[] = {17, 142, 243, 300}
Output: No
天真的方法:
运行一个嵌套循环,并检查每对之间是否存在边(如果它们的按位AND值不为零)。因此,我们形成了整个图,并使用任何周期检测方法来检查该图中是否存在周期。
高效方法:
- 通过连接至少3个边来形成一个循环。
- 想法是制作一个二维数组,其中第i行存储Arr [i]的二进制值。
- 接下来,以列方式循环并检查是否存在1的数目至少为3的列。
- 如果是这样,则表明图中存在一个循环。
- 如果不存在这样的列,则表示图中没有循环。
Arr[] = {26, 33, 35, 40, 50}
The 2D array is as following
bi = [
[0 1 0 1 1 0 0 0],
[1 0 0 0 0 1 0 0],
[1 1 0 0 0 1 0 0],
[0 0 0 1 0 1 0 0],
[0 1 0 0 1 1 0 0],
]
bi[0][1], bi[2][1] and bi[4][1] are equal to 1. This implies that bitwise AND value of the following pairs (Arr[0], Arr[2]), (Arr[2], Arr[4]), (Arr[0], Arr[4]) are non-zero. These 3 edges form a cycle.
下面是上述方法的实现:
C++
// C++ implementation of
// the above approach
#include
using namespace std;
// Maximum numner of bits in Arr[i]
#define MAX 64
// Function to check if a cycle
// exists in the given graph
void checkCycle(int ar[], int n)
{
int bi[n][MAX] = { 0 }, size;
bool flag = false;
// storing the given ar[i] in
// binary in the array
// where bi[i] is the
// binary value of ar[i]
for (int i = 0; i < n; i++) {
size = 0;
while (ar[i] != 0) {
bi[i][size++] = ar[i] % 2;
ar[i] = ar[i] / 2;
}
}
// Checking if any column
// contains at least 3 1's
for (int i = 0; i < MAX; i++) {
int ctr = 0;
for (int j = 0; j < n; j++) {
if (bi[j][i] == 1)
ctr = ctr + 1;
}
if (ctr >= 3) {
// If number of 1's is more than
// 3, set flag to true and break
flag = true;
break;
}
}
// if flag is true, it implies
// that graph contains a cycle
if (flag)
cout << "Yes";
// if flag is false, no cycle
// is there in the graph
else
cout << "No";
}
// Driver code
int main()
{
int ar[] = { 26, 33, 35, 40, 50 };
int n = sizeof(ar) / sizeof(ar[0]);
checkCycle(ar, n);
}
Java
// Java implementation of
// the above approach
import java.io.*;
class GFG
{
// Maximum numner of bits in Arr[i]
static int MAX = 64;
// Function to check if a cycle
// exists in the given graph
static void checkCycle(int ar[], int n)
{
int bi[][] = new int[n][MAX];
int size;
boolean flag = false;
// storing the given ar[i] in
// binary in the array
// where bi[i] is the
// binary value of ar[i]
for (int i = 0; i < n; i++)
{
size = 0;
while (ar[i] != 0)
{
bi[i][size++] = ar[i] % 2;
ar[i] = ar[i] / 2;
}
}
// Checking if any column
// contains at least 3 1's
for (int i = 0; i < MAX; i++)
{
int ctr = 0;
for (int j = 0; j < n; j++)
{
if (bi[j][i] == 1)
ctr = ctr + 1;
}
if (ctr >= 3)
{
// If number of 1's is more than
// 3, set flag to true and break
flag = true;
break;
}
}
// if flag is true, it implies
// that graph contains a cycle
if (flag)
System.out.println ("Yes");
// if flag is false, no cycle
// is there in the graph
else
System.out.println("No");
}
// Driver code
public static void main (String[] args)
{
int ar[] = { 26, 33, 35, 40, 50 };
int n = ar.length;
checkCycle(ar, n);
}
}
// The code is contrubted by ajit
Python3
# Python3 implementation of
# the above approach
# Maximum numner of bits in Arr[i]
MAX = 64
# Function to check if a cycle
# exists in the given graph
def checkCycle(ar, n):
bi = [[0 for i in range(MAX)]
for j in range(n)]
size = -1
flag = False
# storing the given ar[i] in
# binary in the array
# where bi[i] is the
# binary value of ar[i]
for i in range(n):
size = 0
while ar[i]:
bi[i][size] = ar[i] % 2
ar[i] //= 2
size += 1
# Checking if any column
# contains at least 3 1's
for i in range(MAX):
ctr = 0
for j in range(n):
if bi[j][i] == 1:
ctr += 1
if ctr >= 3:
# If number of 1's is more than
# 3, set flag to true and break
flag = True
break
# if flag is true, it implies
# that graph contains a cycle
if flag:
print("Yes")
# if flag is false, no cycle
# is there in the graph
else:
print("No")
# Driver Code
if __name__ == "__main__":
ar = [26, 33, 35, 40, 50]
n = len(ar)
checkCycle(ar, n)
# This code is contributed by
# sanjeev2552
C#
// C# implementation of
// the above approach
using System;
class GFG
{
// Maximum numner of bits in Arr[i]
static int MAX = 64;
// Function to check if a cycle
// exists in the given graph
static void checkCycle(int []ar, int n)
{
int [,]bi = new int[n, MAX];
int size;
Boolean flag = false;
// storing the given ar[i] in
// binary in the array
// where bi[i] is the
// binary value of ar[i]
for (int i = 0; i < n; i++)
{
size = 0;
while (ar[i] != 0)
{
bi[i, size++] = ar[i] % 2;
ar[i] = ar[i] / 2;
}
}
// Checking if any column
// contains at least 3 1's
for (int i = 0; i < MAX; i++)
{
int ctr = 0;
for (int j = 0; j < n; j++)
{
if (bi[j, i] == 1)
ctr = ctr + 1;
}
if (ctr >= 3)
{
// If number of 1's is more than
// 3, set flag to true and break
flag = true;
break;
}
}
// if flag is true, it implies
// that graph contains a cycle
if (flag)
Console.WriteLine("Yes");
// if flag is false, no cycle
// is there in the graph
else
Console.WriteLine("No");
}
// Driver code
public static void Main (String[] args)
{
int []ar = { 26, 33, 35, 40, 50 };
int n = ar.Length;
checkCycle(ar, n);
}
}
// This code is contributed by PrinciRaj1992
Yes