Sunday 15 January 2012

algorithm - Order objects in hierarchic lists acording to their mutual dependence -



algorithm - Order objects in hierarchic lists acording to their mutual dependence -

i've objects mutual dependencies.

a b (depend of a) c (depend of b) d e (depend of b , f) f (depend of c) g h (depend of b)

i want create hierarchic list of these objects object placed in list after list containing it's dependences.

the previou object list placed this:

a, d, g (these objects have no dependencies) b (b depend of a) c, h (c , h depends of b) f (f depend of c) e (e depend of b , f)

wich algorithm can resolve problem ?

if haven't helping construction already, should utilize brute force.

i utilize list, extend vertically in schema , 1 every node of vertical list.

so, in example, have 5 nodes in vertical list , first node, have list 3 nodes (the first node host a, sec d , 3rd g). sec node of vertical list, have list 1 node, hold b , on.

so algorithm this:

1.for every item 2. if item.dependency in list 3. append item in right node 4. else // item not in list 5. create node in list dependency of item 6. append item in created node

so, in above pseudo-code, when list, mean vertical one.

at step 1, check items have.

at step 2, check if item processing exists in list (by searching entire list , returning position found or smarter).

at step 3, go @ position found in step 2 , insert @ end of list located @ node in position value of item (you don't need store dependency again). note if node in position has no entries in it's list, need create it's list.

at step 4, in case our item not found in list.

at step 5, create new node in list, have value dependency of item .

at step 6, insert item in node created @ step 5.

hope helps!

algorithm

No comments:

Post a Comment