📌  相关文章
📜  检查所有连接组件的长度是否为斐波那契数

📅  最后修改于: 2021-09-06 05:58:18             🧑  作者: Mango

给定一个具有V个顶点和E 个边的无向图,任务是找到该图的所有连通分量并检查它们的每个长度是否为斐波那契数
例如,考虑下图。

如上所示,连通分量的长度为 2、3 和 2,它们是斐波那契数列。
例子:

方法:
预先计算斐波那契数并将其存储在 HashSet 中。如本文所述,使用 DFS 方法遍历顶点并生成 Connected 组件。检查所有长度是否都存在于预先计算的斐波那契数的 HashSet 中。
下面是上述方法的实现:

C++
// C++ program to check if the length of
// all connected components are a
// Fibonacci or not
#include 
using namespace std;
 
// Function to traverse graph using
// DFS algorithm and track the
// connected components
void depthFirst(int v, vector graph[],
                vector& visited, int& ans)
{
    // Mark the current vertex as visited
    visited[v] = true;
 
    // Variable ans to keep count of
    // connected components
    ans++;
    for (auto i : graph[v]) {
        if (visited[i] == false) {
            depthFirst(i, graph, visited, ans);
        }
    }
}
 
// Function to check and print if the
// length of all connected components
// are a Fibonacci or not
void countConnectedFibonacci(vector graph[],
                                int V, int E)
{
    // Hash Container (Set) to store
    // the Fibonacci sequence
    unordered_set fibonacci;
    fibonacci.insert(0);
    fibonacci.insert(1);
    // Pre-computation of Fibonacci sequence
    long long a = 0,b = 1;
    for (int i = 2; i < 1001; i++) {
        fibonacci.insert(a + b);
        a = a+b;
        swap(a,b);
    }
 
    // Initializing boolean visited array
    // to mark visited vertices
    vector visited(10001, false);
 
 
    // Following loop invokes DFS algorithm
    for (int i = 1; i <= V; i++) {
        if (visited[i] == false) {
            // ans variable stores the
            // length of respective
            // connected components
            int ans = 0;
 
            // DFS algorithm
            depthFirst(i, graph, visited, ans);
            if(fibonacci.find(ans) == fibonacci.end())
            {
                cout << "No"< graph[1001];
     
    // Defining the number of edges and vertices
    int E = 4,V = 7;
 
    // Constructing the undirected graph
    graph[1].push_back(2);
    graph[2].push_back(5);
    graph[3].push_back(4);
    graph[4].push_back(3);
    graph[3].push_back(6);
    graph[6].push_back(3);
    graph[8].push_back(7);
    graph[7].push_back(8);
     
    countConnectedFibonacci(graph, V, E);
    return 0;
}


Java
// Java program to check if the length of
// all connected components are a
// Fibonacci or not
 
import java.util.*;
 
class GFG{
 
// Function to traverse graph using
// DFS algorithm and track the
// connected components
static void depthFirst(int v, Vector graph[],
                boolean []visited, int ans)
{
    // Mark the current vertex as visited
    visited[v] = true;
 
    // Variable ans to keep count of
    // connected components
    ans++;
    for (int i : graph[v]) {
        if (visited[i] == false) {
            depthFirst(i, graph, visited, ans);
        }
    }
}
 
// Function to check and print if the
// length of all connected components
// are a Fibonacci or not
static void countConnectedFibonacci(Vector graph[],
                                int V, int E)
{
    // Hash Container (Set) to store
    // the Fibonacci sequence
    HashSet fibonacci = new HashSet();
    fibonacci.add(0);
    fibonacci.add(1);
    // Pre-computation of Fibonacci sequence
    int a = 0,b = 1;
    for (int i = 2; i < 1001; i++) {
        fibonacci.add(a + b);
        a = a + b;
        a = a + b;
        b = a - b;
        a = a - b;
    }
 
    // Initializing boolean visited array
    // to mark visited vertices
    boolean []visited = new boolean[10001];
 
 
    // Following loop invokes DFS algorithm
    for (int i = 1; i <= V; i++) {
        if (visited[i] == false) {
            // ans variable stores the
            // length of respective
            // connected components
            int ans = 0;
 
            // DFS algorithm
            depthFirst(i, graph, visited, ans);
            if(!fibonacci.contains(ans))
            {
                System.out.println("No");
                return;
            }
        }
    }
 
    System.out.println("Yes");
}
 
// Driver code
public static void main(String[] args)
{
    // Initializing graph in the form of adjacency list
    Vector []graph = new Vector[1001];
    for(int i = 0; i < graph.length; i++)
        graph[i] = new Vector();
         
    // Defining the number of edges and vertices
    int E = 4,V = 7;
 
    // Constructing the undirected graph
    graph[1].add(2);
    graph[2].add(5);
    graph[3].add(4);
    graph[4].add(3);
    graph[3].add(6);
    graph[6].add(3);
    graph[8].add(7);
    graph[7].add(8);
     
    countConnectedFibonacci(graph, V, E);
}
}
 
// This code is contributed by 29AjayKumar


C#
// C# program to check if the length of
// all connected components are a
// Fibonacci or not
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to traverse graph using
// DFS algorithm and track the
// connected components
static void depthFirst(int v, List []graph,
                         bool []visited, int ans)
{
     
    // Mark the current vertex as visited
    visited[v] = true;
 
    // Variable ans to keep count of
    // connected components
    ans++;
    foreach(int i in graph[v])
    {
        if (visited[i] == false)
        {
            depthFirst(i, graph, visited, ans);
        }
    }
}
 
// Function to check and print if the
// length of all connected components
// are a Fibonacci or not
static void countConnectedFibonacci(List []graph,
                                    int V, int E)
{
     
    // Hash Container (Set) to store
    // the Fibonacci sequence
    HashSet fibonacci = new HashSet();
    fibonacci.Add(0);
    fibonacci.Add(1);
     
    // Pre-computation of Fibonacci sequence
    int a = 0,b = 1;
    for(int i = 2; i < 1001; i++)
    {
        fibonacci.Add(a + b);
        a = a + b;
        a = a + b;
        b = a - b;
        a = a - b;
    }
 
    // Initializing bool visited array
    // to mark visited vertices
    bool []visited = new bool[10001];
 
 
    // Following loop invokes DFS algorithm
    for(int i = 1; i <= V; i++)
    {
        if (visited[i] == false)
        {
             
            // ans variable stores the
            // length of respective
            // connected components
            int ans = 0;
 
            // DFS algorithm
            depthFirst(i, graph, visited, ans);
             
            if(!fibonacci.Contains(ans))
            {
                Console.WriteLine("No");
                return;
            }
        }
    }
    Console.WriteLine("Yes");
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Initializing graph in the
    // form of adjacency list
    List []graph = new List[1001];
    for(int i = 0; i < graph.Length; i++)
        graph[i] = new List();
         
    // Defining the number of edges and vertices
    int E = 4,V = 7;
 
    // Constructing the undirected graph
    graph[1].Add(2);
    graph[2].Add(5);
    graph[3].Add(4);
    graph[4].Add(3);
    graph[3].Add(6);
    graph[6].Add(3);
    graph[8].Add(7);
    graph[7].Add(8);
     
    countConnectedFibonacci(graph, V, E);
}
}
 
// This code is contributed by amal kumar choubey


C++
// C++ program to check if the length of
// all connected components are a
// Fibonacci or not
#include 
using namespace std;
 
// Function to traverse graph using
// DFS algorithm and track the
// connected components
void depthFirst(int v, vector graph[],
                vector& visited, int& ans)
{
    // Mark the current vertex as visited
    visited[v] = true;
 
    // Variable ans to keep count of
    // connected components
    ans++;
    for (auto i : graph[v]) {
        if (visited[i] == false) {
            depthFirst(i, graph, visited, ans);
        }
    }
}
 
// Function to check and print if the
// length of all connected components
// are a Fibonacci or not
void countConnectedFibonacci(vector graph[],
                                int V, int E)
{
 
    // Initializing boolean visited array
    // to mark visited vertices
    vector visited(10001, false);
 
 
    // Following loop invokes DFS algorithm
    for (int i = 1; i <= V; i++) {
        if (visited[i] == false) {
            // ans variable stores the
            // length of respective
            // connected components
            int ans = 0;
 
            // DFS algorithm
            depthFirst(i, graph, visited, ans);
             
            double x1 = sqrt(5*ans*ans + 4);
            int x2 = sqrt(5 * ans * ans + 4);
             
            double y1 = sqrt(5*ans*ans - 4);
            int y2 = sqrt(5 * ans * ans - 4);
             
            if(!(x1 - x2) || !(y1 - y2))
                continue;
            else
            {
                cout << "No"< graph[1001];
     
    // Defining the number of edges and vertices
    int E = 4,V = 7;
 
    // Constructing the undirected graph
    graph[1].push_back(2);
    graph[2].push_back(1);
    graph[2].push_back(5);
    graph[5].push_back(2);
    graph[3].push_back(4);
    graph[4].push_back(3);
    graph[3].push_back(6);
    graph[6].push_back(3);
    graph[8].push_back(7);
    graph[7].push_back(8);
     
    countConnectedFibonacci(graph, V, E);
    return 0;
}


Java
// Java program to check if the length of
// all connected components are a
// Fibonacci or not
import java.util.*;
class GFG{
 
// Function to traverse graph using
// DFS algorithm and track the
// connected components
static void depthFirst(int v, Vector graph[],
                    Vector visited, int ans)
{
    // Mark the current vertex as visited
    visited.add(v, true);
 
    // Variable ans to keep count of
    // connected components
    ans++;
    for (int i : graph[v])
    {
        if (visited.get(i) == false)
        {
            depthFirst(i, graph, visited, ans);
        }
    }
}
 
// Function to check and print if the
// length of all connected components
// are a Fibonacci or not
static void countConnectedFibonacci(Vector graph[],
                                    int V, int E)
{
 
    // Initializing boolean visited array
    // to mark visited vertices
    Vector visited = new Vector<>(10001);
    for(int i = 0; i < 10001; i++)
        visited.add(i, false);
 
    // Following loop invokes DFS algorithm
    for (int i = 1; i < V; i++)
    {
        if (visited.get(i) == false)
        {
            // ans variable stores the
            // length of respective
            // connected components
            int ans = 0;
 
            // DFS algorithm
            depthFirst(i, graph, visited, ans);
             
            double x1 = Math.sqrt(5 * ans * ans + 4);
            int x2 = (int)Math.sqrt(5 * ans * ans + 4);
             
            double y1 = Math.sqrt(5 * ans * ans - 4);
            int y2 = (int)Math.sqrt(5 * ans * ans - 4);
             
            if((x1 - x2) != 0 || (y1 - y2) != 0)
                continue;
            else
            {
                System.out.println("No");
                return;
            }
        }
    }
    System.out.println("Yes");
}
 
// Driver code
public static void main(String[] args)
{
    // Initializing graph in the form of adjacency list
    @SuppressWarnings("unchecked")
    Vector []graph = new Vector[1001];
    for(int i = 0; i < 1001; i++)
        graph[i] = new Vector();
   
    // Defining the number of edges and vertices
    int E = 4,V = 7;
 
    // Constructing the undirected graph
    graph[1].add(2);
    graph[2].add(1);
    graph[2].add(5);
    graph[5].add(2);
    graph[3].add(4);
    graph[4].add(3);
    graph[3].add(6);
    graph[6].add(3);
    graph[8].add(7);
    graph[7].add(8);
     
    countConnectedFibonacci(graph, V, E);
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 program to check if the length of
# all connected components are a
# Fibonacci or not
from math import sqrt
 
# Function to traverse graph using
# DFS algorithm and track the
# connected components
def depthFirst(v):
    global visited, ans, graph
     
    # Mark the current vertex as visited
    visited[v] = True
 
    # Variable ans to keep count of
    # connected components
    ans += 1
    for i in graph[v]:
        if (visited[i] == False):
            depthFirst(i)
 
# Function to check and prif the
# length of all connected components
# are a Fibonacci or not
def countConnectedFibonacci(V, E):
    global graph, ans
 
    # Initializing boolean visited array
    # to mark visited vertices
    # vector visited(10001, false)
 
    # Following loop invokes DFS algorithm
    for i in range(1, V + 1):
        if (visited[i] == False):
           
            # ans variable stores the
            # length of respective
            # connected components
            ans = 0
 
            # DFS algorithm
            depthFirst(i)
            x1 = sqrt(5*ans*ans + 4)
            x2 = sqrt(5 * ans * ans + 4)
            y1 = sqrt(5*ans*ans - 4)
            y2 = sqrt(5 * ans * ans - 4)
            if((not (x1 - x2)) or (not (y1 - y2))):
                continue
            else:
                print("No")
                return
    print ("Yes")
 
# Driver code
if __name__ == '__main__':
   
    # Initializing graph in the form of adjacency list
    graph = [[] for i in range(10001)]
    visited = [False for i in range(10001)]
    ans = 0
 
    # Defining the number of edges and vertices
    E, V = 4, 7
 
    # Constructing the undirected graph
    graph[1].append(2)
    graph[2].append(1)
    graph[2].append(5)
    graph[5].append(2)
    graph[3].append(4)
    graph[4].append(3)
    graph[3].append(6)
    graph[6].append(3)
    graph[8].append(7)
    graph[7].append(8)
 
    countConnectedFibonacci(V, E)
 
# This code is contributed by mohit kumar 29.


C#
// C# program to check if the
// length of all connected
// components are a Fibonacci
// or not
using System;
using System.Collections;
class GFG{
  
// Function to traverse graph using
// DFS algorithm and track the
// connected components
static void depthFirst(int v, ArrayList []graph,
                       ArrayList visited, int ans)
{
  // Mark the current vertex
  // as visited
  visited[v] = true;
 
  // Variable ans to keep count of
  // connected components
  ans++;
   
  foreach(int i in graph[v])
  {
    if ((bool)visited[i] == false)
    {
      depthFirst(i, graph, visited, ans);
    }
  }
}
  
// Function to check and print if the
// length of all connected components
// are a Fibonacci or not
static void countConnectedFibonacci(ArrayList []graph,
                                    int V, int E)
{
  // Initializing boolean visited array
  // to mark visited vertices
  ArrayList visited = new ArrayList();
  for(int i = 0; i < 10001; i++)
    visited.Add(false);
 
  // Following loop invokes
  // DFS algorithm
  for (int i = 1; i < V; i++)
  {
    if ((bool)visited[i] == false)
    {
      // ans variable stores the
      // length of respective
      // connected components
      int ans = 0;
 
      // DFS algorithm
      depthFirst(i, graph, visited, ans);
 
      double x1 = Math.Sqrt(5 * ans * ans + 4);
      int x2 = (int)Math.Sqrt(5 * ans * ans + 4);
 
      double y1 = Math.Sqrt(5 * ans * ans - 4);
      int y2 = (int)Math.Sqrt(5 * ans * ans - 4);
 
      if((x1 - x2) != 0 || (y1 - y2) != 0)
        continue;
      else
      {
        Console.Write("No");
        return;
      }
    }
  }
  Console.Write("Yes");
}
 
// Driver code
public static void Main(string[] args)
{
  // Initializing graph in the
  // form of adjacency list
  ArrayList []graph =
              new ArrayList[1001];
 
  for(int i = 0; i < 1001; i++)
    graph[i] = new ArrayList();
 
  // Defining the number of
  // edges and vertices
  int E = 4,
      V = 7;
 
  // Constructing the
  // undirected graph
  graph[1].Add(2);
  graph[2].Add(1);
  graph[2].Add(5);
  graph[5].Add(2);
  graph[3].Add(4);
  graph[4].Add(3);
  graph[3].Add(6);
  graph[6].Add(3);
  graph[8].Add(7);
  graph[7].Add(8);
 
  countConnectedFibonacci(graph, V, E);
}
}
 
// This code is contributed by rutvik_56


输出:
Yes

复杂度分析:
程序的整体复杂性主要由三个因素决定,即深度优先搜索遍历、Fibonacci 容器中元素的识别以及 Fibonacci 序列的预计算。 DFS 遍历的时间复杂度为O(E + V) ,其中 E 和 V 是图的边和顶点。检查 HashSet 中是否存在特定长度需要O(1)时间复杂度。初始预计算的时间复杂度为 O(N),其中 N 是存储斐波那契数列的数量。
时间复杂度: O(N)
有效的方法:
这种方法基本上避免了斐波那契预先计算,并使用一个简单的公式来检查各个长度是否为斐波那契数。检测 N 是否为斐波那契数的公式是找到5N 2 + 45N 2 – 4的值,并检查它们中的任何一个是否是完全平方数。上述配方由 I Gessel 制定,可从此链接参考。该程序的其余部分具有与上述类似的方法,通过 DFS 遍历计算连接组件。
下面是上述方法的实现:

C++

// C++ program to check if the length of
// all connected components are a
// Fibonacci or not
#include 
using namespace std;
 
// Function to traverse graph using
// DFS algorithm and track the
// connected components
void depthFirst(int v, vector graph[],
                vector& visited, int& ans)
{
    // Mark the current vertex as visited
    visited[v] = true;
 
    // Variable ans to keep count of
    // connected components
    ans++;
    for (auto i : graph[v]) {
        if (visited[i] == false) {
            depthFirst(i, graph, visited, ans);
        }
    }
}
 
// Function to check and print if the
// length of all connected components
// are a Fibonacci or not
void countConnectedFibonacci(vector graph[],
                                int V, int E)
{
 
    // Initializing boolean visited array
    // to mark visited vertices
    vector visited(10001, false);
 
 
    // Following loop invokes DFS algorithm
    for (int i = 1; i <= V; i++) {
        if (visited[i] == false) {
            // ans variable stores the
            // length of respective
            // connected components
            int ans = 0;
 
            // DFS algorithm
            depthFirst(i, graph, visited, ans);
             
            double x1 = sqrt(5*ans*ans + 4);
            int x2 = sqrt(5 * ans * ans + 4);
             
            double y1 = sqrt(5*ans*ans - 4);
            int y2 = sqrt(5 * ans * ans - 4);
             
            if(!(x1 - x2) || !(y1 - y2))
                continue;
            else
            {
                cout << "No"< graph[1001];
     
    // Defining the number of edges and vertices
    int E = 4,V = 7;
 
    // Constructing the undirected graph
    graph[1].push_back(2);
    graph[2].push_back(1);
    graph[2].push_back(5);
    graph[5].push_back(2);
    graph[3].push_back(4);
    graph[4].push_back(3);
    graph[3].push_back(6);
    graph[6].push_back(3);
    graph[8].push_back(7);
    graph[7].push_back(8);
     
    countConnectedFibonacci(graph, V, E);
    return 0;
}

Java

// Java program to check if the length of
// all connected components are a
// Fibonacci or not
import java.util.*;
class GFG{
 
// Function to traverse graph using
// DFS algorithm and track the
// connected components
static void depthFirst(int v, Vector graph[],
                    Vector visited, int ans)
{
    // Mark the current vertex as visited
    visited.add(v, true);
 
    // Variable ans to keep count of
    // connected components
    ans++;
    for (int i : graph[v])
    {
        if (visited.get(i) == false)
        {
            depthFirst(i, graph, visited, ans);
        }
    }
}
 
// Function to check and print if the
// length of all connected components
// are a Fibonacci or not
static void countConnectedFibonacci(Vector graph[],
                                    int V, int E)
{
 
    // Initializing boolean visited array
    // to mark visited vertices
    Vector visited = new Vector<>(10001);
    for(int i = 0; i < 10001; i++)
        visited.add(i, false);
 
    // Following loop invokes DFS algorithm
    for (int i = 1; i < V; i++)
    {
        if (visited.get(i) == false)
        {
            // ans variable stores the
            // length of respective
            // connected components
            int ans = 0;
 
            // DFS algorithm
            depthFirst(i, graph, visited, ans);
             
            double x1 = Math.sqrt(5 * ans * ans + 4);
            int x2 = (int)Math.sqrt(5 * ans * ans + 4);
             
            double y1 = Math.sqrt(5 * ans * ans - 4);
            int y2 = (int)Math.sqrt(5 * ans * ans - 4);
             
            if((x1 - x2) != 0 || (y1 - y2) != 0)
                continue;
            else
            {
                System.out.println("No");
                return;
            }
        }
    }
    System.out.println("Yes");
}
 
// Driver code
public static void main(String[] args)
{
    // Initializing graph in the form of adjacency list
    @SuppressWarnings("unchecked")
    Vector []graph = new Vector[1001];
    for(int i = 0; i < 1001; i++)
        graph[i] = new Vector();
   
    // Defining the number of edges and vertices
    int E = 4,V = 7;
 
    // Constructing the undirected graph
    graph[1].add(2);
    graph[2].add(1);
    graph[2].add(5);
    graph[5].add(2);
    graph[3].add(4);
    graph[4].add(3);
    graph[3].add(6);
    graph[6].add(3);
    graph[8].add(7);
    graph[7].add(8);
     
    countConnectedFibonacci(graph, V, E);
}
}
 
// This code is contributed by Rohit_ranjan

蟒蛇3

# Python3 program to check if the length of
# all connected components are a
# Fibonacci or not
from math import sqrt
 
# Function to traverse graph using
# DFS algorithm and track the
# connected components
def depthFirst(v):
    global visited, ans, graph
     
    # Mark the current vertex as visited
    visited[v] = True
 
    # Variable ans to keep count of
    # connected components
    ans += 1
    for i in graph[v]:
        if (visited[i] == False):
            depthFirst(i)
 
# Function to check and prif the
# length of all connected components
# are a Fibonacci or not
def countConnectedFibonacci(V, E):
    global graph, ans
 
    # Initializing boolean visited array
    # to mark visited vertices
    # vector visited(10001, false)
 
    # Following loop invokes DFS algorithm
    for i in range(1, V + 1):
        if (visited[i] == False):
           
            # ans variable stores the
            # length of respective
            # connected components
            ans = 0
 
            # DFS algorithm
            depthFirst(i)
            x1 = sqrt(5*ans*ans + 4)
            x2 = sqrt(5 * ans * ans + 4)
            y1 = sqrt(5*ans*ans - 4)
            y2 = sqrt(5 * ans * ans - 4)
            if((not (x1 - x2)) or (not (y1 - y2))):
                continue
            else:
                print("No")
                return
    print ("Yes")
 
# Driver code
if __name__ == '__main__':
   
    # Initializing graph in the form of adjacency list
    graph = [[] for i in range(10001)]
    visited = [False for i in range(10001)]
    ans = 0
 
    # Defining the number of edges and vertices
    E, V = 4, 7
 
    # Constructing the undirected graph
    graph[1].append(2)
    graph[2].append(1)
    graph[2].append(5)
    graph[5].append(2)
    graph[3].append(4)
    graph[4].append(3)
    graph[3].append(6)
    graph[6].append(3)
    graph[8].append(7)
    graph[7].append(8)
 
    countConnectedFibonacci(V, E)
 
# This code is contributed by mohit kumar 29.

C#

// C# program to check if the
// length of all connected
// components are a Fibonacci
// or not
using System;
using System.Collections;
class GFG{
  
// Function to traverse graph using
// DFS algorithm and track the
// connected components
static void depthFirst(int v, ArrayList []graph,
                       ArrayList visited, int ans)
{
  // Mark the current vertex
  // as visited
  visited[v] = true;
 
  // Variable ans to keep count of
  // connected components
  ans++;
   
  foreach(int i in graph[v])
  {
    if ((bool)visited[i] == false)
    {
      depthFirst(i, graph, visited, ans);
    }
  }
}
  
// Function to check and print if the
// length of all connected components
// are a Fibonacci or not
static void countConnectedFibonacci(ArrayList []graph,
                                    int V, int E)
{
  // Initializing boolean visited array
  // to mark visited vertices
  ArrayList visited = new ArrayList();
  for(int i = 0; i < 10001; i++)
    visited.Add(false);
 
  // Following loop invokes
  // DFS algorithm
  for (int i = 1; i < V; i++)
  {
    if ((bool)visited[i] == false)
    {
      // ans variable stores the
      // length of respective
      // connected components
      int ans = 0;
 
      // DFS algorithm
      depthFirst(i, graph, visited, ans);
 
      double x1 = Math.Sqrt(5 * ans * ans + 4);
      int x2 = (int)Math.Sqrt(5 * ans * ans + 4);
 
      double y1 = Math.Sqrt(5 * ans * ans - 4);
      int y2 = (int)Math.Sqrt(5 * ans * ans - 4);
 
      if((x1 - x2) != 0 || (y1 - y2) != 0)
        continue;
      else
      {
        Console.Write("No");
        return;
      }
    }
  }
  Console.Write("Yes");
}
 
// Driver code
public static void Main(string[] args)
{
  // Initializing graph in the
  // form of adjacency list
  ArrayList []graph =
              new ArrayList[1001];
 
  for(int i = 0; i < 1001; i++)
    graph[i] = new ArrayList();
 
  // Defining the number of
  // edges and vertices
  int E = 4,
      V = 7;
 
  // Constructing the
  // undirected graph
  graph[1].Add(2);
  graph[2].Add(1);
  graph[2].Add(5);
  graph[5].Add(2);
  graph[3].Add(4);
  graph[4].Add(3);
  graph[3].Add(6);
  graph[6].Add(3);
  graph[8].Add(7);
  graph[7].Add(8);
 
  countConnectedFibonacci(graph, V, E);
}
}
 
// This code is contributed by rutvik_56
输出:
Yes

复杂度分析:
时间复杂度:O(V + E)
此方法避免了较早的预计算,并使用数学公式来检测各个长度是否为斐波那契数。因此,计算是在恒定时间O(1)和恒定空间中实现的,因为它避免了使用任何 HashSet 来存储斐波那契数。因此,此方法中程序的整体复杂性仅通过 DFS 遍历来决定。因此,复杂度为O(E + V) ,其中 E 和 V 是无向图的边数和顶点数。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live