# C++ Program to Create the Prufer Code for a Tree

Prufer code uniquely identifies a tree which is given by user as a graph representation with labels from 1 to p. This tree consist p(value is given by user) labels of node. It has sequence of p – 2 values.

## Algorithm

Begin
Declare i, j, ver, edg, minimum, p to the integer datatype.
Print “Enter the number of vertexes: ”.
Enter the value of ver.
Initialize edg = ver-1.
Declare EDG[edg], DG[ver+1] to the integer datatype.
Initialize DG[ver+1] = {0}.
Print “This tree has (value of edg) edges for (value of ver) vertexes”.
Print “There are (value of edg) pairs of vertexes in the tree”.
for(i = 0; i < edg; i++)
Print "Enter the value of vertex pair for edge ".
Print "Enter the value of V(1) vertex: ".
Enter the value of EDG[i].
Print "Enter the value of V(2) vertex: ".
Enter the value of EDG[i].
DG[EDG[i]]++.
DG[EDG[i]]++.
Print "The Prufer code for the tree is: { ".
for(i = 0; i < ver-2; i++)
minimum = 10000
for(j = 0; j < edg; j++)
if(DG[EDG[j]] == 1) then
if(minimum > EDG[j]) then
minimum = EDG[j].
p = j.
if(DG[EDG[j]] == 1) then
if(minimum > EDG[j]) then
minimum = EDG[j].
p = j.
DG[EDG[p]]--.
DG[EDG[p]]--.
if(DG[EDG[p]] == 0) then
Print the value of EDG[p].
else
Print the value of EDG[p].
Print "}".
End.

## Example

Live Demo

#include<iostream>
using namespace std;
int main() {
int i, j, ver, edg, minimum, p;
cout<<"Enter the number of vertexes: ";
cin>>ver;
cout<<endl;
edg = ver-1;
int EDG[edg], DG[ver+1] = {0};
cout<<"This tree has "<<edg<<" edges for "<<ver<<"vertexes.\n";
cout<<"There are "<<edg<<" pairs of vertexes in the three.\n";
for(i = 0; i < edg; i++) {
cout<<"Enter the value of vertex pair for edge "<<i+1<<":\n";
cout<<"Enter the value of V(1) vertex: ";
cin>>EDG[i];
cout<<"Enter the value of V(2) vertex: ";
cin>>EDG[i];
DG[EDG[i]]++;
DG[EDG[i]]++;
}
cout<<"\nThe Prufer code for the tree is: { "; // Print the prufer code of the given tree.
for(i = 0; i < ver-2; i++) {
minimum = 10000;
for(j = 0; j < edg; j++) {
if(DG[EDG[j]] == 1) {
if(minimum > EDG[j]) {
minimum = EDG[j];
p = j;
}
}
if(DG[EDG[j]] == 1) {
if(minimum > EDG[j]) {
minimum = EDG[j];
p = j;
}
}
}
DG[EDG[p]]--; // Remove the selected vertex by decreasing its degree to 0.
DG[EDG[p]]--; // Decrement the degree of other vertex, since we have removed the EDG.
if(DG[EDG[p]] == 0)
cout<<EDG[p]<<" ";
else
cout<<EDG[p]<<" ";
}
cout<<"}";
return 0;
}

## Output

Enter the number of vertexes: 5

This tree has 4 edges for 5vertexes.
There are 4 pairs of vertexes in the three.
Enter the value of vertex pair for edge 1:
Enter the value of V(1) vertex: 2
Enter the value of V(2) vertex: 3
Enter the value of vertex pair for edge 2:
Enter the value of V(1) vertex: 5
Enter the value of V(2) vertex: 6
Enter the value of vertex pair for edge 3:
Enter the value of V(1) vertex: 7
Enter the value of V(2) vertex: 8
Enter the value of vertex pair for edge 4:
Enter the value of V(1) vertex: 9
Enter the value of V(2) vertex: 10

The Prufer code for the tree is: { 4 8 4 }