1. <tfoot id='860Cg'></tfoot>
    <i id='860Cg'><tr id='860Cg'><dt id='860Cg'><q id='860Cg'><span id='860Cg'><b id='860Cg'><form id='860Cg'><ins id='860Cg'></ins><ul id='860Cg'></ul><sub id='860Cg'></sub></form><legend id='860Cg'></legend><bdo id='860Cg'><pre id='860Cg'><center id='860Cg'></center></pre></bdo></b><th id='860Cg'></th></span></q></dt></tr></i><div id='860Cg'><tfoot id='860Cg'></tfoot><dl id='860Cg'><fieldset id='860Cg'></fieldset></dl></div>
    <legend id='860Cg'><style id='860Cg'><dir id='860Cg'><q id='860Cg'></q></dir></style></legend>
      • <bdo id='860Cg'></bdo><ul id='860Cg'></ul>

      <small id='860Cg'></small><noframes id='860Cg'>

      在 Python 中实现不相交集系统

      时间:2023-08-30
    1. <legend id='SlxrC'><style id='SlxrC'><dir id='SlxrC'><q id='SlxrC'></q></dir></style></legend>
        1. <tfoot id='SlxrC'></tfoot>

          <small id='SlxrC'></small><noframes id='SlxrC'>

            <bdo id='SlxrC'></bdo><ul id='SlxrC'></ul>
              <tbody id='SlxrC'></tbody>

                <i id='SlxrC'><tr id='SlxrC'><dt id='SlxrC'><q id='SlxrC'><span id='SlxrC'><b id='SlxrC'><form id='SlxrC'><ins id='SlxrC'></ins><ul id='SlxrC'></ul><sub id='SlxrC'></sub></form><legend id='SlxrC'></legend><bdo id='SlxrC'><pre id='SlxrC'><center id='SlxrC'></center></pre></bdo></b><th id='SlxrC'></th></span></q></dt></tr></i><div id='SlxrC'><tfoot id='SlxrC'></tfoot><dl id='SlxrC'><fieldset id='SlxrC'></fieldset></dl></div>

                本文介绍了在 Python 中实现不相交集系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                到目前为止,我所掌握的内容主要基于 Cormen 等人的算法简介"第 571 页.

                What I have so far is largely based off page 571 of "Introduction To Algorithms" by Cormen et al.

                我在 python 中有一个 Node 类,它代表一个集合:

                I have a Node class in python that represents a set:

                class Node:
                    def __init__(self, parent, rank = 0):
                        self.parent = parent
                        self.rank = rank

                此实现将使用节点的 List 作为森林(我愿意接受更好的方法来存储集合).

                This implementation is going to use a List of Nodes as the forest (I am open to better ways to store the sets).

                Initialize() 返回一个节点列表,我将其存储在变量 set 中并传递给其他函数.

                Initialize() returns a list of Nodes, that I will store in variable set and pass into the other functions.

                Find 在森林中搜索值并返回它出现的集合.我选择使用 for s in range(len(set)): 所以在递归中,我可以缩小 set[s:] 传入的集合列表.

                Find searches through the forest for the value and returns the set that it appears in. I chose to use for s in range(len(set)): so that in the recursion I could shrink the list of sets being passed in by set[s:].

                def Find(set, value):
                    for s in range(len(set)):
                        if value != set[s].parent:
                            set[s].parent = Find(set[s:], set[s].parent)
                        return set[s]

                Merge 通过找到它们并提升排名较高的集合来合并森林中的集合.

                Merge merges sets in the forest by finding them and promoting the higher ranked set.

                def Merge(set, value1, value2):
                    set_val_1 = Find(set, value1)
                    set_val_2 = Find(set, value2)
                    if set_val_1.rank > set_val_2.rank:
                        set_val_2.parent = set_val_1
                    else:
                        set_val_1.parent = set_val_2
                        if set_val_1.rank == set_val_2.rank:
                            set_val_2.rank += 1

                我在执行 Finds 和 Merges 时遇到了一些错误,即 Find 没有返回正确的集合,所以我我不确定 Merge 是否也能正常工作.我会很感激一些帮助,以确保正确实现这些功能.

                I am getting some errors when I do Finds and Merges, namely Find is not returning the proper set, and so I am not sure if Merge is working properly as well. I would appreciate some help to make sure the functions are implemented properly.

                推荐答案

                我没有这本书的最新版本,但这看起来不像是不相交的森林.

                I don't have the latest edition of the book, but this doesn't look quite like a disjoint-set forest.

                我认为您的错误是认为必须将森林存储在集合中,并且您必须遍历该集合才能对节点进行操作.从 Merge()Find() 中移除 set 并实现 Find() as

                I think your mistake is to think that the forest has to be stored in a collection and that you have to traverse this collection to do the operations on the nodes. Remove set from Merge() and Find() and implement Find() as

                def Find(n):
                    if n != n.parent:
                        n.parent = Find(n.parent)
                    return n.parent
                

                就像书中一样.然后添加一个返回单个正确初始化节点的 MakeSet(),也许还有一个 SameSet() 函数:

                just like in the book. Then add a MakeSet() that returns a single correctly initialized node, and maybe a SameSet() function too:

                def SameSet(n1, n2):
                    return Find(n1) == Find(n2)
                

                你现在有一个工作的不相交集实现.

                You now have a working disjoint set implementation.

                这篇关于在 Python 中实现不相交集系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Python:按字符的位置拆分字符串 下一篇:在文件python中查找和替换多个单词

                相关文章

                <tfoot id='ioLV0'></tfoot>

                <small id='ioLV0'></small><noframes id='ioLV0'>

                  <bdo id='ioLV0'></bdo><ul id='ioLV0'></ul>
                  <i id='ioLV0'><tr id='ioLV0'><dt id='ioLV0'><q id='ioLV0'><span id='ioLV0'><b id='ioLV0'><form id='ioLV0'><ins id='ioLV0'></ins><ul id='ioLV0'></ul><sub id='ioLV0'></sub></form><legend id='ioLV0'></legend><bdo id='ioLV0'><pre id='ioLV0'><center id='ioLV0'></center></pre></bdo></b><th id='ioLV0'></th></span></q></dt></tr></i><div id='ioLV0'><tfoot id='ioLV0'></tfoot><dl id='ioLV0'><fieldset id='ioLV0'></fieldset></dl></div>
                    <legend id='ioLV0'><style id='ioLV0'><dir id='ioLV0'><q id='ioLV0'></q></dir></style></legend>