Fibonacci Cube Graph
Last Updated :
03 Oct, 2022
You are given input as order of graph n (highest number of edges connected to a node), you have to find the number of vertices in a Fibonacci cube graph of order n.
Examples :
Input : n = 3
Output : 5
Explanation :
Fib(n + 2) = Fib(5) = 5
Input : n = 2
Output : 3
A Fibonacci Cube Graph is similar to hypercube graph, but with a fibonacci number of vertices. In fibonacci cube graph only 1 vertex has degree n rest all has degree less than n.
Fibonacci cube graph of order n has F(n + 2) vertices, where F(n) is a n-th fibonacci number,
Fibonacci series : 1, 1, 2, 3, 5, 8, 13, 21, 34……………….
For input n as order of graph, find the corresponding fibonacci number at the position n + 2.
where F(n) = F(n – 1) + F(n – 2)
Approach : Find the (n + 2)-th fibonacci number.
Below is the implementation of above approach :
C++
#include<iostream>
using namespace std;
int fib( int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
int findVertices ( int n)
{
return fib(n + 2);
}
int main()
{
int n = 3;
cout << findVertices(n);
return 0;
}
|
Java
public class GFG {
static int fib( int n)
{
if (n <= 1 )
return n;
return fib(n - 1 ) + fib(n - 2 );
}
static int findVertices ( int n)
{
return fib(n + 2 );
}
public static void main(String args[]) {
int n = 3 ;
System.out.println(findVertices(n));
}
}
|
Python3
def fib(n):
if n < = 1 :
return n
return fib(n - 1 ) + fib(n - 2 )
def findVertices(n):
return fib(n + 2 )
if __name__ = = "__main__" :
n = 3
print (findVertices(n))
|
C#
using System;
class GFG {
static int fib( int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
static int findVertices ( int n)
{
return fib(n + 2);
}
static void Main()
{
int n = 3;
Console.Write(findVertices(n));
}
}
|
PHP
<?php
function fib( $n )
{
if ( $n <= 1)
return $n ;
return fib( $n - 1) + fib( $n - 2);
}
function findVertices ( $n )
{
return fib( $n + 2);
}
$n = 3;
echo findVertices( $n );
?>
|
Javascript
<script>
function fib(n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
function findVertices (n)
{
return fib(n + 2);
}
var n = 3;
document.write( findVertices(n));
</script>
|
Note that the above code can be optimized to work in O(Log n) using efficient implementations discussed in Program for Fibonacci numbers
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...