Given a tree with the weights of all the nodes, the task is to count the number of nodes whose weight is a Fibonacci number.
Examples:
Input:

Output: 2
Explanation:
Nodes having weights 5 and 8 are fibonacci nodes.
Input:

Output: 3
Explanation:
Nodes having weights 1, 3 and 8 are fibonacci nodes.
Approach: The idea is to perform a dfs on the tree and for every node, check whether the weight is a Fibonacci number or not.
- Generate a hash containing all the Fibonacci numbers using Dynamic programming.
- Using depth-first search traversal, traverse through every node of the tree and check whether the node is a Fibonacci number or not by checking if that element is present in the precomputed hash or not.
- Finally, print the total number of Fibonacci nodes.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5;
int ans = 0;
vector< int > graph[100];
vector< int > weight(100);
set< int > fib;
void fibonacci()
{
int prev = 0, curr = 1, len = 2;
fib.insert(prev);
fib.insert(curr);
while (len <= sz) {
int temp = curr + prev;
fib.insert(temp);
prev = curr;
curr = temp;
len++;
}
}
void dfs( int node, int parent)
{
if (fib.find(weight[node]) != fib.end())
ans += 1;
for ( int to : graph[node]) {
if (to == parent)
continue ;
dfs(to, node);
}
}
int main()
{
weight[1] = 5;
weight[2] = 10;
weight[3] = 11;
weight[4] = 8;
weight[5] = 6;
graph[1].push_back(2);
graph[2].push_back(3);
graph[2].push_back(4);
graph[1].push_back(5);
fibonacci();
dfs(1, 1);
cout << ans << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int sz = ( int ) 1e5;
static int ans = 0 ;
static Vector<Integer> []graph = new Vector[ 100 ];
static int []weight = new int [ 100 ];
static HashSet<Integer> fib = new HashSet<Integer>();
static void fibonacci()
{
int prev = 0 , curr = 1 , len = 2 ;
fib.add(prev);
fib.add(curr);
while (len <= sz) {
int temp = curr + prev;
fib.add(temp);
prev = curr;
curr = temp;
len++;
}
}
static void dfs( int node, int parent)
{
if (fib.contains(weight[node]))
ans += 1 ;
for ( int to : graph[node]) {
if (to == parent)
continue ;
dfs(to, node);
}
}
public static void main(String[] args)
{
for ( int i = 0 ; i < 100 ; i++) {
graph[i] = new Vector<Integer>();
}
weight[ 1 ] = 5 ;
weight[ 2 ] = 10 ;
weight[ 3 ] = 11 ;
weight[ 4 ] = 8 ;
weight[ 5 ] = 6 ;
graph[ 1 ].add( 2 );
graph[ 2 ].add( 3 );
graph[ 2 ].add( 4 );
graph[ 1 ].add( 5 );
fibonacci();
dfs( 1 , 1 );
System.out.print(ans + "\n" );
}
}
|
Python3
sz = 1e5
ans = 0
graph = [[] for i in range ( 100 )]
weight = [ 0 for i in range ( 100 )]
fib = set ()
def fibonacci():
prev = 0
curr = 1
len1 = 2
fib.add(prev)
fib.add(curr)
while (len1 < = sz):
temp = curr + prev
fib.add(temp)
prev = curr;
curr = temp;
len1 + = 1
def dfs(node, parent):
global ans
if (weight[node] in fib):
ans + = 1
for to in graph[node]:
if (to = = parent):
continue
dfs(to, node)
if __name__ = = '__main__' :
weight[ 1 ] = 5
weight[ 2 ] = 10
weight[ 3 ] = 11
weight[ 4 ] = 8
weight[ 5 ] = 6
graph[ 1 ].append( 2 )
graph[ 2 ].append( 3 )
graph[ 2 ].append( 4 )
graph[ 1 ].append( 5 )
fibonacci()
dfs( 1 , 1 )
print (ans)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int sz = ( int ) 1e5;
static int ans = 0;
static List< int > []graph = new List< int >[100];
static int []weight = new int [100];
static HashSet< int > fib = new HashSet< int >();
static void fibonacci()
{
int prev = 0, curr = 1, len = 2;
fib.Add(prev);
fib.Add(curr);
while (len <= sz) {
int temp = curr + prev;
fib.Add(temp);
prev = curr;
curr = temp;
len++;
}
}
static void dfs( int node, int parent)
{
if (fib.Contains(weight[node]))
ans += 1;
foreach ( int to in graph[node]) {
if (to == parent)
continue ;
dfs(to, node);
}
}
public static void Main(String[] args)
{
for ( int i = 0; i < 100; i++) {
graph[i] = new List< int >();
}
weight[1] = 5;
weight[2] = 10;
weight[3] = 11;
weight[4] = 8;
weight[5] = 6;
graph[1].Add(2);
graph[2].Add(3);
graph[2].Add(4);
graph[1].Add(5);
fibonacci();
dfs(1, 1);
Console.Write(ans + "\n" );
}
}
|
Javascript
<script>
var sz = 1000000;
var ans = 0;
var graph = Array.from(Array(100), ()=>Array());
var weight = Array(100);
var fib = new Set();
function fibonacci()
{
var prev = 0, curr = 1, len = 2;
fib.add(prev);
fib.add(curr);
while (len <= sz) {
var temp = curr + prev;
fib.add(temp);
prev = curr;
curr = temp;
len++;
}
}
function dfs(node, parent)
{
if (fib.has(weight[node]))
ans += 1;
for ( var to of graph[node]) {
if (to == parent)
continue ;
dfs(to, node);
}
}
weight[1] = 5;
weight[2] = 10;
weight[3] = 11;
weight[4] = 8;
weight[5] = 6;
graph[1].push(2);
graph[2].push(3);
graph[2].push(4);
graph[1].push(5);
fibonacci();
dfs(1, 1);
document.write(ans + "<br>" );
</script>
|
Complexity Analysis:
- Time Complexity : O(N).
In dfs, every node of the tree is processed once and hence the complexity due to the dfs is O(N) if there are total N nodes in the tree. Also, for processing each node the fibonacci() function is used which has a complexity of O(N) too but since this function is executed only once, it does not affect the overall time complexity. Therefore, the time complexity is O(N). - Auxiliary Space : O(N).
Extra space is used for the fibonacci hashset, so the space complexity is O(N).