
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Degree of Every Node from Prufer Sequence
To print the degree of each hub from a given Prufer arrangement, ready to emphasise through the arrangement and tally the events of each node. By following the recurrence of each node, we will determine the degree of that hub within the corresponding labelled tree. This data gives insights into the network and structure of the tree. By printing the degree of each hub, you are ready to analyse the conveyance and distinguish imperative hubs. This examination makes a difference in understanding the properties and characteristics of the initial tree based on the Prufer arrangement representation.
Methods Used
Frequency counting approach
Adjacency list representation approach
Frequency Counting Approach
The frequency counting approach to printing the degree of each hub from a given Prufer arrangement includes tallying the events of each hub to decide its degree. To actualize this approach, initialise a lexicon or cluster to store the frequencies of hubs. Repeat through the Prufer arrangement and increase the number for each hub experienced. The tally for each hub speaks to its degree within the labelled tree. At last, print the degrees of all hubs based on the recurrence check. This approach gives a clear way to analyse the network and dissemination of hub degrees inside the Prufer arrangement and get the structural characteristics of the first tree.
Algorithm
Initialise a purge word reference or cluster to store the frequencies of nodes.
Iterate through each component "hub" within the Prufer sequence.
Check if "hub" exists within the lexicon or array.
If it exists, increase its tally by 1.
If it doesn't exist, include it in the word reference or cluster with an initial count of 1.
Once the cycle is complete, you've got the frequencies of each hub within the Prufer sequence.
Iterate through each key-value pair within the word reference or array.
The key speaks to a hub, and the esteem speaks to its number or degree within the labelled tree.
Print the hub and its comparing degree for each key-value pair.
The printed degrees of hubs speak to their particular degrees within the labelled 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 Approach
The Adjacency List representation approach includes changing the Prufer grouping into an adjacency list information structure. Initialise a purge adjacency list, and for each component within the Prufer sequence, add a section within the list demonstrating the node's neighbours. When building the adjacency list, keep track of the frequencies of each hub. Finally, identify the hub with the most elevated recurrence within the adjacency list, which compares to the hub with the most extreme degree within the Prufer grouping. This approach permits us to proficiently decide the hub to the greatest degree by utilising the adjacency list's structure and the recurrence data inferred from the Prufer grouping.
Algorithm
Initialise an empty adjacency list and a purge recurrence counter.
Iterate through each component within the Prufer sequence:
a. Increase the recurrence counter for the current node.
b. Include the current hub as a neighbour to the hubs mentioned within the sequence.
Find the hub with the most elevated recurrence within the recurrence counter. This hub compares to the hub with the greatest degree.
Return the hub to the greatest degree.
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:
Conclusion
This article clarifies how to print the degree of each hub in a given Prufer grouping utilising two distinctive approaches: the recurrence tallying approach and the adjacency list representation approach. The recurrence tallying approach includes counting the events of each hub within the grouping to determine its degree. The adjacency list representation approach develops a adjacency list from the arrangement and tracks the repeat of each hub to discover the hub with the most noteworthy degree. The article gives C-code illustrations for both approaches and illustrates their utilisation. By printing the hub degrees, one can analyse the organised structure and recognise critical hubs inside the Prufer arrangement representation.