Hello,
I am trying to come up with an algorithm for the following problem:
I have a table of user_groups and a table of user_groups_heirarchy.
Each user_group may consist of other user_groups which are considered to be its sub_groups (no cycles allowed of course). The information about the sub groups of each group appears in the user_groups_heirarchy table.
The table of user_groups_heirarchy has 2 columns: parent_user_group_id and child_user_group_id (which are both foreign keys to the user_groups id column).
So, the user groups and the relations between them are essentialy a directed acyclic graph (DAG).
Given a user_group id, I want to find all its descendents (that is, all the groups which are its sub_groups, whether directly or indirectly). This is to be done of course with minimum calls to the db.
I have no problem of holding redundant data and additional tables for this task. It just needs to be done as efficiently as possible. Does anybody know of an algorithm for this problem? All the regular DAG algorithms are very efficient when done in memory, but require too many db accesses.
thanks,
dshemesh