algorithm - Printing out nodes in a disjoint-set data structure in linear time -
i'm trying exercise in introduction algorithms cormen et al has disjoin set data structure:
suppose wish add operation
print-set(x)
, given nodex
, prints members ofx
's set, in order. show how can add single attribute each node in disjoint-set forestprint-set(x)
takes time linear in number of members ofx
's set, , asymptotic running times of other operations unchanged. assume can print each member of set in o(1) time.
now, i'm quite sure attribute needed tail pointer, can keep track of children.
since disjoint set structure has parent attribute, find-set(x)
can print out nodes going in 1 direction. now, having tail pointer, let's go other direction well.
however, i'm not sure how write algorithm this. if me out in pseudocode, appreciated.
each node should have next
pointer next node in set in. nodes in set should form circular linked list.
when singleton set first created, node's next
pointer points itself.
when merge set node x
, set node y
(and you've checked sets different normalizing set representatives), merge circular linked lists, can swapping x.next
, y.next
; o(1)
operation.
to list elements in set containing node x
, traverse circular linked list starting x
.
Comments
Post a Comment