Skip to content
Related Articles

Related Articles

Print the node with the maximum degree in the prufer sequence
  • Last Updated : 06 Jun, 2019

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the node with
// the maximum degree in the tree
// whose Prufer sequence is given
int findMaxDegreeNode(int prufer[], int n)
{
    int nodes = n + 2;
  
    // Hash-table to mark the
    // degree of every node
    int degree[n + 2 + 1];
  
    // Initially let all the degrees be 1
    for (int i = 1; i <= nodes; i++)
        degree[i] = 1;
  
    // Increase the count of the degree
    for (int i = 0; i < n; i++)
        degree[prufer[i]]++;
  
    int maxDegree = 0;
    int node = 0;
  
    // Find the node with maximum degree
    for (int i = 1; i <= nodes; i++) {
        if (degree[i] > maxDegree) {
            maxDegree = degree[i];
            node = i;
        }
    }
  
    return node;
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 2 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << findMaxDegreeNode(a, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
import java.io.*;
  
class GFG 
{
          
    // Function to return the node with 
    // the maximum degree in the tree 
    // whose Prufer sequence is given 
    static int findMaxDegreeNode(int prufer[], int n) 
    
        int nodes = n + 2
      
        // Hash-table to mark the 
        // degree of every node 
        int []degree = new int[n + 2 + 1]; 
      
        // Initially let all the degrees be 1 
        for (int i = 1; i <= nodes; i++) 
            degree[i] = 1
      
        // Increase the count of the degree 
        for (int i = 0; i < n; i++) 
            degree[prufer[i]]++; 
      
        int maxDegree = 0
        int node = 0
      
        // Find the node with maximum degree 
        for (int i = 1; i <= nodes; i++)
        
            if (degree[i] > maxDegree) 
            
                maxDegree = degree[i]; 
                node = i; 
            
        
      
        return node; 
    
      
    // Driver code 
    public static void main (String[] args) 
    {
  
        int []a = { 1, 2, 2 }; 
        int n = a.length;
        System.out.println(findMaxDegreeNode(a, n)); 
    
}
  
// This code is contributed by ajit_00023

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

      
# Python implementation of the approach
  
# Function to return the node with
# the maximum degree in the tree
# whose Prufer sequence is given
def findMaxDegreeNode(prufer, n):
    nodes = n + 2;
   
    # Hash-table to mark the
    # degree of every node
    degree = [0]*(n + 2 + 1);
   
    # Initially let all the degrees be 1
    for i in range(1,nodes+1):
        degree[i] = 1;
   
    # Increase the count of the degree
    for i in range(n):
        degree[prufer[i]]+=1;
   
    maxDegree = 0;
    node = 0;
   
    # Find the node with maximum degree
    for i in range(1,nodes+1):
        if (degree[i] > maxDegree):
            maxDegree = degree[i];
            node = i;
  
    return node;
  
   
# Driver code
a = [ 1, 2, 2 ];
n = len(a);
print(findMaxDegreeNode(a, n));
  
# This code has been contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG
{
      
    // Function to return the node with 
    // the maximum degree in the tree 
    // whose Prufer sequence is given 
    static int findMaxDegreeNode(int []prufer, int n) 
    
        int nodes = n + 2; 
      
        // Hash-table to mark the 
        // degree of every node 
        int []degree = new int[n + 2 + 1]; 
      
        // Initially let all the degrees be 1 
        for (int i = 1; i <= nodes; i++) 
            degree[i] = 1; 
      
        // Increase the count of the degree 
        for (int i = 0; i < n; i++) 
            degree[prufer[i]]++; 
      
        int maxDegree = 0; 
        int node = 0; 
      
        // Find the node with maximum degree 
        for (int i = 1; i <= nodes; i++)
        
            if (degree[i] > maxDegree) 
            
                maxDegree = degree[i]; 
                node = i; 
            
        
      
        return node; 
    
      
    // Driver code 
    static public void Main () 
    
        int []a = { 1, 2, 2 }; 
        int n = a.Length; 
          
        Console.WriteLine(findMaxDegreeNode(a, n)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :