Home  >  Article  >  Backend Development  >  Print the degree of each node in the given Prufer sequence

Print the degree of each node in the given Prufer sequence

王林
王林forward
2023-09-08 15:09:021232browse

Print the degree of each node in the given Prufer sequence

Print the degree of each center in a given Prufer permutation, ready to highlight and count events for each node through the permutation. By tracing the recursion for each node, we will determine the degree of that center in the corresponding labeled tree. This data provides insight into the tree's network and structure. By printing the degree of each hub, you can analyze the transmission and differentiate the necessary hubs. This examination makes a difference in understanding the properties and characteristics of the initial tree represented based on the Prufer arrangement.

usage instructions

  • Frequency counting method

  • Adjacency list representation method

Frequency counting method

The frequency counting method for printing the degree of each hub from a given Prufer arrangement involves counting the events for each hub to determine its degree. To implement this approach, a dictionary or cluster is initialized to store the frequencies of the centers. Repeat the Prufer arrangement and increase the number of each experienced hub. The count of each hub indicates its degree in the tag tree. Finally, the degrees of all hubs are printed based on repeated checks. This method provides a clear way to analyze the spread of network and hub degrees within the Prufer arrangement and obtain the structural characteristics of the first tree.

algorithm

  • Initialize a clear word reference or cluster to store node frequencies.

  • Iterate over each component "center" in the Prufer sequence.

  • Check if "hub" exists in the dictionary or array.

  • If present, increase its count by 1.

  • If not present, include it in a word reference or cluster with an initial count of 1.

  • Once the loop is complete, you can obtain the frequency of each center in the Prufer sequence.

  • Iterate over each key-value pair in a word reference or array.

  • Keys relate to center, while self-esteem relates to quantity or degree in the tree of markers.

  • Print the center of each key-value pair and its degree of comparison.

  • The printed hub degrees describe their specific degree in the tag tree.

Example

#include <iostream>
#include <vector>

struct HubFrequency {
   int hub;
   int frequency;
};

void countFrequencies(const std::vector<int>& pruferSequence) {
   std::vector<HubFrequency> frequencyVector;

   for (int hub : pruferSequence) {
      bool found = false;
      for (HubFrequency& hf : frequencyVector) {
         if (hf.hub == hub) {
            hf.frequency++;
            found = true;
            break;
         }
      }

      if (!found) {
         frequencyVector.push_back({hub, 1});
      }
   }

   for (const HubFrequency& hf : frequencyVector) {
      std::cout << "Hub: " << hf.hub << ", Degree: " << hf.frequency << std::endl;
   }
}

int main() {
   std::vector<int> pruferSequence = {1, 2, 3, 1, 3};
   countFrequencies(pruferSequence);

   return 0;
}

Output

Hub: 1, Degree: 2
Hub: 2, Degree: 1
Hub: 3, Degree: 2

Adjacency list representation method

The adjacency list representation method includes changing the Prufer grouping to the adjacency list information structure. Initialize a clear adjacency list, and for each component in the Prufer sequence, add a section to the list showing the neighbors of that node. When building your adjacency list, keep track of the frequency of each hub. Finally, the center with the highest repetition rate in the adjacency list is identified and compared to the center with the highest degree in the Prufer grouping. This approach allows us to exploit the structure of adjacency lists and recursive data inferred from Prufer groupings to maximize proficiency in determining hubs.

algorithm

  • Initialize an empty adjacency list and clear the duplicate counter.

  • Iterate over each component in the Prufer sequence:

  • a. Increment the current node's repeat counter.

  • b. Includes the current hub as a neighbor of the hub mentioned in the sequence.

  • Find the center with the highest repetition frequency in the repetition counter. This hub is compared to the hub with the largest degree.

  • Restore the wheel hub to its maximum extent.

Example

#include <iostream>
#include <vector>
#include <unordered_map>

// Function to find the hub with the highest recurrence
int findHighestRecurrence(const std::unordered_map<int, int>& recurrenceCounter) {
   int highestRecurrence = 0;
   int hubWithHighestRecurrence = -1;

   for (const auto& entry : recurrenceCounter) {
      int hub = entry.first;
      int recurrence = entry.second;

      if (recurrence > highestRecurrence) {
         highestRecurrence = recurrence;
         hubWithHighestRecurrence = hub;
      }
   }

   return hubWithHighestRecurrence;
}

// Function to construct adjacency list from Prufer sequence
std::vector<std::vector<int>> constructAdjacencyList(const std::vector<int>& pruferSequence) {
   std::unordered_map<int, int> recurrenceCounter;
   std::vector<std::vector<int>> adjacencyList(pruferSequence.size() + 2);

   for (int hub : pruferSequence) {
      recurrenceCounter[hub]++;
      adjacencyList[hub].push_back(findHighestRecurrence(recurrenceCounter));
      adjacencyList[findHighestRecurrence(recurrenceCounter)].push_back(hub);
   }

   recurrenceCounter[findHighestRecurrence(recurrenceCounter)]++;

   return adjacencyList;
}

int main() {
   // Example Prufer sequence: {1, 3, 4, 2}
   std::vector<int> pruferSequence = {1, 3, 4, 2};
   std::vector<std::vector<int>> adjacencyList = constructAdjacencyList(pruferSequence);

   // Print the constructed adjacency list
   for (int i = 1; i < adjacencyList.size(); i++) {
      std::cout << "Node " << i << " connects to: ";
      for (int j = 0; j < adjacencyList[i].size(); j++) {
         std::cout << adjacencyList[i][j] << " ";
      }
      std::cout << std::endl;
   }

   return 0;
}

Output

Node 1 connects to: 1 1 
Node 2 connects to: 2 2 
Node 3 connects to: 3 3 
Node 4 connects to: 4 4 
Node 5 connects to: 

in conclusion

This article illustrates how to print the degree of each center in a given Prufer grouping using two different methods: the recursive counting method and the adjacency list representation method. Repeat counting methods involve counting events at each center within a grouping to determine their extent. The adjacency list representation method develops adjacency lists based on permutations and tracks the duplication of each hub to discover the hub with the most noteworthy degree. This article provides C code descriptions of both methods and explains their use. By printing hub degrees, we can analyze the tissue structure and identify critical hubs in the Prufer arrangement representation.

The above is the detailed content of Print the degree of each node in the given Prufer sequence. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete