23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

O(logn). Thus, we can summarize the performance of our simple sequence-based<br />

partition implementation as follows.<br />

Proposition 11.10: Us<strong>in</strong>g a sequence-based implementation of a<br />

partition, <strong>in</strong> a series of nmakeSet, union, <strong>and</strong>f<strong>in</strong>d operations start<strong>in</strong>g from<br />

an <strong>in</strong>itially empty partition, the amortized runn<strong>in</strong>g time of each operation is<br />

O(logn).<br />

Note that <strong>in</strong> this sequence-based implementation of a partition, each f<strong>in</strong>d<br />

operation takes worst-case O(1) time. It is the runn<strong>in</strong>g time of the union<br />

operations that is the computational bottleneck.<br />

In the next section, we describe a tree-based implementation of a partition that<br />

does not guarantee constant-time f<strong>in</strong>d operations, but has amortized time much<br />

better than O(logn) per union operation.<br />

11.6.3 A Tree-Based Partition Implementation <br />

An alternative data structure uses a collection of trees to store the n elements <strong>in</strong> sets,<br />

where each tree is associated with a different set. (See Figure 11.17.) In particular,<br />

we implement each tree with a l<strong>in</strong>ked data structure whose nodes are themselves the<br />

set position objects. We still view each position p as be<strong>in</strong>g a node hav<strong>in</strong>g a variable,<br />

element, referr<strong>in</strong>g to its element x, <strong>and</strong> a variable, set, referr<strong>in</strong>g to a set conta<strong>in</strong><strong>in</strong>g x,<br />

as before. But now we also view each position p as be<strong>in</strong>g of the "set" data type.<br />

Thus, the set reference of each position p can po<strong>in</strong>t to a position, which could even<br />

be p itself. Moreover, we implement this approach so that all the positions <strong>and</strong> their<br />

respective set references together def<strong>in</strong>e a collection of trees.<br />

We associate each tree with a set. For any position p, if p's set reference po<strong>in</strong>ts back<br />

to p, then p is the root of its tree, <strong>and</strong> the name of the set conta<strong>in</strong><strong>in</strong>g p is "p" (that is,<br />

we will be us<strong>in</strong>g position names as set names <strong>in</strong> this case). Otherwise, the set<br />

reference for p po<strong>in</strong>ts to p's parent <strong>in</strong> its tree. In either case, the set conta<strong>in</strong><strong>in</strong>g p is<br />

the one associated with the root of the tree conta<strong>in</strong><strong>in</strong>g p.<br />

Figure 11.17: Tree-based implementation of a<br />

partition consist<strong>in</strong>g of three disjo<strong>in</strong>t sets: A = 1,4,7, B =<br />

2,3,6,9, <strong>and</strong> C = 5,8,10,11,12.<br />

722

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!