in reply to out of order tree generation
If you are looking to transform the data you already have in the form of a hashtable in order to make searches like slaves_of('Hermes') then one way to do it would be a data structure which knows about its boss and all its slaves (if any). For example:
my %Professor; my %Dwight; my %Leila; my %Hermes = ( # \%XYZ is a reference to hashtable XYZ # [] is an array reference 'slaves' => [\%Dwight, \%Leila], 'boss' => \%Professor ); my %Dwight = ( ... );
Eventually you will have the top boss through which you can traverse your graph because it references everyone else directly or indirectly, assuming no mavericks.
Should you want to make the above object-oriented then you create an object class for Worker with similar attributes like slaves and boss above and also get/set methods to access them. That might be a tad tidier and earn you some OO-kudos.
You can also consider Prolog which was made for remembering and querying such relationships extremely well. But I doubt it will earn you any kudos at all.
Once you have managed to put your data in a suitable data structure or use the hashtable you already have, there's nothing bad with it (just querying it, for slaves, is a bit awkward), then you want to actually render that graph you have into some kind of diagram.
For this there's a special language called DOT. It specifies graph relations like parent/children and it is understood by many graph-drawing packages. They read DOT and output JPEG. So you need to make a function which reads your data and produces DOT the simplest version (i.e. an enumeration of boss/slave relationship) should look like this:
graph AGRAPH { Professor -- Hermes Professor -- Amy ... Hermes -- Cubert ... }
Now that you have your DOT-producing function you feed it to one of many programs like GraphViz to render to an image.
However, Perl being what it is and with so many contributors, GraphViz2 has a Perl API so you can add your data directly to it by adding nodes and edges. See Project Management: Graph & Diagram for Visualizing & Analyzing Structure with GraphViz for something similar (Note: use GraphViz>>2<<). Also there is Graph::Easy which accepts nodes and edges and then outputs DOT etc.
Note that you can convert your data to DOT or enter it into GraphViz2 directly, without the preliminary data structure I suggest. But then you would not be able to validate your data. What if there are some disconnected islands by mistake? The benefit of creating your own data structure is to be able to validate your data. Apart from abstracting and querying (like who is the boss of exactly 3 workers?). But who cares, they all got fired at the end, didn't they!
bw, bliako
|
|---|