Skip to content

Instantly share code, notes, and snippets.

Created December 26, 2012 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/2df01576cd9843d34b10 to your computer and use it in GitHub Desktop.
Save anonymous/2df01576cd9843d34b10 to your computer and use it in GitHub Desktop.
(defn find-root [graph ^long node-id]
(let [^Node node (get graph node-id)
parent-id (.parent-id node)]
(if (= parent-id node-id)
[graph node-id]
(let [[graph ^long root-id] (find-root graph parent-id)]
[(update-in graph [node-id] set-parent root-id)
root-id]))))
@jordanlewis
Copy link

OK, so the reason it hurts your time complexity is that the path compression only helps the nodes between the one you look up and the root! Usually, since the rest of the path before the node you look up continues to point at the node that gets compressed, it'll be quicker to find the root next time for the whole path, not just the nodes between the looked-up node and the root. To me it's a pretty clear worsening of the time guarantees.

@jordanlewis
Copy link

Oh, that makes sense. Nevermind!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment