Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

That was actually the first thing I googled for. Apparently, though, "Prufer" is the more common Anglicism, and neither produced any useful introductory documents.

So what the hell, I'll give it a shot.

The idea is to generate a unique sequence for each labelled tree on N vertices. What you end up doing is removing vertices one at a time, starting with the highest label leaf vertex (you can start with the lowest, it doesn't matter, as long as you're consistent). When you remove a vertex, you add the label of the vertex it was adjacent to to the sequence. Keep going until you only have two vertices left. You've got a Prüfer sequence!

So, for example, if you have the tree:

1--3--2--4 |\ | 5 6

You'd start by removing vertex 6 (highest label leaf vertex). Add 3 to the Prüfer sequence (which is now 3), and you have the tree:

1--3--2--4 \ 5

Now remove 5 and add 3, you have (3, 3) and the tree:

1--3--2--4

Remove 4, add 2, you get (3, 3, 2) -- eventually, you end up with (3, 3, 2, 3) and the tree 1--3.

It turns out that there's a one-to-one correspondence between labelled trees and Prüfer sequences, so each Prüfer sequence uniquely determines a tree. (Which is why the code I posted earlier works.)

Going from a Prüfer sequence to a tree is left as an exercise for the reader. :-)

--
F o x t r o t U n i f o r m
Found a typo in this node? /msg me
The hell with paco, vote for Erudil!


In reply to Re(2): Random Trees by FoxtrotUniform
in thread Random Trees by FoxtrotUniform

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2022-05-19 01:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?