📜  斐波那契立方体图

📅  最后修改于: 2022-05-13 01:57:54.286000             🧑  作者: Mango

斐波那契立方体图

您以图 n 的顺序给出输入(连接到节点的最大边数),您必须找到 n 阶斐波那契立方体图中的顶点数。
例子 :

Input : n = 3
Output : 5
Explanation : 
Fib(n + 2) = Fib(5) = 5

Input : n = 2
Output : 3

斐波那契立方图类似于超立方图,但具有斐波那契数的顶点。在斐波那契立方体图中,只有 1 个顶点的度数为 n,其余所有顶点的度数都小于 n。
n阶斐波那契立方图有F(n + 2)个顶点,其中F(n)是第n个斐波那契数,
斐波那契数列:1、1、2、3、5、8、13、21、34………………。

斐波那契立方体图

对于输入 n 作为图形的顺序,在位置 n + 2 处找到相应的斐波那契数。
其中 F(n) = F(n – 1) + F(n – 2)
方法:找到第 (n + 2) 个斐波那契数。
以下是上述方法的实现:

C++
// CPP code to find vertices in a fibonacci
// cube graph of order n
#include
using namespace std;
  
// function to find fibonacci number
int fib(int n)
{
    if (n <= 1)
        return n;
    return fib(n - 1) + fib(n - 2);
}
  
// function for finding number of vertices 
// in fibonacci cube graph
int findVertices (int n)
{
    // return fibonacci number for f(n + 2) 
    return fib(n + 2);
}
  
// driver program
int main()
{
    // n is the order of the graph
    int n = 3;
    cout << findVertices(n);
    return 0;
}


Java
// java code to find vertices in a fibonacci
// cube graph of order n
public class GFG {
      
    // function to find fibonacci number
    static int fib(int n)
    {
        if (n <= 1)
            return n;
        return fib(n - 1) + fib(n - 2);
    }
      
    // function for finding number of vertices 
    // in fibonacci cube graph
    static int findVertices (int n)
    {
        // return fibonacci number for f(n + 2) 
        return fib(n + 2);
    }
          
    public static void main(String args[]) {
          
        // n is the order of the graph
        int n = 3;
        System.out.println(findVertices(n));
    }
}
  
// This code is contributed by Sam007


Python3
# Python3 code to find vertices in 
# a fibonacci cube graph of order n 
  
# Function to find fibonacci number 
def fib(n): 
  
    if n <= 1: 
        return n
          
    return fib(n - 1) + fib(n - 2) 
  
# Function for finding number of 
# vertices in fibonacci cube graph 
def findVertices(n): 
  
    # return fibonacci number 
    # for f(n + 2) 
    return fib(n + 2) 
  
# Driver Code
if __name__ == "__main__": 
  
    # n is the order of the graph 
    n = 3
    print(findVertices(n)) 
  
# This code is contributed 
# by Rituraj Jain


C#
// C# code to find vertices in a fibonacci
// cube graph of order n
using System;
  
class GFG {
      
    // function to find fibonacci number
    static int fib(int n)
    {
        if (n <= 1)
            return n;
        return fib(n - 1) + fib(n - 2);
    }
      
    // function for finding number of 
    // vertices in fibonacci cube graph
    static int findVertices (int n)
    {
          
        // return fibonacci number for
        // f(n + 2) 
        return fib(n + 2);
    }
      
    // Driver code
    static void Main()
    {
          
        // n is the order of the graph
        int n = 3;
          
        Console.Write(findVertices(n));
    }
}
  
// This code is contributed by Sam007


PHP


Javascript


输出 :
5

请注意,使用斐波那契数程序中讨论的有效实现可以优化上述代码以在 O(Log n) 中工作