Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions hairNet/hairNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ def getChain(self, x):
ret.append(x_next)
x_next = self.next[x_next]
return ret
def reverseChain(self, x_root, x): # x_root is the head of list, x is the end of list
if self.rank[x_root] == 1: return
def reverseChain(self, x_root): # x_root is the head of list
if self.rank[x_root] == 1: return x_root

x = x_root # x is the end of list
while self.next[x] != -1:
x = self.next[x] # find last element

x_pre = -1
x_cur = x_root
while x_cur != -1:
Expand All @@ -75,21 +80,21 @@ def reverseChain(self, x_root, x): # x_root is the head of list, x is the end of
x_pre = x_cur
x_cur = x_next
self.rank[x] = self.rank[x_root]

return x # return new root
def union(self, x, y):
x_root = self.findRoot(x)
y_root = self.findRoot(y)
if x_root == y_root: # already connected
return

if y_root != y and x_root != x:
if (y_root != y and x_root != x) or (y_root == y and x_root == x):
# Case 1: two root points are independent, should reverse one of chains, choose shorter chain to reverse
# and transform this situation to Case 2
if self.rank[x_root] <= self.rank[y_root]:
self.reverseChain(x_root, x)
x_root = x
x_root = self.reverseChain(x_root)
else:
self.reverseChain(y_root, y)
y_root = y
y_root = self.reverseChain(y_root)

if y_root == y: # Case 2: one of two points is dependent or all two points are dependent
if self.next[x] != -1:
Expand Down