Print the degree of every node from the given Prufer sequence
Last Updated :
22 Jun, 2022
Given a Prufer sequence, the task is to find the degrees of all the nodes of the tree made by the prufer sequence.
Examples:
Input: arr[] = {4, 1, 3, 4}
Output: 2 1 2 3 1 1
The tree is:
2----4----3----1----5
|
6
Input: arr[] = {1, 2, 2}
Output: 2 3 1 1 1
A simple approach is to create the tree using the Prufer sequence and then find the degree of all the nodes.
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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printDegree( int prufer[], int n)
{
int node = n + 2;
int degree[n + 2 + 1];
for ( int i = 1; i <= node; i++)
degree[i] = 1;
for ( int i = 0; i < n; i++)
degree[prufer[i]]++;
for ( int i = 1; i <= node; i++) {
cout << degree[i] << " " ;
}
}
int main()
{
int a[] = { 4, 1, 3, 4 };
int n = sizeof (a) / sizeof (a[0]);
printDegree(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void printDegree( int prufer[], int n)
{
int node = n + 2 ;
int [] degree = new int [n + 2 + 1 ];
for ( int i = 1 ; i <= node; i++)
{
degree[i] = 1 ;
}
for ( int i = 0 ; i < n; i++)
{
degree[prufer[i]]++;
}
for ( int i = 1 ; i <= node; i++)
{
System.out.print(degree[i] + " " );
}
}
public static void main(String[] args)
{
int a[] = { 4 , 1 , 3 , 4 };
int n = a.length;
printDegree(a, n);
}
}
|
Python3
def printDegree(prufer, n):
node = n + 2
degree = [ 1 ] * (n + 2 + 1 )
for i in range ( 0 , n):
degree[prufer[i]] + = 1
for i in range ( 1 , node + 1 ):
print (degree[i], end = " " )
if __name__ = = "__main__" :
a = [ 4 , 1 , 3 , 4 ]
n = len (a)
printDegree(a, n)
|
C#
using System;
class GFG
{
static void printDegree( int []prufer, int n)
{
int node = n + 2;
int [] degree = new int [n + 2 + 1];
for ( int i = 1; i <= node; i++)
{
degree[i] = 1;
}
for ( int i = 0; i < n; i++)
{
degree[prufer[i]]++;
}
for ( int i = 1; i <= node; i++)
{
Console.Write(degree[i] + " " );
}
}
public static void Main(String[] args)
{
int []a = {4, 1, 3, 4};
int n = a.Length;
printDegree(a, n);
}
}
|
Javascript
<script>
function printDegree(prufer , n)
{
var node = n + 2;
var degree = Array(n + 2 + 1).fill(0);
for (i = 1; i <= node; i++) {
degree[i] = 1;
}
for (i = 0; i < n; i++) {
degree[prufer[i]]++;
}
for (i = 1; i <= node; i++) {
document.write(degree[i] + " " );
}
}
var a = [ 4, 1, 3, 4 ];
var n = a.length;
printDegree(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 degree array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...