Print the node with the maximum degree in the prufer sequence
Last Updated :
22 Jun, 2022
Given a Prufer sequence of a Tree, the task is to print the node with the maximum degree in the tree whose Prufer sequence is given. In case there are many nodes with maximum degree, print the node with the smallest number.
Examples:
Input: a[] = {4, 1, 3, 4}
Output: 4
The tree is:
2----4----3----1----5
|
6
Input: a[] = {1, 2, 2}
Output: 2
A simple approach is to create the tree using the Prufer sequence and then find the degree of all the nodes and then find the maximum among them.
Efficient approach: Create a degree[] array of size 2 more than the length of the Prufer sequence, since the length of prufer sequence is N – 2 if N is the number of nodes. Initially, fill the degree array with 1. Iterate in the Prufer sequence and increase the frequency in the degree table for every element. This method works because the frequency of a node in the Prufer sequence is one less than the degree in the tree. Now iterate in the degree array and find the node with the maximum frequency which will be our answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxDegreeNode( int prufer[], int n)
{
int nodes = n + 2;
int degree[n + 2 + 1];
for ( int i = 1; i <= nodes; i++)
degree[i] = 1;
for ( int i = 0; i < n; i++)
degree[prufer[i]]++;
int maxDegree = 0;
int node = 0;
for ( int i = 1; i <= nodes; i++) {
if (degree[i] > maxDegree) {
maxDegree = degree[i];
node = i;
}
}
return node;
}
int main()
{
int a[] = { 1, 2, 2 };
int n = sizeof (a) / sizeof (a[0]);
cout << findMaxDegreeNode(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findMaxDegreeNode( int prufer[], int n)
{
int nodes = n + 2 ;
int []degree = new int [n + 2 + 1 ];
for ( int i = 1 ; i <= nodes; i++)
degree[i] = 1 ;
for ( int i = 0 ; i < n; i++)
degree[prufer[i]]++;
int maxDegree = 0 ;
int node = 0 ;
for ( int i = 1 ; i <= nodes; i++)
{
if (degree[i] > maxDegree)
{
maxDegree = degree[i];
node = i;
}
}
return node;
}
public static void main (String[] args)
{
int []a = { 1 , 2 , 2 };
int n = a.length;
System.out.println(findMaxDegreeNode(a, n));
}
}
|
Python3
def findMaxDegreeNode(prufer, n):
nodes = n + 2 ;
degree = [ 0 ] * (n + 2 + 1 );
for i in range ( 1 ,nodes + 1 ):
degree[i] = 1 ;
for i in range (n):
degree[prufer[i]] + = 1 ;
maxDegree = 0 ;
node = 0 ;
for i in range ( 1 ,nodes + 1 ):
if (degree[i] > maxDegree):
maxDegree = degree[i];
node = i;
return node;
a = [ 1 , 2 , 2 ];
n = len (a);
print (findMaxDegreeNode(a, n));
|
C#
using System;
class GFG
{
static int findMaxDegreeNode( int []prufer, int n)
{
int nodes = n + 2;
int []degree = new int [n + 2 + 1];
for ( int i = 1; i <= nodes; i++)
degree[i] = 1;
for ( int i = 0; i < n; i++)
degree[prufer[i]]++;
int maxDegree = 0;
int node = 0;
for ( int i = 1; i <= nodes; i++)
{
if (degree[i] > maxDegree)
{
maxDegree = degree[i];
node = i;
}
}
return node;
}
static public void Main ()
{
int []a = { 1, 2, 2 };
int n = a.Length;
Console.WriteLine(findMaxDegreeNode(a, n));
}
}
|
Javascript
<script>
function findMaxDegreeNode(prufer, n)
{
let nodes = n + 2;
let degree = new Array(n + 2 + 1);
degree.fill(0);
for (let i = 1; i <= nodes; i++)
degree[i] = 1;
for (let i = 0; i < n; i++)
degree[prufer[i]]++;
let maxDegree = 0;
let node = 0;
for (let i = 1; i <= nodes; i++)
{
if (degree[i] > maxDegree)
{
maxDegree = degree[i];
node = i;
}
}
return node;
}
let a = [ 1, 2, 2 ];
let n = a.length;
document.write(findMaxDegreeNode(a, n));
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the number of elements in the array.
Auxiliary Space: O(N), as we are using extra space for the degree array. Where N is the number of elements.
Share your thoughts in the comments
Please Login to comment...