不相交集的链表表示 - 算法简介文本中的遗漏?

发布于 2024-09-07 05:07:53 字数 728 浏览 5 评论 0原文

成功解决了我的上一个 CLRS 问题, 这是另一个:

算法简介,第二版,第 12 页。 501-502,描述了不相交集合的链表表示,其中每个列表成员维护以下三个字段:

  • 将成员
  • 指针设置为下一个对象
  • 指针返回到第一个对象(集合代表)。

尽管链表可以仅使用单个“链接”对象类型来实现,但教科书显示了一个辅助“链表”对象,其中包含指向“头”链接和“尾”链接的指针。拥有指向“尾部”的指针有利于Union(x, y)操作,因此不需要遍历较大集合x中的所有链接来开始将较小的集合 y 的链接附加到它。

然而,为了获得对尾部链接的引用,似乎每个链接对象都需要维护一个第四字段:对链接列表辅助对象本身的引用。在这种情况下,为什么不完全删除链接列表对象并使用第四个字段直接指向尾部呢?

您认为这是文本中的遗漏吗?

Having had success with my last CLRS question, here's another:

In Introduction to Algorithms, Second Edition, p. 501-502, a linked-list representation of disjoint sets is described, wherein each list member the following three fields are maintained:

  • set member
  • pointer to next object
  • pointer back to first object (the set representative).

Although linked lists could be implemented by using only a single "Link" object type, the textbook shows an auxiliary "Linked List" object that contains a pointer to the "head" link and the "tail" link. Having a pointer to the "tail" facilitates the Union(x, y) operation, so that one need not traverse all of the links in a larger set x in order to start appending the links of the smaller set y to it.

However, to obtain a reference to the tail link, it would seem that each link object needs to maintain a fourth field: a reference to the Linked List auxiliary object itself. In that case, why not drop the Linked List object entirely and use that fourth field to point directly to the tail?

Would you consider this an omission in the text?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2024-09-14 05:07:53

我刚刚打开文本,教科书的描述对我来说似乎很好。

据我了解,数据结构是这样的:

struct Set {
    LinkedListObject * head;
    LinkedListObject * tail;
};

struct LinkedListObject {
    Value set_member;
    Set *representative;
    LinkedListObject * next;
};

教科书没有谈到我的书(第二版)中的任何“辅助”链表结构。你能贴出相关段落吗?

建立一个联盟会是这样的:

// No error checks.
Set * Union(Set *x, Set *y) {

    x->tail->next = y->head;    
    x->tail = y->tail;

    LinkedListObject *tmp = y->head;

    while (tmp) {

        tmp->representative = x;
        tmp = tmp->next;

    }
    return x;
}

I just opened the text and the textbook description seems fine to me.

From what I understand the data-structure is something like:

struct Set {
    LinkedListObject * head;
    LinkedListObject * tail;
};

struct LinkedListObject {
    Value set_member;
    Set *representative;
    LinkedListObject * next;
};

The textbook does not talk of any "auxillary" linked list structure in the book I have (second edition). Can you post the relevant paragraph?

Doing a Union would be something like:

// No error checks.
Set * Union(Set *x, Set *y) {

    x->tail->next = y->head;    
    x->tail = y->tail;

    LinkedListObject *tmp = y->head;

    while (tmp) {

        tmp->representative = x;
        tmp = tmp->next;

    }
    return x;
}
表情可笑 2024-09-14 05:07:53
why not drop the Linked List object entirely and use that fourth field to point directly to the tail?

可以从路径压缩中获得见解。所有元素都应该指向列表的头部。如果没有发生,则查找集操作会执行此操作(通过更改 p[x] 并返回它)。你同样谈论尾巴。所以只有实现了这样的功能我们才能使用它。

why not drop the Linked List object entirely and use that fourth field to point directly to the tail?

An insight can be taken from path compression. There all the elements are supposed to point to head of list. If it doesn't happen then the find-set operation does that (by changing p[x] and returning that). You talk similarly of tail. So if such function is implemented only then can we use that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文