Skip to content

Possible improvement to path compression runtime #321

@gnat79

Description

@gnat79

p, self.parent_arr[p] = self.parent_arr[p], n

I thinks the intention here is to implement path compression, but it could be that this code only does 1/k of the total optimization because of the way the tuple assignment works. Here "k" would be the number of hops from this node up to the root. If I'm right, this would make a big difference for large datasets where the MST is fairly large.

If I understand python correctly (debatable!), the right side tuple is evaluated from left to right, and then the left side assignments are made from left to right. That means that means that p on the left is updated before self.parent[p] is updated, which I do not believe is the intended behavior.

I think the following code will work correctly. I can probably fork and test this code over the weekend, but hoping Leland or another author can maybe clarify before I dig in too much.

        while self.parent_arr[p] != n:
            #p, self.parent_arr[p] = self.parent_arr[p], n
            tmp = self.parent_arr[p]
            self.parent_arr[p] = n
            p = tmp
        return n

Thanks in advance for spending time looking at this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions