Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> C++

एक पेड़ के लिए प्रूफर कोड बनाने के लिए C++ प्रोग्राम

प्रूफर कोड विशिष्ट रूप से एक पेड़ की पहचान करता है जो उपयोगकर्ता द्वारा 1 से पी तक के लेबल के साथ ग्राफ प्रतिनिधित्व के रूप में दिया जाता है। इस पेड़ में नोड के पी (मूल्य उपयोगकर्ता द्वारा दिया गया है) लेबल शामिल हैं। इसमें p-2 मानों का क्रम है।

एल्गोरिदम

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][2], 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][0].
      Print "Enter the value of V(2) vertex: ".
      Enter the value of EDG[i][1].
      DG[EDG[i][0]]++.
      DG[EDG[i][1]]++.
   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][0]] == 1) then
               if(minimum > EDG[j][0]) then
                  minimum = EDG[j][0].
                  p = j.
            if(DG[EDG[j][1]] == 1) then
               if(minimum > EDG[j][1]) then
                  minimum = EDG[j][1].
                  p = j.
            DG[EDG[p][0]]--.
            DG[EDG[p][1]]--.
      if(DG[EDG[p][0]] == 0) then
         Print the value of EDG[p][1].
      else
         Print the value of EDG[p][0].
   Print "}".
End.

उदाहरण

#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][2], 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][0];
      cout<<"Enter the value of V(2) vertex: ";
      cin>>EDG[i][1];
      DG[EDG[i][0]]++;
      DG[EDG[i][1]]++;
   }
   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][0]] == 1) {
            if(minimum > EDG[j][0]) {
               minimum = EDG[j][0];
               p = j;
            }
         }
         if(DG[EDG[j][1]] == 1) {
            if(minimum > EDG[j][1]) {
               minimum = EDG[j][1];
               p = j;
            }
         }
      }
      DG[EDG[p][0]]--; // Remove the selected vertex by decreasing its degree to 0.
      DG[EDG[p][1]]--; // Decrement the degree of other vertex, since we have removed the EDG.
      if(DG[EDG[p][0]] == 0)
         cout<<EDG[p][1]<<" ";
      else
         cout<<EDG[p][0]<<" ";
   }
   cout<<"}";
   return 0;
}

आउटपुट

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 }

  1. सी ++ प्रोग्राम में एक पेड़ में पूर्वजों-वंशज संबंधों के लिए प्रश्न

    इस समस्या में, हमें एक एन वर्टेक्स ट्री और क्यू प्रश्न दिए गए हैं जिनमें से प्रत्येक में दो मान i और j शामिल हैं। हमारा काम एक पेड़ में पूर्वजों-वंशज संबंधों के लिए एक प्रश्न को हल करने के लिए एक प्रोग्राम बनाना है। प्रत्येक प्रश्न को हल करने के लिए, हमें यह जांचना होगा कि क्या नोड i पेड़ में नोड j

  1. log1p () C++ प्रोग्राम में

    हमें किसी भी प्रकार के चर के साथ दिया गया है और कार्य log1p() फ़ंक्शन का उपयोग करके परिणाम खोजना है। log1p() एक विश्लेषणात्मक कार्य है जो a तर्क लेता है और इसका रिटर्न मान भी होता है। सिंटैक्स double log1p (double x); Where x ranges between [-1, ?] float log1p (float x); वापसी का प्रकार - यह फ़ंक्श

  1. सी ++ प्रोग्राम पाप (एक्स) और कॉस (एक्स) के मूल्य की गणना करने के लिए

    इनपुट को कोण के रूप में दिया गया है और कार्य दिए गए कोण के अनुरूप sin(x) और cos(x) के मान की गणना करना और परिणाम प्रदर्शित करना है पाप के लिए(x) sin(x) एक त्रिकोणमितीय फलन है जिसका उपयोग x कोण के मान की गणना करने के लिए किया जाता है। फॉर्मूला $$\sin (x) =\displaystyle\sum\limits_{k=0}^\infty \fr