Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,483 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,218 people online right now. Registration is fast and FREE... Join Now!




What data structure & algorithm for a multiple hierarchical struct

 
Reply to this topicStart new topic

What data structure & algorithm for a multiple hierarchical struct

Mgccl
post 22 Jun, 2008 - 03:36 PM
Post #1


New D.I.C Head

*
Joined: 22 Jun, 2008
Posts: 2

It seems like a normal graph problem.
Single hierarchical data are trees, each node have only one parent
multiple hierarchical data can have more than one parents.
Don't know if that is the technical term to refer to it.

My initial data structure: each node contain a list of it's direct parent nodes and direct child nodes.

There are a few operations.
1. Select all nodes that's are child/parent node of A & C | B...
2. Select all nodes that's are direct child/parent node of A & C | B...
3. Select all highest parent(that is not root)/lowest child of A & C | B...
4. Is a node child/parent node of A & C | B...
5. Insert a new node provides it's direct parents and direct child.

But even the simplest operation can take a long time with my algorithm.
If one ask "is Z a child of Y and X"
It will start by locate Z(a hash table pointing to each node, so finding a node is O(1)).
P(x) is a function return a set of x's direct parent, where x can be a set of nodes.
Do P(Z).
If Y and X are in it, then return true.
If not, it do P(P(Z))
until it reach the root(all element are the child of the root), which will return false, or find X and Y.

Insertion is pretty fast, O(n) where n is the number of parents+child.

But we can see that if a node has 2 direct parents, and each direct parents have 2 more direct parents... the time complexity is exponential.

So did some improvement, and the current data structure: Each node maintains list of ALL parents and ALL children and mark which ones are direct parents/children.

then it seems everything can be done much faster.
Costing some more storage space though.

Does anyone else know better data structure for this?

Or is there already libraries does the exact same job out there?
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 12 Jul, 2008 - 04:53 PM
Post #2


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


It might be that the technical term for your structure is a directed acyclic graph (DAG). But then again, I don't know enough about what you're doing to be certain on that.

The problem here is that generalized graph operations are inherently expensive in some way. Creating an optimized solution often relies on exploiting some specific property of a particular graph/tree. Binary search trees are a good example of this. It's the rules, not the structure, that make it cheap to search. Otherwise, searching would be an O(n) operation, just like an unsorted list.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 03:18PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month