algorithm - Union-Find: retrieve all members of a set efficiently -
i'm working union-find
algorithm. in first part of program, algorithm computes partition of big set e
.
after that, want retrieve members of set s
, contains given node x
.
until now, naively, testing membership of elements of e
set s
. yesterday reading "introduction algorithms" (by clrs, 3rd edition, ex. 21.3-4), and, in exercises, found that:
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.
"linear in number of members of x
's set" great improvement problem! so, i'm trying solve exersice... , after unsuccessful tries, i'm asking on stack overflow!
recall union-find implemented upside-down tree, each set s = {v1, v2, ..., vn}, have vn - 1 edges, have same root (or sink).
now, whenever add edge (vi, vj) tree, add edge (using new attribute) (vj, vi). , when remove node, remove attribute well.
note new edge separated old one. use only when printing elements in set. , modify whenever original edge modified in original algorithm.
note attribute list of nodes, total number of elements in lists combined still n - 1.
this give second tree, not upside down. now, using root, , doing tree-traversal (using e.g. bfs or dfs), can print elements.
Comments
Post a Comment