0

I'm doing a project based on hashtables and disjoint sets, while implementing the makeset function for the nodes i had a doubt about it. This is the pseuodocode of the makeset function (i'm using path compression and rank euristics):

Makeset(x)
{
  x.parent = x;
  x.rank = 0
}

This should make the set containing only the element x by theory. Now looking at this image:

Path image

If i call the makeset function on element 1 of the image, the set after the function has x as it's parent (as it already was before) but rank = 0, the problem is that the nodes connected to it (2,3,4 and 5 in the e.g) are still connected to it. Is this the right way to do this ? i followed the pseudocodes on cormen's algorithm introduction book.

Emanuele Illiano
  • 31
  • 1
  • 1
  • 3
  • Generally, the `Makeset` function should only be called once, for all elements, in the beginning of the program, to initialize all sets as empty and disjoint from each other. It doesn't make sense to me that you are calling `Makeset` after some nodes already have another parent (who's not themselves). – Daniel Jul 02 '20 at 14:15
  • I know, i think the same way as you, but it's an university project and i have to implement the makeset as a method that the user can call at runtime. – Emanuele Illiano Jul 02 '20 at 14:23
  • But is this `Makeset` supposed to ask a node as input? When I implement `Makeset`, is frequently something like `Makeset(){ for(int i = 0; i < n; i++) parent[i] = i; }`, meaning I call it once to reset all nodes. – Daniel Jul 02 '20 at 14:25
  • The pseudocode from cormen has x has parameter, where x a single node in the set list – Emanuele Illiano Jul 02 '20 at 15:26
  • But in the start it may call it for every node, doesn't it? – Daniel Jul 02 '20 at 19:05
  • yes, in the start i call it of each node during hash insert. – Emanuele Illiano Jul 05 '20 at 09:09

0 Answers0